Skip to content

fix(messages): preserve RunUsage on response roundtrip#5747

Open
he-yufeng wants to merge 1 commit into
pydantic:mainfrom
he-yufeng:fix/model-response-run-usage
Open

fix(messages): preserve RunUsage on response roundtrip#5747
he-yufeng wants to merge 1 commit into
pydantic:mainfrom
he-yufeng:fix/model-response-run-usage

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Summary

  • allow ModelResponse.usage to deserialize either per-request usage or run-level usage
  • keep the default as RequestUsage so existing stored messages still round-trip as before
  • add a regression test for RunUsage.requests and RunUsage.tool_calls surviving JSON round-trip

Fixes #5744.

To verify

  • PYTHONUTF8=1 uv run pytest tests\test_messages.py::test_pre_usage_refactor_messages_deserializable tests\test_messages.py::test_model_response_preserves_run_usage_roundtrip tests\test_messages.py::test_file_part_serialization_roundtrip -q
  • PYTHONUTF8=1 uv run ruff check pydantic_ai_slim\pydantic_ai\messages.py tests\test_messages.py
  • PYTHONUTF8=1 uv run ruff format --check pydantic_ai_slim\pydantic_ai\messages.py tests\test_messages.py
  • PYTHONUTF8=1 PYRIGHT_PYTHON_IGNORE_WARNINGS=1 uv run pyright pydantic_ai_slim\pydantic_ai\messages.py tests\test_messages.py

I also ran the full tests\test_messages.py file. The new usage-related tests passed; the full file still has two Windows-local platform failures unrelated to this change (.csv MIME detection and CRLF bytes from Path.write_text).

@github-actions github-actions Bot added size: S Small PR (≤100 weighted lines) bug Report that something isn't working, or PR implementing a fix labels Jun 1, 2026
@he-yufeng he-yufeng force-pushed the fix/model-response-run-usage branch from 2878aac to 72dc3e2 Compare June 1, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: S Small PR (≤100 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[roundtrip-sweep] ModelResponse.usage: RunUsage loses requests and tool_calls on roundtrip

1 participant