fix(anthropic): strip Responses-only kwargs before Messages SDK call (#31673) (#42155)

A Responses-API-shaped payload carrying instructions=/input=/store=/
parallel_tool_calls= can reach the native Anthropic messages.stream() /
messages.create() call under a rare api_mode-flip race (e.g. a concurrent
auxiliary vision call mutating a shared agent between the kwargs build and
the stream dispatch). The Anthropic SDK rejects these with a non-retryable
TypeError that kills the whole turn and propagates the entire fallback chain.

Add sanitize_anthropic_kwargs() at both Anthropic dispatch sites: it drops
the Responses-only keys in place and logs a WARNING (with #31673 breadcrumb)
when one is present, so the underlying race stays visible in the wild
instead of being silently papered over.
This commit is contained in:
Teknium
2026-06-08 09:36:38 -07:00
committed by GitHub
parent 47d5177a7d
commit 399b8ee5f0
4 changed files with 149 additions and 0 deletions
+7
View File
@@ -3915,6 +3915,13 @@ class AIAgent:
def _anthropic_messages_create(self, api_kwargs: dict):
if self.api_mode == "anthropic_messages":
self._try_refresh_anthropic_client_credentials()
# Defensive: strip Responses-only kwargs that can leak in under an
# api_mode-flip race (the Anthropic SDK raises a non-retryable
# TypeError on them). See #31673.
from agent.anthropic_adapter import sanitize_anthropic_kwargs
sanitize_anthropic_kwargs(
api_kwargs, log_prefix=getattr(self, "log_prefix", "")
)
return self._anthropic_client.messages.create(**api_kwargs)
def _rebuild_anthropic_client(self) -> None: