ReqLLM.Providers.Anthropic.AdapterHelpers (ReqLLM v1.16.0)

View Source

Shared helper functions for Anthropic model adapters (Bedrock, Vertex).

These functions are NOT used by the native Anthropic provider - they are specific to adapters that wrap Anthropic's API in other platforms.

Summary

Functions

Apply the object-level requirements strict decoding mandates: every property is marked required and additionalProperties is set to false.

Extract structured output from tool calls in response.

Extract stub tool definitions from messages when tools are needed but none provided.

Conditionally add a parameter to a map if the value is not nil.

Add extended thinking configuration to request body if enabled.

Prepare context and options for :object operations using a synthetic structured_output tool, forcing tool choice to use it.

Reduce a compiled schema to the subset that grammar-constrained (output_config.format) decoding supports: strip unsupported keywords and force every property required with additionalProperties: false.

Recursively remove JSON Schema keywords that strict (grammar-constrained) decoding does not support.

Resolve the anthropic_structured_output_mode from opts (top-level or nested under :provider_options), defaulting to :auto.

Functions

enforce_strict_schema_requirements(schema)

Apply the object-level requirements strict decoding mandates: every property is marked required and additionalProperties is set to false.

extract_and_set_object(response)

Extract structured output from tool calls in response.

Used for :object operations to get the final structured output.

extract_stub_tools_from_messages(body)

@spec extract_stub_tools_from_messages(map()) :: [map()]

Extract stub tool definitions from messages when tools are needed but none provided.

Bedrock and Azure are strict about tool validation in multi-turn conversations. If the conversation history contains tool_use or tool_result blocks, the API requires corresponding tool definitions. This function extracts tool names from the messages and creates minimal stub definitions.

maybe_add_param(body, key, value)

Conditionally add a parameter to a map if the value is not nil.

maybe_add_thinking(body, opts)

Add extended thinking configuration to request body if enabled.

Extended thinking doesn't work when tool_choice forces a specific tool. See: https://docs.claude.com/en/docs/build-with-claude/extended-thinking

prepare_structured_output_context(context, opts)

Prepare context and options for :object operations using a synthetic structured_output tool, forcing tool choice to use it.

This is the best-effort tool-calling path. For grammar-constrained output, callers pass anthropic_structured_output_mode: :json_schema, which the platform adapter (e.g. Google Vertex) handles via the response-level output_config.format path instead of this tool — see structured_output_mode/1 and strict_json_schema/1.

strict_json_schema(compiled_schema)

Reduce a compiled schema to the subset that grammar-constrained (output_config.format) decoding supports: strip unsupported keywords and force every property required with additionalProperties: false.

Array length (minItems/maxItems) cannot be expressed, so callers that need an exact count must re-validate it on the response.

strip_constraints_recursive(schema)

Recursively remove JSON Schema keywords that strict (grammar-constrained) decoding does not support.

Strips numeric/length/array constraints (minimum, maximum, minLength, maxLength, minItems, maxItems) and the Gemini-style propertyOrdering annotation (which ReqLLM.Schema.to_json/1 emits but Anthropic's strict tool rejects) from the schema and every nested properties/items subschema.

structured_output_mode(opts)

Resolve the anthropic_structured_output_mode from opts (top-level or nested under :provider_options), defaulting to :auto.