Skip to content

fix(core): preserve structured guardrail diagnostics and agent output during persistence - #5016

Open
rksharma-owg wants to merge 1 commit into
openai:mainfrom
rksharma-owg:fix/runstate-structured-guardrail-results
Open

rksharma-owg wants to merge 1 commit into
openai:mainfrom
rksharma-owg:fix/runstate-structured-guardrail-results

Conversation

@rksharma-owg

Copy link
Copy Markdown
Contributor

Summary

When serializing RunState to JSON, guardrail results (GuardrailFunctionOutput.output_info and OutputGuardrailResult.agent_output) were passed directly to _ensure_json_compatible(), which relies on json.dumps(..., default=str) for non-JSON native values. Consequently, structured Pydantic models and dataclasses degraded into opaque Python repr strings like allowed=True reason='approved'. Upon restoring via RunState.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:
    • Added _serialize_guardrail_payload(value: Any) -> Any to normalize structured output and diagnostic values using _serialize_output_value before passing to _ensure_json_compatible.
    • Updated _serialize_guardrail_results and _serialize_tool_guardrail_results to use _serialize_guardrail_payload for output_info and agent_output.
  • tests/test_run_state.py:
    • Added test_runner_guardrail_models_survive_state_serialization asserting Pydantic models in input/output guardrails and structured agent output round-trip as JSON dictionaries.
    • Added test_tool_guardrail_dataclasses_survive_state_serialization verifying dataclasses in tool guardrail diagnostics round-trip.
    • Added test_guardrail_state_keeps_fallback_when_model_serializer_raises verifying best-effort string fallback when a model serializer raises.

Verification

Reuse output normalization for persisted guardrail diagnostics and answers while retaining best-effort fallback when custom serialization fails.

Closes openai#5006
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-14T12:04:46.015203Z ec4b2fa PR opened
🔒 Security Review Completed 2026-09-14T12:09:33.192634Z ec4b2fa PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.

RunState stringifies structured guardrail diagnostics and agent output during persistence

1 participant