Add `ModelResponse.model_id` recording the model the agent sent each request to

Merged
DO
1mo ago
+1300-83
  • Related: #11789 (audit of __pydantic_ai__ metadata), #11330 (sticky ModelFallback, which builds on this)

Adds ModelResponse.model_id: the model the agent sent each request to, identified the way you configured it. That's the string the model was selected by (like 'openai:gpt-5', or an alias a resolve_model_id capability resolved), or otherwise the model's Model.model_id. model_name is what the provider reports, often a dated version (gpt-4o answers as gpt-4o-2024-08-06), and the same for a gateway and a direct route to one model, so it can't tell which of your configured models answered. model_id can, which lets a later step or run send a request back to that model.

What changes

  • The field. ModelResponse.model_id: str | None = None. The agent loop sets it on every response it commits from a model request, streamed and not, from the attempt that produced it (request_context.model_id or model.model_id). It's set next to where run_id and conversation_id are filled, in _append_response and _commit_interrupted_response. For streams, the AgentStream carries the attempt's id onto stream_response() snapshots and the response run_stream records. A response that already has one keeps it. A response that never reached a model, like a SkipModelRequest before the request, gets None.
  • Untrusted history. sanitize_messages always resets model_id on responses, as it does workspace_ref, because Pydantic AI routes later requests by it. The trust-boundary docs list it.
  • ModelFallback's continuation pin moves onto the field. It no longer stamps metadata['__pydantic_ai__']['fallback_candidate']. A resumed suspended response goes back to the candidate whose model_id it records. FALLBACK_CAPABILITY_PIN_KEY is removed. That pin hasn't been released (#9348 is newer than v2.63.0), so stored histories don't need migrating.
  • Left alone for #11789: FallbackModel's fallback_model_id (released, model-level, also used outside the agent), the request-deadline stamp, and the other model-layer markers.

Decisions to review

  • Name: model_id, matching RunContext.model_id, ModelRequestContext.model_id and Model.model_id. Its value is exactly ctx.model_id or model.model_id.
  • Resume behavior is unchanged. The pin applies only when the suspended response's model_id is one of ModelFallback's candidates. A response a model outside the chain started continues on the step's model, as before. Before this PR, a response stamped with a non-candidate raised UserError; that path is gone, since every response now carries an id. Enforcing the pin for a model outside the chain (raising, or routing to it) is a possible later option.
  • Two candidates with the same model_id (same provider and model, different settings or keys) still can't be told apart, exactly as with the old pin.
  • Docs examples: the example harness now names its mock models after the provider they stand in for, so printed outputs show the model_id a user would see ('{{ openai:medium }}'), not function:....

Tests

  • tests/test_model_response_model_id.py covers:
    • a plain run;
    • a model selected by an alias only a capability resolves;
    • streamed;
    • a ModelFallback fallback, both plain and streamed;
    • a SkipModelRequest before any request;
    • a producer-supplied id being kept.
  • Existing tests extended:
    • continuation-pin tests in tests/test_capability_model_fallback.py;
    • sanitize in tests/test_sanitize_messages.py;
    • durable round trips: the Temporal ModelFallback workflow returns the candidate's model_id, and a DBOS run records the unwrapped model's id.
  • Snapshot updates to agent-run responses come from --inline-snapshot=fix, and docs example outputs from --update-examples.
  • scripts/durable_exec_contract.json is unchanged. The API check against v2.63.0 reports nothing for this change: an added field with a default needs no waiver.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code