-
Notifications
You must be signed in to change notification settings - Fork 963
feat: Implement LangChain structured output #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds dynamic structured output functionality to the MCPAgent by introducing middleware that automatically applies structured output formatting during agent execution. The key improvement is that structured output can now be applied earlier in the agent loop rather than only at the end, allowing the LLM to receive schema-enforced responses natively.
- Introduced a new
dynamic_structured_outputmiddleware that applies structured output formatting during agent execution - Modified the
streammethod to pass the output schema through context to the middleware - Simplified structured output handling by leveraging middleware instead of post-processing
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| libraries/python/mcp_use/agents/mcpagent.py | Added dynamic_structured_output middleware method, updated imports for middleware and structured output utilities, modified stream method to pass schema via context, and simplified structured output handling logic |
| libraries/python/examples/new_structured_output_example.py | Added new example demonstrating structured output with Airbnb MCP server |
Comments suppressed due to low confidence (1)
libraries/python/mcp_use/agents/mcpagent.py:180
- Normal methods should have 'self', rather than 'request', as their first parameter.
async def dynamic_structured_output(request: ModelRequest, handler):
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| self._model_provider, self._model_name = extract_model_info(self.llm) | ||
|
|
||
| @wrap_model_call | ||
| async def dynamic_structured_output(request: ModelRequest, handler): |
Copilot
AI
Nov 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dynamic_structured_output method is missing the self parameter. Instance methods decorated with @wrap_model_call should still include self as the first parameter to properly bind to the class instance.
| async def dynamic_structured_output(request: ModelRequest, handler): | |
| async def dynamic_structured_output(self, request: ModelRequest, handler): |
PR Summary
Overview
response_formatintoMCPAgentwithout recreating the agent per call.Key Changes
mcp_use/agents/mcpagent.pydynamic_structured_outputmiddleware (@wrap_model_call) that inspects pending tool calls and updates theModelRequestwithAutoStrategyonly when the agent is about to return a final response. This avoids constraining intermediate reasoning/tool-selection steps.response_formatthrough the agent'sastreamcontext so LangChain can surface nativestructured_responsepayloads.structured_responsechunks during streaming and, on completion, deliver them directly when present. When the provider did not emit a native payload (e.g., single-shot replies), re-run the final answer throughllm.with_structured_output(schema)to coerce it into the requested schema without re-executing the agent loop.examples/python/new_structured_output_example.pyDesign Decisions & Rationale
response_formatafter tools finish (or when no tools were needed), keeping agent behaviour unchanged while still leveraging LangChain 1.0's structured output pipeline.with_structured_output()is faster and more reliable; we still have_attempt_structured_outputavailable if future providers lack native support.context={"response_format": output_schema}intoastreamensures LangChain's compiled graph can producestructured_responseentries, unlocking the zero-copy fast path when available.Testing Notes
new_structured_output_example.py) to confirm:Follow-ups / Considerations
try/exceptand fall back to_attempt_structured_output(already present) for compatibility.Fixes #397