fix: handle serde duplicate field error when providers send both legacy and new usage field names#331
Merged
Merged
Conversation
…cy and new usage field names GPT-5.4 (and potentially other newer models) return both legacy (prompt_tokens/completion_tokens) and new (input_tokens/output_tokens) field names in the usage object simultaneously. serde_json >= 1.0.120 tracks visited fields during struct deserialization. When an alias maps to a field already set from the primary key, it raises 'duplicate field' error, breaking all reviews using affected models. Fix: Replace serde alias-based Usage deserialization with manual parse_usage_value() that extracts from serde_json::Value with explicit preference order (primary > camelCase > new). Updated both streaming and non-streaming code paths. Closes #330
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.
Problem
GPT-5.4 returns both legacy (
prompt_tokens) and new (input_tokens) field names in the usage object simultaneously.serde_json>= 1.0.120 tracks visited fields during struct deserialization — when an alias maps to a field already set from the primary key, it raisesduplicate fielderror, breaking all reviews using affected models.Closes #330
Changes
Before:
Usagestruct used#[serde(alias = "input_tokens")]to accept both naming conventions. When both names appeared in the same JSON, serde errored.After:
Usageis now a plain struct (no serde derive). Newparse_usage_value()function extracts fromserde_json::Valuewith explicit preference order:prompt_tokens→completion_tokens→total_tokenspromptTokens→completionTokens→totalTokensinput_tokens→output_tokensThis handles all three scenarios:
Both streaming (
extract_stream_usage) and non-streaming (ChatResponse) code paths updated.Tests
Cora Self-Review Notes
Cora flagged 2 theoretical concerns on this PR:
prompt_tokens: 0withinput_tokens > 0. No known provider does this; GPT-5.4 sends matching values.Both are acceptable trade-offs for now. Can revisit if a provider actually exhibits these behaviors.