Skip to content

[FEATURE] Structured Output Revamp #891

@afarntrog

Description

@afarntrog

Problem Statement

The existing Strucuted output feature in strands sdk has lead to user confusion. Users expect to get a similar behavior out of structured output as they get from invoking an agent. Structured output today does not operate agentically (no tool use), and provides poor ways to inspect metrics or streamed events.

Proposed Solution

from pydantic import BaseModel
from strands import Agent

class WeatherInCity(BaseModel):
    city: str
    weather: str

agent = Agent(tools=[http_request])
result = agent("Whats the weather in paris?", output_model=WeatherInCity)

# Contains the structured output of the model
result.structured_output
  • Structured output result will be added to the agent messages as a tool result
  • If the model does not use the output_model tool during execution, and instead returns a "stop_reason", an error will be raised
  • If the model tries to use the structured_output model, but fails for a validation exception, this is returned to the model to retry

Use Case

  • Tools attached to an agent can be used while generating structured output
  • Metrics are included when using structured output (as of now it does not show)
  • Events from the LLM are accessible to the user during structured output streaming

Alternatives Solutions

  • Extend the existing agent.structured_output function to exhibit this behavior

Additional Context

N/A

Sub-issues

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions