fix(guardrails): strip stray guardrail field when parsing LLM JSON#2880
Open
obchain wants to merge 1 commit into
Open
fix(guardrails): strip stray guardrail field when parsing LLM JSON#2880obchain wants to merge 1 commit into
obchain wants to merge 1 commit into
Conversation
…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 Report✅ All modified and coverable lines are covered by tests.
... and 70 files with indirect coverage changes 🚀 New features to boost your workflow:
|
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
GuardrailResult.parsewas passing both**data(parsed LLM JSON) and an explicitguardrail=...kwarg to the Pydantic model. When the LLM echoes aguardrailkey in its JSON output, this produced a double-kwarg / Pydantic validation error and broke the guardrail flow.guardrailfield before constructing the model so the runtime instance is the only source of truth.guardrailfield and verifies the runtime guardrail wins.Fixes #2334
Test plan
uv run --extra test pytest test/agentchat/group/test_guardrails.py::TestGuardrailResult— 8 passed