fix(logging): prevent AnthropicResponse ValidationError for Responses API events in anthropic_messages logging

When the experimental Anthropic pass-through adapter routes a request through
the Responses API backend (e.g. Azure/Foundry), the success handler in
_handle_anthropic_messages_response_logging() receives a ResponseIncompleteEvent
or ResponseCompletedEvent object from the Responses API stream. These types are
not dict-serialisable as AnthropicResponse, so AnthropicResponse.model_validate()
raises a ValidationError in background (non-blocking) logging:

  1 validation error for AnthropicResponse
  Input should be a valid dictionary or instance of AnthropicResponse
  [type=model_type, input_value=ResponseIncompleteEvent(...)]

Fix: add isinstance guards for ResponseCompletedEvent / ResponseIncompleteEvent
before the model_validate call, and also guard any other non-dict, non-
AnthropicResponse type. When the guard triggers, emit a debug-level log and
return the result as-is so downstream logging continues without crashing.

Fixes #27091
This commit is contained in:
PRABHU KIRAN VANDRANKI 2026-05-06 15:34:34 -04:00
parent 850fe595ac
commit 19f8c63cf6

View file

@ -3446,6 +3446,33 @@ class Logging(LiteLLMLoggingBaseClass):
)
else:
from litellm.types.llms.anthropic import AnthropicResponse
from litellm.types.llms.openai import (
ResponseCompletedEvent,
ResponseIncompleteEvent,
)
# When the experimental Anthropic pass-through adapter routes a request
# through the Responses API (e.g. to Azure/Foundry), the success handler
# may receive a ResponseIncompleteEvent or ResponseCompletedEvent rather
# than a plain dict or AnthropicResponse instance.
# AnthropicResponse.model_validate() rejects these types and raises a
# ValidationError in background logging (non-blocking but noisy).
# Guard against this before calling model_validate. (Fixes #27091)
if isinstance(result, (ResponseCompletedEvent, ResponseIncompleteEvent)):
verbose_logger.debug(
"anthropic_messages logging: result is a Responses API event (%s), "
"skipping AnthropicResponse transform",
type(result).__name__,
)
return result # type: ignore[return-value]
if not isinstance(result, (dict, AnthropicResponse)):
verbose_logger.debug(
"anthropic_messages logging: unexpected result type %s, "
"skipping AnthropicResponse transform",
type(result).__name__,
)
return result # type: ignore[return-value]
pydantic_result = AnthropicResponse.model_validate(result)
import httpx