litellm/litellm/litellm_core_utils
Mateo Wang c734ee772f
fix(bedrock/converse): drop toolSpec.strict for Opus 4.7/4.8 (#31582) (#31923)
* fix(bedrock/converse): drop toolSpec.strict for Opus 4.7/4.8

Bedrock Converse routes Claude Opus 4.7/4.8 through an Anthropic-compatible
validator that maps toolSpec to the native tool shape and rejects the extra
`strict` key with `tools.N.custom.strict: Extra inputs are not permitted`,
even though Anthropic's native API accepts `strict` as a top-level tool field
for the same models. Sonnet 4.5/4.6 and Opus <=4.6 accept `toolSpec.strict`
unchanged.

The existing gate `get_bedrock_base_model(model).startswith("anthropic")`
(introduced in #29814 to forward `strict` for Claude on Bedrock Converse) is
too broad and regressed Opus 4.7/4.8 callers — see #31582.

Replace the inline check with a small `bedrock_converse_supports_strict_tools`
helper that excludes the Opus 4.7/4.8 family from strict forwarding. All
other Anthropic models on Bedrock keep the existing behavior.

Closes #31582.

* fix(bedrock/converse): move strict-tools regression to a clean test file

The original regression test was added to
test_litellm_core_utils_prompt_templates_factory.py, which has
pre-existing ruff-format violations throughout (multi-line asserts that
fit on one line). The lint workflow runs `ruff format --check` on
changed files only, so touching that file surfaces those pre-existing
violations and fails CI for unrelated reasons.

Move the #31582 regression coverage into a new dedicated test file so
the format check stays green. Also collapses the helper's `not any(...)`
onto a single line to satisfy ruff format.

Covers: #31582

* refactor(bedrock/converse): drive strict-tools gate from model cost map

Replace the hardcoded Opus 4.7/4.8 pattern list with a
bedrock_converse_supports_strict_tools flag on the affected entries in
model_prices_and_context_window.json, resolved via get_model_info with a
local cost map fallback, so future models with the same restriction only
need a JSON update

* chore: revert unrelated credential_migration.py reformat

---------

Co-authored-by: ly-wang19 <ly-wang19@users.noreply.github.com>
(cherry picked from commit 85f924148a)
2026-07-03 16:43:50 -07:00
..
audio_utils style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
llm_cost_calc style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
llm_response_utils style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
prompt_templates fix(bedrock/converse): drop toolSpec.strict for Opus 4.7/4.8 (#31582) (#31923) 2026-07-03 16:43:50 -07:00
specialty_caches style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
tokenizers Code Quality Improvement - remove tokenizers/ from /llms (#7163) 2024-12-10 23:50:15 -08:00
api_route_to_call_types.py Agent Guardrails - on streaming output (#21206) 2026-02-14 11:36:52 -08:00
app_crypto.py style: run black formatter on entire codebase 2026-03-11 17:07:57 -03:00
asyncify.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
cached_imports.py style: run black formatter on entire codebase 2026-03-11 17:07:57 -03:00
chat_completion_agentic_loop.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
cli_token_utils.py feat(cli): per-agent lite claude / codex / opencode commands that wrap coding agents through the proxy (#29850) 2026-06-10 13:52:26 -07:00
cloud_storage_security.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
completion_timeout.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
core_helpers.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
coroutine_checker.py style: run black formatter on entire codebase 2026-03-11 17:07:57 -03:00
credential_accessor.py fix(router.py): support reusable credentials via passthrough router (#9758) 2025-04-04 18:40:14 -07:00
custom_logger_registry.py feat: litellm oss 110626 (#30202) 2026-06-11 22:30:26 -07:00
dd_tracing.py _add_dd_apm_tags_for_litellm_call_id (#22219) 2026-02-26 16:42:23 -08:00
default_encoding.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
dot_notation_indexing.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
duration_parser.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
env_utils.py fix(callbacks): allow MAX_CALLBACKS override via env var (#20781) 2026-02-09 12:11:32 -08:00
exception_mapping_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
fallback_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
get_blog_posts.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
get_litellm_params.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
get_llm_provider_logic.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
get_model_cost_map.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
get_provider_specific_headers.py [Fix] Forward anthropic-beta headers to Bedrock, VertexAI (#15700) 2025-10-18 16:26:32 -07:00
get_supported_openai_params.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
health_check_helpers.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
health_check_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
initialize_dynamic_callback_params.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
json_validation_rule.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
litellm_logging.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
llm_request_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
logging_callback_manager.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
logging_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
logging_worker.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
mock_functions.py Support returning virtual key in custom auth + Handle provider-specific optional params for embedding calls (#11346) 2025-06-03 07:24:13 -07:00
model_param_helper.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
model_response_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
README.md Guardrails API - add streaming support (#17400) 2025-12-02 22:52:09 -08:00
realtime_streaming.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
reasoning_effort_utils.py fix(bedrock): normalize Messages system role and adaptive-thinking for Claude Invoke (#31364) 2026-06-27 11:35:36 -07:00
redact_messages.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
request_timeout_resolver.py fix(router): honor litellm_settings.request_timeout as an independent per-attempt timeout (#31119) 2026-06-23 14:22:54 -07:00
response_header_helpers.py fix(utils.py): guarantee openai-compatible headers always exist in response 2024-09-28 21:08:15 -07:00
rules.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
safe_json_dumps.py perf(spend-logs): only strip NUL bytes in safe_dumps when present (#31424) 2026-06-26 09:37:19 -07:00
safe_json_loads.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
secret_redaction.py Litellm oss staging (#29492) 2026-06-02 08:48:10 -07:00
sensitive_data_masker.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
streaming_chunk_builder_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
streaming_handler.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
thread_pool_executor.py (Fixes) OpenAI Streaming Token Counting + Fixes usage track when litellm.turn_off_message_logging=True (#8156) 2025-01-31 15:06:37 -08:00
token_counter.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
url_utils.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00

Folder Contents

This folder contains general-purpose utilities that are used in multiple places in the codebase.

Core files:

  • streaming_handler.py: The core streaming logic + streaming related helper utils
  • core_helpers.py: code used in types/ - e.g. map_finish_reason.
  • exception_mapping_utils.py: utils for mapping exceptions to openai-compatible error types.
  • default_encoding.py: code for loading the default encoding (tiktoken)
  • get_llm_provider_logic.py: code for inferring the LLM provider from a given model name.
  • duration_parser.py: code for parsing durations - e.g. "1d", "1mo", "10s"
  • api_route_to_call_types.py: mapping of API routes to their corresponding CallTypes (e.g., /chat/completions -> [acompletion, completion])