Skip to content

Serialize Pydantic models by alias so they round-trip - #321

Open
jimbobbennett wants to merge 1 commit into
restatedev:mainfrom
jimbobbennett:fix/pydantic-serde-by-alias
Open

jimbobbennett wants to merge 1 commit into
restatedev:mainfrom
jimbobbennett:fix/pydantic-serde-by-alias

Conversation

@jimbobbennett

Copy link
Copy Markdown

Problem

DefaultSerde and PydanticJsonSerde dump Pydantic models with model_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 LLM result 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:

openai.BadRequestError: Error code: 400 - Unknown parameter: 'input[1].async_'.

The invocation then retries forever. Details and a standalone repro are in #320.

Fix

Dump with by_alias=True in:

  • DefaultSerde.serialize (Pydantic branch)
  • PydanticJsonSerde.serialize
  • RestateModelResponse.to_input_items(), which builds Responses API input and so needs wire names too

Deserialization 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 through PydanticJsonSerde at all unless it set populate_by_name.

Tests

tests/serde.py gets two tests, and both fail without the fix:

  • an aliased model round-trips through both serdes with no leftover extras
  • a journaled RestateModelResponse holding a ResponseFunctionToolCall replays with async and no async_

just lint, pyright, mypy, and just test-fast (506 passed) all pass locally. I didn't run the server-backed tests.

End to end, I ran a DurableRunner weather agent with a function tool (restate-sdk 1.5.4 with this patch, openai-agents 0.25.0, restate-server 1.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

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]>
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@jimbobbennett

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pydantic serde drops field aliases, so OpenAI Agents replays send async_ and fail with 400

1 participant