Skip to content

fix(bedrock): scope tool name mappings to request context and honor TTL (#54172) - #54310

Open
MilindChatrabhuji wants to merge 3 commits into
BerriAI:mainfrom
MilindChatrabhuji:fix/bedrock-converse-tool-name-cache-54172
Open

MilindChatrabhuji wants to merge 3 commits into
BerriAI:mainfrom
MilindChatrabhuji:fix/bedrock-converse-tool-name-cache-54172

Conversation

@MilindChatrabhuji

Copy link
Copy Markdown

Overview

Fixes #54172

On the Bedrock Converse route, tool names in model responses were being rewritten using a process-global cache (litellm.bedrock_tool_name_mappings) that is shared across all requests and API keys on the proxy.

  1. make_valid_bedrock_tool_name replaces invalid characters (such as :) with _ and stores sanitized -> original in litellm.bedrock_tool_name_mappings.
  2. get_bedrock_tool_name (llms/bedrock/common_utils.py) checked if response_tool_name in litellm.bedrock_tool_name_mappings.cache_dict:, reading cache_dict directly and completely bypassing TTL checks on read.
  3. If any request contained a tool name X whose sanitized form equaled a valid tool name Y used by another client, every subsequent Converse response that called Y was returned to all clients as X (causing client validation failures such as Claude Code's No such tool available).

Solution

  1. Request Context Scoping:
    • Introduced bedrock_tool_name_mappings_context: ContextVar[dict[str, str] | None] = ContextVar("bedrock_tool_name_mappings_context", default=None) in litellm/llms/bedrock/common_utils.py.
    • Exposed bedrock_tool_name_mappings_context in _lazy_imports_registry.py and litellm/__init__.py.
    • In make_valid_bedrock_tool_name: When a tool name is sanitized (input_tool_name != valid_string), stores valid_string -> input_tool_name in the active request context bedrock_tool_name_mappings_context.get(). Also retains global cache entry for backwards compatibility.
  2. TTL Enforcement & Safe Fallback:
    • In get_bedrock_tool_name(response_tool_name: str) -> str:
      • Checks request context first (bedrock_tool_name_mappings_context.get()), preventing cross-tenant and cross-request tool corruption.
      • When falling back to global cache, invokes litellm.bedrock_tool_name_mappings.get_cache(key=response_tool_name) to strictly honor TTL and purge expired entries, rather than reading .cache_dict directly.
      • Returns response_tool_name unchanged if not mapped or if the entry has expired.

Testing

Added unit tests covering:

  • test_get_bedrock_tool_name_honors_ttl: Confirms expired cache entries are never returned.
  • test_get_bedrock_tool_name_context_isolation: Confirms request contexts isolate tool mappings across concurrent contexts without leaking.
  • test_get_bedrock_tool_name_global_cache_fallback: Confirms valid global cache fallback and context override precedence.
  • test_make_valid_bedrock_tool_name_request_context_and_cache: Confirms make_valid_bedrock_tool_name writes to both request context and global cache.

@veria-ai

veria-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

Reviews

Comment thread litellm/llms/bedrock/common_utils.py Resolved
@MilindChatrabhuji
MilindChatrabhuji force-pushed the fix/bedrock-converse-tool-name-cache-54172 branch from 11f13dc to 4e86cb1 Compare November 27, 2026 11:55
@MilindChatrabhuji

Copy link
Copy Markdown
Author

@kimnamu Thank you for catching that! You were right: _transform_request and _async_transform_request previously only initialized �edrock_tool_name_mappings_context when None and never reset or scoped it across BedrockConverseLLM calls, so sequential calls on the same thread or asyncio task reused the prior request's dict.

Updated PR #54310 ($sha):

  • _transform_request and _async_transform_request (litellm/llms/bedrock/chat/converse_transformation.py) now unconditionally set a fresh {} at the start of each transform.
  • BedrockConverseLLM.completion, �sync_completion, and �sync_streaming (litellm/llms/bedrock/chat/converse_handler.py) wrap each request in oken = bedrock_tool_name_mappings_context.set({}) ... inally: reset(token).
  • AWSEventStreamDecoder / make_sync_call / make_call (litellm/llms/bedrock/chat/invoke_handler.py, converse_handler.py) snapshot ool_name_mappings per stream so streams cannot leak or interfere after completion/�sync_streaming returns.
  • Added regression tests ( est_bedrock_converse_sequential_same_thread_and_same_task_no_leak, est_aws_event_stream_decoder_uses_isolated_tool_name_mappings) and verified all 6 scenarios (sync-same-thread, sync-two-threads, �sync-same-task, �sync-two-tasks, sync-own-name, �sync-own-name) + 9/9 targeted pytest tool-name cases pass locally (run on Windows with an in-memory shim for newer _native cost-map symbols absent from the local binary).

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.

[Bug]: Bedrock Converse – process-global bedrock_tool_name_mappings rewrites tool names across requests and API keys

1 participant