feat(multiagent): add structured_output_model support to Graph #1159
+254
−16
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.
Add Structured Output Support to Graph Multi-Agent Pattern
Description
This PR adds support for
structured_output_modelparameter to the Graph multi-agent pattern, enabling structured output from all agent nodes in a graph. This addresses the feature request in #538.The implementation allows users to:
structured_output_modelat the graph level when invoking the graphstructured_output_modelduring agent initializationChanges
Core Implementation
Added
structured_output_modelparameter to:Graph.__call__()Graph.invoke_async()Graph.stream_async()Execution chain updates:
_execute_graph()- accepts and passesstructured_output_model_execute_nodes_parallel()- propagates to parallel node execution_stream_node_to_queue()- forwards to individual nodes_execute_node()- uses agent's default or graph-level modelPer-agent support:
_default_structured_output_modelsetCode Changes
File:
src/strands/multiagent/graph.pyType[BaseModel]import frompydanticstructured_output_modelparameterstructured_output_modelthrough all layersFile:
tests/strands/multiagent/test_graph.pyTestGraphStructuredOutputtest class with 3 comprehensive tests:test_graph_passes_structured_output_model_to_agents- verifies parameter passingtest_graph_structured_output_sync- tests synchronous invocationtest_graph_structured_output_multiple_nodes- tests multiple nodesUsage Examples
Example 1: Graph-Level Structured Output
Example 2: Per-Agent Structured Output
Example 3: Mixed Approach (Agent Default + Graph Override)
Testing
Test Coverage
Added comprehensive test suite in
TestGraphStructuredOutput:test_graph_passes_structured_output_model_to_agentsstructured_output_modelis correctly passed from Graph to Agenttest_graph_structured_output_sync__call__()methodtest_graph_structured_output_multiple_nodesstructured_output_modelTest Results
Manual Testing
Implementation Details
Priority Order
When determining which
structured_output_modelto use:agent._default_structured_output_model) - highest prioritygraph(..., structured_output_model=Model)) - fallbackThis allows maximum flexibility:
Backward Compatibility
✅ Fully backward compatible
structured_output_modelis an optional parameterNested Multi-Agent Support
Works seamlessly with nested multi-agent patterns:
structured_output_modelBenefits
Related Issues
Fixes #538
Checklist
hatch fmt --formatter)hatch fmt --linter)Additional Notes