Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle azure deepseek reasoning response #8288

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
import traceback
import uuid
import re
from typing import Dict, Iterable, List, Literal, Optional, Union

import litellm
Expand Down Expand Up @@ -415,8 +416,17 @@ def convert_to_model_response_object( # noqa: PLR0915
for field in choice["message"].keys():
if field not in message_keys:
provider_specific_fields[field] = choice["message"][field]

# Handle reasoning models that display `reasoning_content` within `content``
content = choice["message"].get("content", None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have this be in a separate helper function - it'll be easier to add a unit test for this as well

reasoning_match = re.match(r"<think>(.*?)</think>(.*)", content, re.DOTALL)

if reasoning_match:
provider_specific_fields["reasoning_content"] = reasoning_match.group(1)
content = reasoning_match.group(2)

message = Message(
content=choice["message"].get("content", None),
content=content,
role=choice["message"]["role"] or "assistant",
function_call=choice["message"].get("function_call", None),
tool_calls=tool_calls,
Expand Down
44 changes: 43 additions & 1 deletion tests/llm_translation/test_azure_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from litellm.llms.anthropic.chat import ModelResponseIterator
import httpx
import json
from respx import MockRouter
from litellm.llms.custom_httpx.http_handler import HTTPHandler

load_dotenv()
import io
Expand Down Expand Up @@ -184,3 +184,45 @@ def test_completion_azure_ai_command_r():
pass
except Exception as e:
pytest.fail(f"Error occurred: {e}")

def test_azure_deepseek_reasoning_content():
import json

client = HTTPHandler()

with patch.object(client, "post") as mock_post:
mock_response = MagicMock()

mock_response.text = json.dumps(
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "<think>I am thinking here</think>\n\nThe sky is a canvas of blue",
"role": "assistant",
}
}
],
}
)

mock_response.status_code = 200
# Add required response attributes
mock_response.headers = {"Content-Type": "application/json"}
mock_response.json = lambda: json.loads(mock_response.text)
mock_post.return_value = mock_response


response = litellm.completion(
model='azure_ai/deepseek-r1',
messages=[{"role": "user", "content": "Hello, world!"}],
api_base="https://litellm8397336933.services.ai.azure.com/models/chat/completions",
api_key="my-fake-api-key",
client=client
)

print(response)
assert(response.choices[0].message.reasoning_content == "I am thinking here")
assert(response.choices[0].message.content == "\n\nThe sky is a canvas of blue")