Skip to content

feat(bedrock): add native structured outputs and strict tool use modes - #2086

Closed
ndgigliotti wants to merge 1 commit into
567-labs:mainfrom
ndgigliotti:feat/bedrock-structured-outputs
Closed

ndgigliotti wants to merge 1 commit into
567-labs:mainfrom
ndgigliotti:feat/bedrock-structured-outputs

Conversation

@ndgigliotti

Copy link
Copy Markdown
Contributor

Describe your changes

Add BEDROCK_STRUCTURED_OUTPUTS and BEDROCK_TOOLS_STRICT modes that leverage Bedrock's native constrained decoding (announced Feb 2026) for guaranteed JSON schema compliance via the Converse API.

What's included:

  • BEDROCK_STRUCTURED_OUTPUTS — uses outputConfig.textFormat with json_schema type for native schema-constrained text generation
  • BEDROCK_TOOLS_STRICT — adds strict: true to toolSpec for native schema enforcement on tool use
  • _set_additional_properties_false() helper to recursively patch Pydantic schemas (Bedrock requires additionalProperties: false on all object types)
  • Handler, reask, and parser functions for both modes
  • Auto-routing in from_provider() — models that support structured outputs default to BEDROCK_STRUCTURED_OUTPUTS instead of BEDROCK_JSON/BEDROCK_TOOLS
  • 22 unit tests covering handlers, reask, parsing, registry, and mode classification

Smoke-tested models (all pass first-try):

  • Anthropic: Claude Haiku 4.5, Sonnet 4.5, Opus 4.5, Opus 4.6
  • Qwen: Qwen3 32B, Qwen3 Coder
  • Google: Gemma 3 12B, 27B
  • MiniMax: M2
  • Mistral: Large 3, Ministral 8B, Voxtral Small
  • Moonshot: Kimi K2 Thinking
  • NVIDIA: Nemotron Nano 9B, 12B

Known broken models (excluded from auto-routing, documented):

  • OpenAI GPT-OSS (all variants) — returns markdown-wrapped JSON, ignores outputConfig
  • Mistral Magistral Small 2509 — returns free-form text, ignores outputConfig
  • DeepSeek V3.2 — API rejects outputConfig.textFormat with ValidationException

Issue ticket number and link

Closes #2084

Checklist before requesting a review

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • If it is a core feature, I have added documentation.

@ndgigliotti
ndgigliotti force-pushed the feat/bedrock-structured-outputs branch from f71c192 to 855d642 Compare February 28, 2026 06:12
@ndgigliotti

Copy link
Copy Markdown
Contributor Author

@jxnl Just rebased onto latest main. Would appreciate a review when you get a chance. Thanks!

@ndgigliotti
ndgigliotti force-pushed the feat/bedrock-structured-outputs branch from 855d642 to ffe4bc2 Compare March 21, 2026 06:08
@ndgigliotti

Copy link
Copy Markdown
Contributor Author

Hey @jxnl, rebased again onto latest main. This has been open for over a month with no review. Is this something you'd want to merge, or should I close it? Happy either way, just want to know if it's worth keeping up to date.

@sanjeevpenupala

Copy link
Copy Markdown

Currently building a production FastAPI backend that wraps Bedrock with Instructor for structured outputs, at my company. Native constrained decoding via outputConfig.textFormat.json_schema would be a significant improvement over the current tool-use approach for schema compliance. We'd adopt this immediately if merged. Happy to help test if that's useful!

@ndgigliotti
ndgigliotti force-pushed the feat/bedrock-structured-outputs branch from ffe4bc2 to a126c08 Compare March 29, 2026 03:27
@fede-mt

fede-mt commented Jun 11, 2026

Copy link
Copy Markdown

Thanks a lot for working on this PR. We rely on Instructor with Amazon Bedrock, and having native structured outputs would make our production integrations much more reliable and simpler to maintain.

We would be very happy to see this merged!

