Skip to content

Anthropic models failing by injecting phantom empty message #1854

@nbenedefinite

Description

@nbenedefinite

Initial Checks

Description

When running Anthropic models with Pydantic AI, jobs consistently fail around message 10 due to this error: 'message': 'messages.12: all messages must have non-empty content except for the optional final assistant message'.

All of our messages have content in the Pydantic array, but when Pydantic AI converts to the Anthropic array, it injects additional empty messages - sometimes 2 or more - into the new array. In addition, message 12 in the Pydantic array is often mislabeled as a neighbor (see truncated json), meaning message 12 in neither array is actually empty - despite the error saying it is.

Included are these two arrays from a failing job - they have been truncated to address the "message 12" error. Notice the mismatch of indexes within each array, as well as the presence of a phantom empty "user" message in the Anthropic array. Both message arrays were grabbed from the "Raw Data" tab in Logfire.
truncated_anthropic_messages.json
truncated_pydantic_messages.json

Example Code

Not applicable - data mismatch in Pydantic AI

Python, Pydantic AI & LLM client version

Python = 3.13.1
Pydantic AI = 0.2.11
LLM Client = claude-3-7-sonnet-20250219

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions