fix(core): preserve structured guardrail diagnostics and agent output during persistence - #5016
Open
rksharma-owg wants to merge 1 commit into
Open
rksharma-owg wants to merge 1 commit into
rksharma-owg wants to merge 1 commit into
Conversation
Reuse output normalization for persisted guardrail diagnostics and answers while retaining best-effort fallback when custom serialization fails. Closes openai#5006
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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.
Summary
When serializing
RunStateto JSON, guardrail results (GuardrailFunctionOutput.output_infoandOutputGuardrailResult.agent_output) were passed directly to_ensure_json_compatible(), which relies onjson.dumps(..., default=str)for non-JSON native values. Consequently, structured Pydantic models and dataclasses degraded into opaque Python repr strings likeallowed=True reason='approved'. Upon restoring viaRunState.from_json(), these values remained raw strings instead of structured dictionaries.This PR reuses the structured conversion pipeline (
_serialize_output_value) used by tool outputs, converting models and dataclasses into plain JSON data while retaining a safe fallback if custom serialization raises.Closes #5006
Changes
src/agents/run_state.py:_serialize_guardrail_payload(value: Any) -> Anyto normalize structured output and diagnostic values using_serialize_output_valuebefore passing to_ensure_json_compatible._serialize_guardrail_resultsand_serialize_tool_guardrail_resultsto use_serialize_guardrail_payloadforoutput_infoandagent_output.tests/test_run_state.py:test_runner_guardrail_models_survive_state_serializationasserting Pydantic models in input/output guardrails and structured agent output round-trip as JSON dictionaries.test_tool_guardrail_dataclasses_survive_state_serializationverifying dataclasses in tool guardrail diagnostics round-trip.test_guardrail_state_keeps_fallback_when_model_serializer_raisesverifying best-effort string fallback when a model serializer raises.Verification
tests/test_run_state.py.ScriptedModelagainst the reproduction in RunState stringifies structured guardrail diagnostics and agent output during persistence #5006.ruff.