Repository navigation
perf(dispatch): call model_validate_json directly and stream JSON into Python objects (#255) - #262
Merged
Conversation
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 #255
Summary of Changes
Pydantic Model Direct Validation (
src/dispatch.rs):serde_json::from_slice::<JsonValue>AST parsing and intermediate Python dictionary allocation whenbody_modelis configured.body_bytes(reusing the borrowedPyBytesbuffer zero-copy when available) directly to Pydantic'smodel_validate_json. This delegates JSON parsing to Pydantic v2's native Rust/C core (pydantic-core), avoiding double allocation and multiple AST traversals.model_validate(pyv)for models or validators that do not implementmodel_validate_json.Direct Streaming JSON Deserializer (
src/schema.rs):json_bytes_to_py(py, bytes)usingserde::de::Visitorandserde::de::DeserializeSeed.dict,list,str,int,float,bool,None) in a single pass without ever allocating an intermediateserde_json::Value,serde_json::Map, or RustStringAST.visit_borrowed_str) intoPyString.Error Handling & Parity:
{"error": "json: ..."}when non-Pydantic JSON parsing fails.{"detail": [...]}when Pydantic model validation fails.Tests (
tests/test_validation.py):test_model_validate_json_called_directlyto verify thatmodel_validate_jsonis invoked directly with raw bytes.test_body_model_fallback_without_model_validate_jsonto verify fallback for classes withoutmodel_validate_json.test_body_model_malformed_json_returns_422to verify 422 withjson_invaliderror on malformed JSON bodies.