From 19f8c63cf6dedca43e7bf443b7abb3793fe63574 Mon Sep 17 00:00:00 2001 From: PRABHU KIRAN VANDRANKI <72809214+VANDRANKI@users.noreply.github.com> Date: Wed, 6 May 2026 15:34:34 -0400 Subject: [PATCH] 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 --- litellm/litellm_core_utils/litellm_logging.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index fd14f55add3..c270797455b 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -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