Skip to content

Fall back to non-streaming for structured output on Bedrock Converse - #451

Open
cpsievert wants to merge 4 commits into
fix-streaming-structured-outputfrom
bedrock-converse-stream-fallback
Open

cpsievert wants to merge 4 commits into
fix-streaming-structured-outputfrom
bedrock-converse-stream-fallback

Conversation

@cpsievert

@cpsievert cpsievert commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

ChatBedrock() with the default Converse API extracts structured data through a forced tool call (the same strategy as Anthropic's tool mode, reusing AnthropicProvider.create_data_model_tool with a {"data": ...}-wrapped input schema). But Converse streams tool input in its own event type rather than as text deltas, and stream_content() only surfaces delta.text — so:

chat = ChatBedrock()  # api="converse" is the default for most models
chunks = list(chat.stream("...", data_model=Person))
# chunks == []  (silently!)
Person.model_validate_json("".join(chunks))  # ValidationError, far from the cause

No crash like #317, but arguably worse: a silent empty stream that violates the documented "".join(chunks) contract. The final turn does record the ContentJson correctly, which makes the empty stream even more surprising.

Note that streaming the raw tool input wouldn't fix the contract either, since it's {"data": ...}-wrapped — so graceful degradation (as with Anthropic tool mode) is the right answer.

Fix

BedrockConverseProvider.can_stream() returns False when a data_model is present, so Chat transparently downgrades to a single non-streaming request: one chunk containing the complete JSON, plus the fallback warning from #317.

Test plan

  • New sync + async tests in tests/test_provider_bedrock_converse.py mirror the Anthropic downgrade tests from Fix crash when streaming structured data (Anthropic tool mode) #317 (mocked _converse/_converse_async, assert warning, single-chunk JSON round-trip, and ContentJson in history)
  • pytest tests/test_provider_bedrock_converse.py — 107 passed
  • ruff clean on changed files; pyright shows no new errors vs. baseline

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused provider capability override is consistent with the Converse implementation and is covered by sync and async tests.

0 open findings

What changed in this PR

Adds graceful non-streaming fallback for Bedrock Converse structured output.

Changes:

  • Disables streaming when a data model is supplied.
  • Adds synchronous and asynchronous fallback tests.
  • Documents Bedrock Converse behavior.
File Description
chatlas/​_provider_bedrock_converse.py Reports structured output as non-streamable.
tests/​test_provider_bedrock_converse.py Tests fallback, JSON output, warnings, and history.
docs/​get-started/​structured-data.qmd Documents the fallback behavior.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@cpsievert
cpsievert force-pushed the bedrock-converse-stream-fallback branch from 2a74e91 to bff6d30 Compare October 12, 2026 22:44
@cpsievert
cpsievert added this pull request to stack #452 October 12, 2026 22:45
@cpsievert
cpsievert force-pushed the bedrock-converse-stream-fallback branch from bff6d30 to 4be2cfc Compare October 12, 2026 22:56
@cpsievert
cpsievert force-pushed the bedrock-converse-stream-fallback branch from 4be2cfc to 945dfc9 Compare October 12, 2026 23:00
@cpsievert
cpsievert force-pushed the bedrock-converse-stream-fallback branch from 945dfc9 to 323564a Compare October 12, 2026 23:02
@cpsievert
cpsievert requested a balanced review from Copilot October 12, 2026 23:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused provider capability change correctly addresses the empty-stream behavior with adequate sync and async coverage.

0 open findings

🧠 Review effort: Balanced

@cpsievert
cpsievert force-pushed the bedrock-converse-stream-fallback branch from 323564a to 2692b45 Compare October 12, 2026 23:05
Bedrock Converse extracts structured data through a forced tool call
(like Anthropic's tool mode), and Converse streams tool input in its own
event type rather than as text. As a result, streaming with a data_model
silently yielded zero chunks, violating the `"".join(chunks)` contract.

Override `can_stream()` so `Chat` transparently downgrades to a single
non-streaming request (with a warning), yielding the complete JSON as
one chunk.

This branch has not been deployed

No deployments
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.

2 participants