Skip to content

Commit

Permalink
fix(#5818): Force to use string serializer for deepseek function call…
Browse files Browse the repository at this point in the history
…ing (#5824)

Co-authored-by: Engel Nyst <[email protected]>
  • Loading branch information
xingyaoww and enyst authored Dec 26, 2024
1 parent ad45f8d commit a021045
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion openhands/core/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Message(BaseModel):
# - tool execution result (to LLM)
tool_call_id: str | None = None
name: str | None = None # name of the tool
# force string serializer
force_string_serializer: bool = False

@property
def contains_image(self) -> bool:
Expand All @@ -73,7 +75,9 @@ def serialize_model(self) -> dict:
# - into a single string: for providers that don't support list of content items (e.g. no vision, no tool calls)
# - into a list of content items: the new APIs of providers with vision/prompt caching/tool calls
# NOTE: remove this when litellm or providers support the new API
if self.cache_enabled or self.vision_enabled or self.function_calling_enabled:
if not self.force_string_serializer and (
self.cache_enabled or self.vision_enabled or self.function_calling_enabled
):
return self._list_serializer()
# some providers, like HF and Groq/llama, don't support a list here, but a single string
return self._string_serializer()
Expand Down
8 changes: 8 additions & 0 deletions openhands/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def __init__(
if self.is_function_calling_active():
logger.debug('LLM: model supports function calling')

# Compatibility flag: use string serializer for DeepSeek models
# See this issue: https://github.com/All-Hands-AI/OpenHands/issues/5818
self._use_string_serializer = False
if 'deepseek' in self.config.model:
self._use_string_serializer = True

# if using a custom tokenizer, make sure it's loaded and accessible in the format expected by litellm
if self.config.custom_tokenizer is not None:
self.tokenizer = create_pretrained_tokenizer(self.config.custom_tokenizer)
Expand Down Expand Up @@ -618,6 +624,8 @@ def format_messages_for_llm(self, messages: Message | list[Message]) -> list[dic
message.cache_enabled = self.is_caching_prompt_active()
message.vision_enabled = self.vision_is_active()
message.function_calling_enabled = self.is_function_calling_active()
if 'deepseek' in self.config.model:
message.force_string_serializer = True

# let pydantic handle the serialization
return [message.model_dump() for message in messages]

0 comments on commit a021045

Please sign in to comment.