Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `ChatBedrock()` with the default Converse API previously yielded zero chunks from `.stream()`/`.stream_async()` with `data_model` (its structured output is extracted through a tool call, which can't stream). It now gets the same non-streaming fallback as `ChatAnthropic(structured_output_mode="tool")`: a warning, then the complete JSON as a single chunk. (#451)
* `chat_structured()` and `chat_structured_async()` no longer advertise registered tools to the model (with a warning), unless the conversation history contains tool use and the provider requires tool specs for such requests (e.g. Bedrock Converse, which rejects them otherwise). Extraction never executes tools, so previously the model could answer with an ordinary tool call, failing extraction with `Data extraction failed: 0 data results received.` Multi-step extraction (tools, then JSON) remains available via `.stream()`/`.stream_async()` with a `data_model`, which do run the tool loop. (#463)
* `ChatBedrock()` with the default Converse API now uses Bedrock's native structured outputs API (`outputConfig`) for models that support it (currently Anthropic Claude models), restoring true incremental streaming from `.stream()`/`.stream_async()` with `data_model` for those models. Since Converse's support varies by model in ways that can't be derived from the model ID, the first structured request to a model doubles as a non-streaming probe: a model that accepts `outputConfig` streams natively from then on, while a rejection triggers a one-time warning, a transparent retry with the previous forced-tool-call behavior, and tool mode for subsequent requests. Pass the new `structured_output_mode="tool"` to force the tool-call behavior, or `"native"` to force `outputConfig` (which surfaces the model's error instead of falling back). (#463)
* `ChatBedrock(api="messages")` now supports `structured_output_mode` with the same semantics as the Converse path. Mantle's `output_config` support also varies by model in ways that can't be derived from the model ID (and differs from both the Anthropic API and Converse), so `"auto"` uses the same probe behavior: the first structured request doubles as a non-streaming probe, a rejection triggers a one-time warning and a transparent retry with the forced tool call, and the verdict is remembered. Previously, the default `"auto"` mode sent `output_config` to every Claude 4.5+ model, which mantle rejects for some (e.g. `anthropic.claude-haiku-4-5`) with a 400 error. (#465)
* Structured-output requests to most OpenAI-compatible providers no longer include OpenAI's `strict` flag, which causes some backends to reject the request. Requests to OpenAI, `ChatGroq()`, `ChatAzureOpenAICompletions()`, and `ChatPosit()` models with an `openai/` prefix are unchanged. If structured output becomes less reliable for your provider (for example, OpenAI models behind a proxy), pass the new `strict=True` argument to `ChatOpenAICompletions()`, `ChatOpenRouter()`, `ChatPortkey()`, or `ChatHuggingFace()`.
* `Chat.get_tokens()` no longer errors when the conversation ends with a user turn that has no completed assistant response yet (e.g. after a failed request or `add_turn()`), when assistant turns don't strictly alternate with user turns, or when the history holds a single user turn (which now returns an empty list). Token accounting is now driven off complete assistant turns instead of assuming strict user/assistant alternation.
* `ChatBedrockAnthropic(reasoning=)` now accepts an effort level (`"low"`, `"medium"`, `"high"`, `"xhigh"` or `"max"`) to enable adaptive thinking, as `ChatAnthropic()` already did. It previously only took a token budget, which its default model (Claude Sonnet 5) rejects, so there was no way to turn on thinking with that model.
Expand Down
26 changes: 23 additions & 3 deletions chatlas/_provider_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,28 @@ def can_stream(self, data_model: Optional[type[BaseModel]]) -> bool:
return True
return self._use_native_structured_output()

def _use_native_structured_output(self) -> bool:
def _structured_output_model(
self, kwargs: Optional["SubmitInputArgs"]
) -> Optional[str]:
"""The model whose capabilities decide the structured-output strategy.

`None` (the default) keys the strategy on the provider's configured
model, which is also what `can_stream()` sees (request kwargs aren't
available to that hook), keeping the two consistent. Providers whose
native support is tracked per serving model (Bedrock's mantle
endpoint) override this to honor a per-request `model` override,
paired with a loud stream-time guard for the case `can_stream()`
can't see.
"""
return None

def _use_native_structured_output(self, model: Optional[str] = None) -> bool:
# `model` is the effective request model, which request kwargs may
# have overridden; verdicts and support checks should reflect the
# model that will actually serve the request.
mode = self._structured_output_mode
return mode == "native" or (
mode == "auto" and supports_structured_outputs(self.model)
mode == "auto" and supports_structured_outputs(model or self.model)
)

def _chat_perform_args(
Expand All @@ -536,7 +554,9 @@ def _chat_perform_args(
) -> "SubmitInputArgs":
tool_schemas = [self._anthropic_tool_schema(tool) for tool in tools.values()]

use_native = self._use_native_structured_output()
use_native = self._use_native_structured_output(
self._structured_output_model(kwargs)
)

if data_model is not None and use_native:
from anthropic import transform_schema
Expand Down
Loading
Loading