fix: prompt cache markers on tool results are dropped before reaching the provider (#33757)

Tool results sent through an OpenAI-compatible gateway lose their prompt cache marker right before the request leaves, so Anthropic models behind OpenRouter or LiteLLM never cache the prefix in tool loops. Now the marker is kept on a single text block when a tool result carries one. Tool results without a marker are sent exactly as before.

Fixes #33755
This commit is contained in:
Classic298 2026-11-04 22:44:14 +01:00 • committed by GitHub
parent d221a4f69b
commit 1be39d2f32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1559,8 +1559,14 @@ async def generate_chat_completion(
if not is_responses and 'messages' in payload:
for message in payload['messages']:
if message.get('role') == 'tool' and isinstance(message.get('content'), list):
message['content'] = ''.join(
part.get('text', '') for part in message['content'] if part.get('type') in ('input_text', 'text')
parts = message['content']
text = ''.join(part.get('text', '') for part in parts if part.get('type') in ('input_text', 'text'))
cache_control = next(
(part['cache_control'] for part in reversed(parts) if part.get('cache_control')), None
)
# Prompt-cache markers only work on a content block, so keep the text as one
message['content'] = (
[{'type': 'text', 'text': text, 'cache_control': cache_control}] if cache_control and text else text
)
is_streaming_request = bool(payload.get('stream', False))