You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
frompydantic_ai.models.mistralimportMistralModelfrompydantic_ai.agentimportAgentfrompydanticimportBaseModel, FieldclassPoemInfo(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 fineresult0=agent.run_sync("Make a Poem, about singing like a bird on a motorbike")
# This also works fineresult1=agent.run_sync("repeat the last answer")
# This causes the errorresult2=agent.run_sync("repeat the last answer", message_history=result0.new_messages())
### LOG Error Messagemistralai.models.sdkerror.SDKError: APIerroroccurred: Status400
{"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
The text was updated successfully, but these errors were encountered:
defnew_mixtral_messages(result: AgentRunResult) ->list[ModelMessage]:
""" Converts messages to a format compatible with Mistral: - Converts ToolCallPart to TextPart containing the args as text - Skips ToolReturnPart entirely - Preserves message metadata and other part types """new_messages= []
formsginresult.new_messages():
new_parts= []
forpartinmsg.parts:
ifisinstance(part, ToolCallPart):
# Convert tool call to text, handling both dict and string argscontent=part.args_as_json_str() ifisinstance(part.args, dict) elsestr(part.args)
new_parts.append(TextPart(content=content))
elifisinstance(part, ToolReturnPart):
# Skip tool returnscontinueelse:
# Keep all other parts unchangednew_parts.append(part)
# Create the appropriate message typeifisinstance(msg, ModelResponse):
new_msg=ModelResponse(
parts=new_parts,
model_name=msg.model_name,
timestamp=msg.timestamp
)
elifisinstance(msg, ModelRequest):
new_msg=ModelRequest(parts=new_parts)
else:
raiseNotImplemented("There should not be another type of message!")
new_messages.append(new_msg)
returnnew_messages
Initial Checks
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
Python, Pydantic AI & LLM client version
The text was updated successfully, but these errors were encountered: