perf: avoid full response model_dump in usage cost path#1886
Merged
alexmojaki merged 8 commits intoApr 29, 2026
Merged
Conversation
- Use model_dump(include=...) to pass only pricing fields - Fall back to plain model_dump() when include is unsupported - Add focused tests for include path and fallback path
- Run include/fallback assertions only when pricing backend is available - Prevent false failures on pydantic 2.4 job
Collaborator
|
The complication not mentioned in the issue is that this should only apply to embeddings responses so that it doesn't get in the way of pydantic/genai-prices#290 in the future. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
- use model_dump(include={'model', 'usage'}) only for openai embeddings
- keep plain model_dump() for chat, responses, and non-openai providers
- align tests with scoped behavior and keep ci-matrix-safe assertions
4d8ccde to
d66bae2
Compare
Contributor
Author
alexmojaki
reviewed
Apr 28, 2026
- remove defensive TypeError fallback for include model_dump - keep scoped include path for openai embeddings only - drop fallback-only test and keep scoped behavior coverage
Collaborator
|
thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1844
get_usage_attributes()callsresponse.model_dump()on the full response before passing it togenai_prices.extract_usage(). For embedding responses this serializes all the embedding vectors even thoughextract_usageonly reads two top-level fields (modelandusagefor OpenAI/Anthropic,modelVersionandusageMetadatafor Google GenAI).What changed
The cost path now calls
model_dump(include=...)with just those four fields. Falls back to plainmodel_dump()ifinclude=is not supported (TypeError), keeping compatibility with custom response wrappers.Tests
test_model_dump_prefers_include_for_cost— confirmsinclude=is passed on the primary pathtest_model_dump_include_fallback_to_plain_dump— confirms the TypeError fallback still computes costGENAI_PRICES_AVAILABLEso environments where the pricing backend cannot import (e.g. pydantic 2.4 matrix) do not produce false failures