π OpenHands: AI-Driven Development
fix(#5818): Force to use string serializer for deepseek function calling (#5824)
Xingyao Wanga021045dced8
diff --git a/openhands/core/message.py b/openhands/core/message.pyindex e7ae745d38..5da0cc6be3 100644--- a/openhands/core/message.py+++ b/openhands/core/message.py@@ -62,6 +62,8 @@ class Message(BaseModel):# - tool execution result (to LLM)tool_call_id: str | None = Nonename: str | None = None # name of the tool+ # force string serializer+ force_string_serializer: bool = False@propertydef contains_image(self) -> bool:@@ -73,7 +75,9 @@ class Message(BaseModel):# - 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 stringreturn self._string_serializer()diff --git a/openhands/llm/llm.py b/openhands/llm/llm.pyindex 85fa1d667c..423301f190 100644--- a/openhands/llm/llm.py+++ b/openhands/llm/llm.py@@ -122,6 +122,12 @@ class LLM(RetryMixin, DebugMixin):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 litellmif self.config.custom_tokenizer is not None:self.tokenizer = create_pretrained_tokenizer(self.config.custom_tokenizer)@@ -618,6 +624,8 @@ class LLM(RetryMixin, DebugMixin):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 serializationreturn [message.model_dump() for message in messages]