feat(bedrock): add native structured output support#2093
Open
layertwo wants to merge 1 commit intostrands-agents:mainfrom
Open
feat(bedrock): add native structured output support#2093layertwo wants to merge 1 commit intostrands-agents:mainfrom
layertwo wants to merge 1 commit intostrands-agents:mainfrom
Conversation
e355fa8 to
d4381f6
Compare
…textFormat
Add opt-in native structured output mode for BedrockModel that uses
Bedrock's outputConfig.textFormat API for schema-constrained responses,
replacing the tool-based workaround when enabled.
Model-level:
- Add `structured_output_mode` config ("tool" | "native", defaults to "tool")
- Add `convert_pydantic_to_json_schema()` utility with recursive
`additionalProperties: false` injection
- Thread `output_config` through stream() -> _stream() -> _format_request()
- Native mode parses JSON text response instead of extracting tool use args
Agent-level:
- StructuredOutputContext gains native_mode: skips tool registration,
stores output_config, and extracts results from text responses
- event_loop_cycle handles end_turn as success in native mode
- stream_messages passes output_config through to model.stream()
Closes strands-agents#1652
d4381f6 to
23bae41
Compare
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.
Description
Bedrock now supports native structured output for supported models, enforcing JSON schema compliance at the token generation level via
outputConfig.textFormat. Currently, Strands uses a tool-based workaround forstructured_output()- converting the Pydantic model into a fake tool spec and forcing tool use. This is indirect and can lead to extra round-trips when the model doesn't format correctly.Native structured output eliminates the workaround by letting Bedrock guarantee schema-compliant JSON responses directly.
Public API Changes
New
structured_output_modeconfig option onBedrockModel:New public utility
convert_pydantic_to_json_schema()exported fromstrands.tools.structured_output— converts a Pydantic model to a JSON schema dict withadditionalProperties: falseinjected recursively, suitable for Bedrock'soutputConfig.textFormat.structure.jsonSchema.No breaking changes. Default behavior (
structured_output_mode="tool") is unchanged.Use Cases
Related Issues
Resolves #1652
Documentation PR
strands-agents/docs#749
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
convert_pydantic_to_json_schema()covering basic, nested, optional, and list-of-objects schemas_format_request()withoutput_configstructured_output()in native mode (mocked Bedrock responses)hatch run preparepasses across Python 3.10-3.14hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.