Skip to content

OpenRouter uses non-compatible finish reason #2844

@jscheel

Description

@jscheel

Initial Checks

Description

Chat Completions compatible APIs should use the following finish_reason values

["stop", "length", "tool_calls", "content_filter", "function_call"]

However, OpenRouter does not use function_call and introduces a new error finish_reason. This causes pydantic ai to raise a validation error:

pydantic_ai.exceptions.UnexpectedModelBehavior: Invalid response from OpenAI chat completions endpoint: 1 validation error for ChatCompletion
choices.0.finish_reason
  Input should be 'stop', 'length', 'tool_calls', 'content_filter' or 'function_call' [type=literal_error, input_value='error', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error

OpenRouter docs: https://openrouter.ai/docs/api-reference/overview#finish-reason

See https://pydanticlogfire.slack.com/archives/C083V7PMHHA/p1757441718457539?thread_ts=1757428644.886389&cid=C083V7PMHHA for context.

Example Code

Replace model name with openrouter:google/gemini-2.5-flash-lite in weather example

Python, Pydantic AI & LLM client version

python 3.13
pydantic-ai 1.0.2

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions