Skip to content

fix(guardrails): strip stray guardrail field when parsing LLM JSON#2880

Open
obchain wants to merge 1 commit into
ag2ai:mainfrom
obchain:fix/issue-2334
Open

fix(guardrails): strip stray guardrail field when parsing LLM JSON#2880
obchain wants to merge 1 commit into
ag2ai:mainfrom
obchain:fix/issue-2334

Conversation

@obchain
Copy link
Copy Markdown
Contributor

@obchain obchain commented May 18, 2026

Summary

  • GuardrailResult.parse was passing both **data (parsed LLM JSON) and an explicit guardrail=... kwarg to the Pydantic model. When the LLM echoes a guardrail key in its JSON output, this produced a double-kwarg / Pydantic validation error and broke the guardrail flow.
  • Pop any incoming guardrail field before constructing the model so the runtime instance is the only source of truth.
  • Adds a unit test that parses a JSON payload with a stray guardrail field and verifies the runtime guardrail wins.

Fixes #2334

Test plan

  • uv run --extra test pytest test/agentchat/group/test_guardrails.py::TestGuardrailResult — 8 passed

…uardrailResult

`GuardrailResult.parse` deserialises an LLM-produced JSON payload and then
constructs the model with the runtime ``guardrail`` instance via
``GuardrailResult(**data, guardrail=guardrail)``. When the LLM echoes back a
``guardrail`` field (which is an ``arbitrary_types_allowed`` python reference
that should never round-trip through JSON), the resulting double-kwarg crashes
with ``TypeError`` / Pydantic validation error and the agent loop fails.

Strip any incoming ``guardrail`` key from the parsed dict before building the
model so the runtime instance is the only source of truth.

Fixes ag2ai#2334
@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
autogen/agentchat/group/guardrails.py 88.73% <100.00%> (+0.16%) ⬆️

... and 70 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

[Bug]: Pydantic Parsing error with GuardrailResult

1 participant