Add `ModelResponse.model_id` recording the model the agent sent each request to
Merged
DO
1mo ago+1300+1300+0123456789012345678901234567890123456789-83-83-01234567890123456789
- Related: #11789 (audit of
__pydantic_ai__metadata), #11330 (stickyModelFallback, 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 whererun_idandconversation_idare filled, in_append_responseand_commit_interrupted_response. For streams, theAgentStreamcarries the attempt's id ontostream_response()snapshots and the responserun_streamrecords. A response that already has one keeps it. A response that never reached a model, like aSkipModelRequestbefore the request, getsNone. - Untrusted history.
sanitize_messagesalways resetsmodel_idon responses, as it doesworkspace_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 stampsmetadata['__pydantic_ai__']['fallback_candidate']. A resumed suspended response goes back to the candidate whosemodel_idit records.FALLBACK_CAPABILITY_PIN_KEYis 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'sfallback_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, matchingRunContext.model_id,ModelRequestContext.model_idandModel.model_id. Its value is exactlyctx.model_id or model.model_id. - Resume behavior is unchanged. The pin applies only when the suspended response's
model_idis one ofModelFallback'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 raisedUserError; 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_ida user would see ('{{ openai:medium }}'), notfunction:....
Tests
tests/test_model_response_model_id.pycovers:- a plain run;
- a model selected by an alias only a capability resolves;
- streamed;
- a
ModelFallbackfallback, both plain and streamed; - a
SkipModelRequestbefore 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
ModelFallbackworkflow returns the candidate'smodel_id, and a DBOS run records the unwrapped model's id.
- continuation-pin tests in
- Snapshot updates to agent-run responses come from
--inline-snapshot=fix, and docs example outputs from--update-examples. scripts/durable_exec_contract.jsonis 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