Open
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
Description
When using the Mistral model with pydantic-ai agents, passing message history from a previous result to a new run causes an error. The API returns a 400 error with the message "Unexpected role 'user' after role 'tool'".
Example Code
from pydantic_ai.models.mistral import MistralModel
from pydantic_ai.agent import Agent
from pydantic import BaseModel, Field
class PoemInfo(BaseModel):
title: str = Field(..., description="Create a fitting titel for your poem")
creator: str = Field(..., description="the creator of the poem")
text: str = Field(..., description="The complete poem")
model = MistralModel('mistral-small-latest', api_key="API_KEY_HERE")
agent = Agent(model=model, result_type=PoemInfo)
# This works fine
result0 = agent.run_sync("Make a Poem, about singing like a bird on a motorbike")
# This also works fine
result1 = agent.run_sync("repeat the last answer")
# This causes the error
result2 = agent.run_sync("repeat the last answer", message_history=result0.new_messages())
### LOG Error Message
mistralai.models.sdkerror.SDKError: API error occurred: Status 400
{"object":"error","message":"Unexpected role 'user' after role 'tool'","type":"invalid_request_error","param":null,"code":null}
Python, Pydantic AI & LLM client version
Python 3.12.3 on ubuntu 24.04 amd64
mistralai==1.6.0
pydantic-ai==0.0.43