The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(azure_ai): strip non-spec message fields on a copy, not the caller's messages
ayaangazali95bc890fcfc1
diff --git a/litellm/llms/azure_ai/chat/transformation.py b/litellm/llms/azure_ai/chat/transformation.pyindex 683c05cfbaa..067c89214ab 100644--- a/litellm/llms/azure_ai/chat/transformation.py+++ b/litellm/llms/azure_ai/chat/transformation.py@@ -1,3 +1,4 @@+import copyimport enumimport refrom typing import Any, Final, cast@@ -182,9 +183,13 @@ class AzureAIStudioConfig(OpenAIConfig):Anthropic-format clients that echo thinking blocks back as history.2. Transforms list content to a string.3. If message contains an image or audio, send as is (user-intended)++ Operates on a deep copy so the caller's messages keep their thinking blocks+ and provider metadata, which a fallback to another provider still needs."""+ messages = copy.deepcopy(messages)for message in messages:- message_dict = cast(dict, message) # cast-ok: TypedDict is a runtime dict stripped in place+ message_dict = cast(dict, message) # cast-ok: TypedDict is a runtime dict stripped on our copyfor field in NON_OPENAI_SPEC_MESSAGE_FIELDS:filter_value_from_dict(message_dict, field)diff --git a/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.py b/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.pyindex 80c4355b560..beb7e9dfab0 100644--- a/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.py+++ b/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.py@@ -334,3 +334,53 @@ def test_azure_ai_strips_non_openai_spec_message_fields():assistant_message = transformed_messages[1]assert assistant_message["content"] == "I can help."assert assistant_message["tool_calls"][0]["function"]["name"] == "read_file"+++def test_azure_ai_stripping_does_not_mutate_caller_messages():+ """+ The stripping must not touch the caller's messages. LiteLLM reuses the same+ message objects when falling back to another provider, so stripping in place+ would hand the fallback a conversation history with its thinking blocks and+ provider metadata already destroyed.+ """+ config = AzureAIStudioConfig()++ messages = [+ {"role": "user", "content": "Read a file."},+ {+ "role": "assistant",+ "content": "I can help.",+ "thinking_blocks": [+ {"type": "thinking", "thinking": "Reading the file.", "signature": "sig"}+ ],+ "provider_specific_fields": {"thought_signature": "sig-top"},+ "tool_calls": [+ {+ "id": "call_1",+ "type": "function",+ "function": {+ "name": "read_file",+ "arguments": "{}",+ "provider_specific_fields": {"thought_signature": "sig-nested"},+ },+ }+ ],+ },+ ]++ request = config.transform_request(+ model="fw-glm-5.2",+ messages=messages,+ optional_params={},+ litellm_params={},+ headers={},+ )++ assert not _find_key_anywhere(request["messages"], "thinking_blocks")++ original_assistant = messages[1]+ assert original_assistant["thinking_blocks"][0]["thinking"] == "Reading the file."+ assert original_assistant["provider_specific_fields"] == {"thought_signature": "sig-top"}+ assert original_assistant["tool_calls"][0]["function"]["provider_specific_fields"] == {+ "thought_signature": "sig-nested"+ }