fix: embedding deserialization breaks with serde_json/arbitrary_precision#1518
Open
EinarasGar wants to merge 1 commit into0xPlaygrounds:mainfrom
Open
fix: embedding deserialization breaks with serde_json/arbitrary_precision#1518EinarasGar wants to merge 1 commit into0xPlaygrounds:mainfrom
EinarasGar wants to merge 1 commit into0xPlaygrounds:mainfrom
Conversation
…sion Change embedding response structs to use `Vec<serde_json::Number>` instead of `Vec<f64>`, then convert to f64 after parsing. When `serde_json/arbitrary_precision` is enabled (commonly pulled in by `rust_decimal/serde-with-arbitrary-precision`), all JSON numbers are represented as strings internally. This causes `Vec<f64>` fields to fail deserialization with: "data did not match any variant of untagged enum ApiResponse" `serde_json::Number` deserializes correctly regardless of whether `arbitrary_precision` is active, then `.as_f64()` converts to the expected type. Affects all providers: Gemini, OpenAI, Mistral, OpenRouter, Together, and Cohere.
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
Embedding deserialization fails when
serde_json'sarbitrary_precisionfeature is enabled anywhere in the dependency tree. The error is:This is commonly triggered by
rust_decimalwith theserde-with-arbitrary-precisionfeature, which is widely used in financial applications.Root cause
When
serde_json/arbitrary_precisionis active, all JSON numbers are internally represented as strings. TheVec<f64>fields in embedding response structs (EmbeddingValues,EmbeddingData, etc.) fail to deserialize because serde expects native floats but receives string-backed numbers.The
arbitrary_precisionfeature is a compile-time, crate-wide flag — it cannot be opted out of per-deserialization call. Any crate in the dependency tree that enables it (directly or transitively) affects allserde_jsonusage in the binary.Reproduction
Any project that combines rig embeddings with
rust_decimal:The same code works in a crate without
rust_decimalin the dependency tree.Fix
Change embedding response structs from
Vec<f64>toVec<serde_json::Number>, then convert tof64via.as_f64()when building theEmbeddingstruct.serde_json::Numberis the canonical number type that works correctly regardless of whetherarbitrary_precisionis enabled — it's the internal representation serde_json uses in both modes.Affected providers
All 6 providers with embedding support:
EmbeddingValues.values)EmbeddingData.embedding)EmbeddingData.embedding)EmbeddingData.embedding)EmbeddingData.embedding)EmbeddingResponse.embeddings)Change summary
Each provider's embedding response struct:
Vec<f64>→Vec<serde_json::Number>, with.filter_map(|n| n.as_f64())at the point where theEmbeddingstruct is built. No new dependencies, no API changes.