Repository navigation
Serialize Pydantic models by alias so they round-trip - #321
Open
jimbobbennett wants to merge 1 commit into
Open
jimbobbennett wants to merge 1 commit into
jimbobbennett wants to merge 1 commit into
Conversation
DefaultSerde and PydanticJsonSerde dumped Pydantic models with model_dump_json(), which writes field names, while model_validate_json() reads aliases. An aliased field therefore did not survive a journal round trip. For the OpenAI Agents integration, a Responses API function call was journaled with "async_" instead of "async"; on replay the unknown key was kept as an extra and sent in the next request, which the API rejects with 400 "Unknown parameter: 'input[1].async_'". Dump with by_alias=True in both serdes and in RestateModelResponse.to_input_items(). Fixes restatedev#320 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
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
DefaultSerdeandPydanticJsonSerdedump Pydantic models withmodel_dump_json(), which writes field names.model_validate_json()reads aliases, so a model with an aliased field doesn't survive a journal round trip.With the OpenAI Agents integration, this breaks every agent that calls a function tool. The journaled
call LLMresult stores the Responses API function call with"async_"instead of"async". On replay,async_is an unknown key, so the OpenAI model keeps it as an extra, and it goes out in the next request:The invocation then retries forever. Details and a standalone repro are in #320.
Fix
Dump with
by_alias=Truein:DefaultSerde.serialize(Pydantic branch)PydanticJsonSerde.serializeRestateModelResponse.to_input_items(), which builds Responses API input and so needs wire names tooDeserialization is unchanged.
model_validate_json()already reads aliases, so dump and validate now agree. Models without aliases serialize exactly as before.One behavior change: a handler whose input or output type is a Pydantic model with aliases now writes the aliased keys to the wire, which matches the JSON schema the SDK already publishes for it (
model_json_schema()uses aliases by default). Before this change, such a model couldn't round-trip throughPydanticJsonSerdeat all unless it setpopulate_by_name.Tests
tests/serde.pygets two tests, and both fail without the fix:RestateModelResponseholding aResponseFunctionToolCallreplays withasyncand noasync_just lint, pyright, mypy, andjust test-fast(506 passed) all pass locally. I didn't run the server-backed tests.End to end, I ran a
DurableRunnerweather agent with a function tool (restate-sdk1.5.4 with this patch,openai-agents0.25.0,restate-server1.7.10) against the OpenAI Responses API. Without the patch it loops on the 400 above. With it, the invocation completes and the tool call replays cleanly.Fixes #320
🤖 Generated with Claude Code