Add BEDROCK_STRUCTURED_OUTPUTS and BEDROCK_TOOLS_STRICT modes that
leverage Bedrock's native constrained decoding for guaranteed schema
compliance, reducing retries and token costs.
@ndgigliotti
ndgigliotti force-pushed the feat/bedrock-structured-outputs branch from a126c08 to e48799c Compare July 3, 2026 05:02
jxnl added a commit that referenced this pull request Aug 3, 2026
## Summary

- accumulate declared, nested, and unknown numeric OpenAI/Anthropic
usage fields across retries while preserving non-numeric metadata
- add corrective feedback when a Responses API retry receives no tool
call
- preserve raw iterable type hints through sync and async v2
parallel-tool wrappers
- strengthen API-key-free coverage for current and future SDK usage
counters

## Consolidated and superseded items

- closes #2493
- consolidates contributor work from #2498, #2500, and #2501 with
original commit authorship preserved
- supersedes #2497 because it drops unknown `model_extra` counters
- supersedes #2499 because its hand-maintained provider field lists
would drift as SDKs evolve

## Validation

- focused changed-surface suite: `110 passed`
- broad offline v2/coverage suite: `2368 passed, 91 skipped, 73
deselected`
- Ruff check and format check: passed
- scoped `ty check`: passed
- `uv lock --check`: passed
- pre-commit hooks and `git diff --check`: passed

The 73 deselected tests require live provider credentials. An unfiltered
local run confirmed its 22 failures were provider network connections in
the restricted environment; GitHub provider jobs remain the
authoritative validation for those paths.

## Intentionally skipped

- provider additions or expansions: #2436, #2435, #2423, #2409, #2384,
#2322, #2306, #2298, #2283, #2168, #2086; issues #2408, #2383, #2365,
#2260, #2084, #2076
- broad architecture, product, security, or streaming decisions: #2394,
#2392, #2357, #2356, #2355, #2351, #2321, #2307, #2287, #2263; issues
#2479, #2403, #2393, #2391, #2316, #2272, #2056
- dependency batch: #2433
- nontrivial examples and editorial/resource additions: #2468, #2405,
#2401, #2354, #2346, #2311, #2305; issue #2404

These remain open because they need dedicated product, architecture,
provider, security, dependency, or editorial review and are not required
for the `1.15.5` patch release.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes retry usage totals and reask message content on failure paths;
scope is limited and heavily covered by tests, with no auth or
data-store changes.
> 
> **Overview**
> Bundles three v2 retry and wrapper fixes for a patch release.
> 
> **Retry usage accounting** replaces hand-maintained token field sums
with generic `_accumulate_models` on Pydantic usage objects. Numeric
fields (including nested models and `model_extra` counters) add across
retries; booleans and other non-numeric metadata are not treated as
billable. OpenAI and Anthropic paths share this logic.
> 
> **OpenAI Responses reask** appends a user correction when
`RESPONSES_TOOLS` validation fails but the output has no tool calls
(e.g. reasoning-only), so retries include feedback instead of repeating
the same request.
> 
> **Parallel tools** in `patch_v2` skips `prepare_response_model` and
does not replace `response_model` with the handler’s prepared wrapper
for parallel modes, keeping raw `Iterable[...]` hints so schemas and
parsed results include every member type.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
bbddca1. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

jxnl commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

The native structured-output portion has been reconstructed against the current v2 provider architecture in draft PR #2515, with explicit Mode.JSON_SCHEMA / Mode.TOOLS_STRICT, recursive strict-schema handling, the verified boto3 floor, current docs, and attribution to @ndgigliotti. The replacement intentionally omits this PR's hard-coded model routing and stale provider-specific aliases. Keeping this PR open until #2515 reaches main; it can then be closed as superseded.

jxnl commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Closing this PR as superseded by #2515. The replacement preserves the native Bedrock structured-output intent on the current v2 architecture, uses explicit JSON Schema and strict-tool modes, removes hard-coded automatic routing, raises the required boto3 floor, and is fully green. Credit for the original contribution is retained in #2515. Issue #2084 remains open until the replacement reaches main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(bedrock): Add support for AWS Bedrock native structured outputs

5 participants