feat: add schema-constrained structured outputs and recovery loop to Agent - #12990
Open
rautaditya2606 wants to merge 2 commits into
Open
rautaditya2606 wants to merge 2 commits into
rautaditya2606 wants to merge 2 commits into
Conversation
rautaditya2606
requested review from
anakin87
and
a lite review from Copilot
and removed request for
a team
September 27, 2026 20:01
Contributor
|
@rautaditya2606 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
…d mypy type ignores
Contributor
anakin87
requested changes
Oct 2, 2026
anakin87
left a comment
Member
There was a problem hiding this comment.
Thank you!
See #12989 (comment)
No changes are needed from your side.
We'll follow up in the issue/here if we decide to implement this feature.
This branch has not been deployed
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.
Related Issues
Proposed Changes
Adds
response_schemaandmax_schema_retriesparameters toAgent, enabling schema-constrained output validation and automatic self-correction for terminal agent replies:BaseModelsubclass or JSON Schema dictionary without coupling to provider-specific client parameters.ChatMessage.from_user) containing the schema and error description, re-looping up tomax_schema_retriestimes within the agent's overallmax_agent_stepsbudget.Draft202012Validator.check_schema, raisingValueErroron malformed schemas before running.json ...) before validation to prevent unnecessary retries on formatted responses.model_exit_reason == _EXIT_REASON_TEXT, ensuring schema validation never fires on unexecuted tool-call messages when no tools are present."structured_output"inresolved_state_schemaand output sockets (type: response_schema | None) while ensuring it is excluded from input sockets.to_dict,from_dict, andclone.How did you test it?
Added
TestAgentStructuredOutputintest/components/agents/test_agent.pycovering:structured_output=Nonewithout unhandled exceptions.max_agent_stepscutoff stopping recovery.run_asyncparity with syncrun.response_schema=None.to_dict,from_dict, andcloneserde roundtrips.Quality checks run:
hatch run fmt(clean)hatch run test:types haystack/components/agents/agent.py test/components/agents/test_agent.py(clean)hatch -e test run pytest test/components/agents/test_agent.py(138 passed)Notes for the reviewer
The validation and retry logic is completely isolated to
haystack/components/agents/agent.py. Whenresponse_schemais omitted, the execution path and output dictionary remain byte-identical to existing behavior.Note: when
response_schemais a Pydantic model,structured_outputholds a liveBaseModelinstance in agent state. This hasn't been tested against Agent's breakpoint/snapshot resume path — if that machinery serializes arbitrary state values mid-run, a Pydantic instance may not round-trip the same way plain dicts do. Dict-schema mode is unaffected. Flagging as untested rather than claiming full compatibility.Checklist
feat: