-
Notifications
You must be signed in to change notification settings - Fork 407
feat: Add Structured Output as part of the agent loop #943
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
feat: Implement comprehensive structured output system This feature addition introduces a complete structured output system that allows agents to return validated Pydantic models instead of raw text responses, providing type safety and consistency for AI agent interactions. ## Key Features Added ### Core Structured Output System - **New output module**: Complete structured output architecture with base classes, modes, and utilities - **Agent integration**: Native structured_output_type parameter support in Agent class and __call__ method - **Event loop integration**: Enhanced event loop to handle structured output processing and validation - **Tool-based fallback**: Automatic fallback mechanism using structured output tools when native support unavailable ### Architecture Components - **OutputMode base class**: Abstract interface for different structured output implementations - **ToolMode implementation**: Tool-based structured output mode with caching and retry logic - **OutputSchema resolution**: Centralized schema resolution utility with BASE_KEY constant - **Structured output handler**: Comprehensive handler with logging, caching, and error handling ### Developer Experience - **PydanticAI-style interface**: Familiar API pattern for structured output specification - **Comprehensive documentation**: 400+ line README with examples, use cases, and best practices - **Type safety**: Full typing support with proper generic types and validation - **Streaming compatibility**: Works seamlessly with existing streaming functionality ### Tool Integration - **Structured output tool**: Dedicated tool for handling structured output requests - **Registry integration**: Enhanced tool registry to support structured output tools - **Backward compatibility**: Maintains compatibility with existing tool ecosystem ## Technical Implementation ### Files Added - `src/strands/output/`: Complete output module with base classes, modes, and utilities - `src/strands/tools/structured_output/`: Dedicated structured output tool implementation - `src/strands/types/output.py`: Type definitions for output system - Comprehensive documentation and examples ### Files Modified - Enhanced Agent class with structured_output_type parameter and default schema support - Updated event loop for structured output processing and validation - Improved AgentResult to include structured_output field - Model provider updates for structured output compatibility ### Key Improvements - **Error handling**: Robust error handling with fallback mechanisms - **Performance**: Caching system for improved performance with repeated schema usage - **Logging**: Enhanced logging for debugging and monitoring structured output operations - **Code quality**: Comprehensive formatting, linting, and style improvements ## Usage Examples python # Basic structured output from strands import Agent from pydantic import BaseModel class UserProfile(BaseModel): name: str age: int occupation: str agent = Agent() result = agent("Create a profile for John, 25, dentist", structured_output_type=UserProfile) profile = result.structured_output # Validated UserProfile instance ## Migration Notes - Existing agents continue to work without changes - New structured_output_type parameter is optional - Legacy output modes are deprecated but still functional Resolves: Multiple structured output related issues
Add explicit user message instructing the agent to format previous response as structured output during forced structured output attempts.
src/strands/output/modes.py
Outdated
return True # All our models support function calling | ||
|
||
|
||
class NativeMode(OutputMode): |
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.
Are we planning on implementing any of this? If not then I think we should discard these until we've proved that how they can be implemented; IMHO during implementation we could determine that the signature needs to be changed and if we shipped it, we're stuck with it as-is
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.
Good point. I'll keep the mode
we're using and remove the others (maybe just leave a comment as to what they can be)
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.
I comment this elsewhere, but I think we can just remove mode for now until we decide if/when we want it
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.
I removed all the other modes except for the ToolMode. You think we should get rid of this concept of mode altogether? I think that may make it challenging to move fast and add a Native mode etc. Because we'll have to then introduce this concept. What is/are the downsides and concerns with just having out ToolMode? For example, how does us worse off than having some kind of other implementation that is specific to tools?
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.
Agree with skipping mode for now. I think it helps us move faster by shipping less code for now.
Im a bit concerned that adding mode will be a bit more information than what the customer really needs right now. They want to invoke an agent and get structured output. They can build different modes if they really want to. If we hear from customers that they really want to have mode, we can add that later.
tools: List[ToolSpec] = [tool_spec for tool_spec in all_tools.values()] | ||
return tools | ||
|
||
def register_dynamic_tool(self, tool: AgentTool) -> None: |
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.
I'm not sure we should be adding/removing the tool dynamically - can we simply append the tool_spec inside of the event_loop?
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.
Hmm. I'm not opposed but to better understand, what is the downside? Are we concerned that others will use this method to dynamically register tools? Or is it something else? Wouldn't appending the tool_spec basically be dynamically adding but without a method? (There does seem to be a specific self.dynamic_tools
variable. When is that supposed to be used?
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.
IMHO, it's better to be functional (don't modify state unless you have to) as it's side-effect free. In this case, you always have to remember to unregister even in exceptional cases and while reading the code, you need to remember that something is temporarily added.
Are we even calling unregister_dynamic_tool right now?
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.
I'm open to being wrong about this, but it feels... odd
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.
Hmmm. You're making an interesting point with the register/deregister "mental overhead". I do like how it has more of a 'native' feel when it's part of the toolbox - even though we add it dynamically. Lemme see if there's a better way to add to the tools we provide the model w/o the dynamic register. I can probably just do something like tool_specs = existing tool specs + SO tool spec
or something
from pydantic import BaseModel, Field | ||
|
||
from strands.tools.structured_output import convert_pydantic_to_tool_spec | ||
from strands.tools.structured_output.structured_output_utils import convert_pydantic_to_tool_spec |
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.
I think this move would be a breaking change no?
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.
hmmm. I't really an internal helper tool for the sdk (even though it's not prefixed with _
). are concerned customers are using and will continue using the old import?
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.
In the past, anything that doesn't have _ we've maintained backwards compatibility for
Thus why everything should be private unless we have a reason to make it public
I think we'd block the new structured_output change (#919) on whether or not someone is using kwargs vs invocation_state. That is, if you're using structured_output and you're trying to pass additional features, then you must be using agent.invoke_async(output_model=Person, additional_arg=some_value) # does not use structured_output
agent.invoke_async(output_model=Person, invocation_state={"additional_arg": some_value}) # uses structured_output |
src/strands/agent/agent.py
Outdated
|
||
def __call__(self, prompt: AgentInput = None, **kwargs: Any) -> AgentResult: | ||
def __call__( | ||
self, prompt: AgentInput = None, structured_output_type: Optional[Type[BaseModel]] = None, **kwargs: Any |
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.
Chatted offline about this, but lets add support for invocation_state first before adding this new field: #919
Also, we need to include a *
to ensure backwards compatibility
self, prompt: AgentInput = None, structured_output_type: Optional[Type[BaseModel]] = None, **kwargs: Any | |
self, prompt: AgentInput = None, *, structured_output_type:Type[BaseModel] | None = None, **kwargs: Any |
src/strands/agent/agent.py
Outdated
|
||
async def invoke_async(self, prompt: AgentInput = None, **kwargs: Any) -> AgentResult: | ||
async def invoke_async( | ||
self, prompt: AgentInput = None, structured_output_type: Optional[Type[BaseModel]] = None, **kwargs: Any |
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.
self, prompt: AgentInput = None, structured_output_type: Optional[Type[BaseModel]] = None, **kwargs: Any | |
self, prompt: AgentInput = None, *, structured_output_type:Type[BaseModel] | None = None, **kwargs: Any |
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.
lets try not to name files or directories utils
. Can we come up with a more specific name for what this does?
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.
gotcha. what's the aversion to naming it utils
is it because it's too generic? Because in this case I'm kinda using the path to imply it's utility output.utils
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.
utils end up being dumping grounds for unrelated functionality, so it's effectively banned; it usually makes sense in a better name module/file. In this case, I think this could live in OutputSchema
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.
makes sense!
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.
nit: dont name directories or files utils
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.
Can we have a more concise readme here? I dont think this needs to be so verbose
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.
Yes! Once I add tests i will reduce the readme scope
|
||
# Store in invocation state with namespaced key | ||
key = f"{BASE_KEY}_{tool_use_id}" | ||
invocation_state[key] = validated_object |
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.
We should not be using invocation_state
as part of the sdk. This is customer information that we should not touch.
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.
updated!
Replace StructuredOutputHandler with StructuredOutputContext to provide better encapsulation and cleaner separation of concerns. This change: - Introduces StructuredOutputContext to manage structured output state - Updates Agent and event loop to use the new context-based approach - Modifies tool executors to work with the context pattern - Removes the handler-based implementation in favor of context
- Replace mode.get_tool_specs() calls with cached tool_specs property - Improve code formatting and add trailing commas
Rename parameter throughout codebase for better clarity. This change improves API consistency and makes the parameter's purpose more explicit.
Simplify output mode options by removing unused NativeMode and PromptMode implementations, keeping only ToolMode for structured output. This reduces complexity while maintaining full functionality through the tool-based approach.
src/strands/agent/agent_result.py
Outdated
message: Message | ||
metrics: EventLoopMetrics | ||
state: Any | ||
structured_output: Optional[BaseModel] = None |
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.
In the future we plan on allowing for more types than BaseModel
however I think it's best to set the type then. It probably won't be any
but more like a very large set of types that we would extract out to StructuredOutputType
src/strands/event_loop/event_loop.py
Outdated
attributes = {"event_loop_cycle_id": str(invocation_state.get("event_loop_cycle_id"))} | ||
cycle_start_time, cycle_trace = agent.event_loop_metrics.start_cycle(attributes=attributes) | ||
invocation_state["event_loop_cycle_trace"] = cycle_trace | ||
output_schema: OutputSchema = invocation_state.get("output_schema") |
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.
updated!
src/strands/event_loop/event_loop.py
Outdated
) | ||
agent.messages.append({ | ||
"role": "user", | ||
"content": [{"text": "You must format the previous response as structured output."}] |
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.
Good point. hmmm
src/strands/event_loop/event_loop.py
Outdated
forced_invocation_state["tool_choice"] = {"any": {}} | ||
forced_invocation_state["_structured_output_only"] = True | ||
|
||
events = recurse_event_loop(agent=agent, invocation_state=forced_invocation_state) |
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.
(this code has been slightly updated but I think you're question is beyond the updates).
Are you asking why recurse and not call the model.structured_output
? It's because we our using the Tool based approach and if the model didn't call it on it's own, we will pass in only the StructuredOutputTool and then recurse the event loop so the model calls it on it's own.
cycle_trace: Trace, | ||
cycle_span: Any, | ||
invocation_state: dict[str, Any], | ||
**kwargs: Any, |
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.
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.
Make it explicit; never use kwargs for silently passing things; it's a PITA to understand and trace down (thus our stricter rules about it 😄 )
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.
This is also private and thus not necessary to maintain backwards compatibility
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.
gotcha!
|
||
def __call__(self, prompt: AgentInput = None, **kwargs: Any) -> AgentResult: | ||
def __call__( | ||
self, prompt: AgentInput = None, structured_output_model: Optional[Type[BaseModel]] = None, **kwargs: Any |
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.
nit: Switch to union types Type[BaseModel] | None
- list[ContentBlock]: Multi-modal content blocks | ||
- list[Message]: Complete messages with roles | ||
- None: Use existing conversation history | ||
structured_output_model: Pydantic model type(s) for structured output (overrides agent default). |
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.
Is there a way to pass None
if you don't want structured output? Should that be an option?
@deprecated( | ||
"Agent.structured_output method is deprecated." | ||
" You should pass in `structured_output_model` directly into the agent invocation." | ||
" see the <LINK> for more details" |
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.
TODO - update LINK
from strands.types.tools import ToolSpec | ||
|
||
|
||
class ToolMode(OutputMode): |
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.
Along the lines of YAGNI, If this is the only mode, I'd ditch it for now;
I think all of this could be inlined into Structured_output_context for now
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.
utils end up being dumping grounds for unrelated functionality, so it's effectively banned; it usually makes sense in a better name module/file. In this case, I think this could live in OutputSchema
src/strands/output/modes.py
Outdated
return True # All our models support function calling | ||
|
||
|
||
class NativeMode(OutputMode): |
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.
I comment this elsewhere, but I think we can just remove mode for now until we decide if/when we want it
# Force structured output tool call if LLM didn't use it automatically | ||
if structured_output_context and structured_output_context.output_schema and stop_reason == "end_turn": | ||
if not structured_output_context.can_retry(): | ||
logger.warning( |
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.
I think this should be a throw case? Let's throw for now and we can lighten later if we decide that's not desirable.
But for now I'd say "If we said it has to be structured_output and it ends up not being structured_output, we should make the user aware of that"
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.
hmmm. I can hear that. I think this comes back to what you mentioned to me earlier how we can consider a structured_output_config
in the future and something like this can be configured. Although, I would push back a bit on raising an error to be the default as users may desire structured output but settle on the text output when it's not feasible. I would like to reason through some more use cases as for now I can hear both sides.
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.
Can we raise an exception that captures that additional information. If our structured output feature did not produce structured output, that feels like an error. If someone wants to do something with that, they can catch the error and then handle it. An exception feels more explicit.
if structured_output_context and structured_output_context.output_schema: | ||
if structured_output_result := structured_output_context.extract_result(tool_uses): | ||
yield StructuredOutputEvent(structured_output=structured_output_result) | ||
invocation_state["request_state"]["stop_event_loop"] = True |
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.
My gut says that we should figure out how to do this without using stop_event_loop; is that possible?
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.
If we do do this, then can StructuredOutputTool do this instead?
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.
My gut says that we should figure out how to do this without using stop_event_loop; is that possible?
Yes! I actually had an initial way to do it w/o this but I found this to be cleaner because we need "less" code. With that said here is my alternate way:
We simply set a field in the structured_output_context
to stop - such as structured_output_context.end_loop
.
Then we just add that check like so (updated or condition) here:
if (
invocation_state["request_state"].get("stop_event_loop", False)
or
structured_output_context.end_loop
):
agent.event_loop_metrics.end_cycle(cycle_start_time, cycle_trace)
yield EventLoopStopEvent(
stop_reason, message, agent.event_loop_metrics, invocation_state["request_state"], structured_output_result
)
return
- Tracking expected tool names from output schemas | ||
- Managing validated result storage | ||
- Extracting structured output results from tool executions | ||
- Managing retry attempts for structured output forcing |
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.
In what case does retrying take effect and when it's useful? If it's a tool-call, what can cause the LLM to fail the tool call?
tools: List[ToolSpec] = [tool_spec for tool_spec in all_tools.values()] | ||
return tools | ||
|
||
def register_dynamic_tool(self, tool: AgentTool) -> None: |
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.
I'm open to being wrong about this, but it feels... odd
Description
This PR implements a comprehensive structured output system that allows agents to return validated Pydantic models. Strands developers can pass in the
structured_output_model
field, set to a Pydantic model, when initializing an agent or when invoking the agent. The agent will attempt to populate the pydantic object and set it to a field,structured_output
, that can be accessed from theAgentResult
object. Callers can use different pydantic models per invocation, or the same, or for some invocations usestructured_output_model
and for others ignore it.Examples
Structured Output on the agent invocation
Structured Output when initializing the agent
See the README.md for more examples.
Key Features:
•
structured_output_model
parameter support in Agent class and call method• Complete output module with base classes, modes, and utilities (src/strands/output/)
• Tool-based system with automatic retry logic
• Enhanced event loop integration for structured output processing and validation
• Comprehensive documentation with examples, use cases, and best practices
• Type safety with full typing support and Pydantic validation
• Backward compatibility with existing tool ecosystem
ℹ️ NOTE: ℹ️
API-Bar raising
structured_output_model
is the parameter name we agreed toStructuredOutputEvent
we added a new Typed Event calledStructuredOutputEvent
Open questions:
gettext
but that would be hard to scaleRelated Issues
Documentation PR
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
• [ ] I ran hatch run prepare
Checklist
• [ ] I have read the CONTRIBUTING document
• [ ] I have added any necessary tests that prove my fix is effective or my feature works
• [ ] I have updated the documentation accordingly
• [ ] I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
• [ ] My changes generate no new warnings
• [ ] Any dependent changes have been merged and published
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.