@@ -722,6 +722,19 @@ def _build_messages(self, prompt, system_prompt=None, chat_history=None, output_
722722 if schema_model and hasattr (schema_model , 'model_json_schema' ):
723723 system_prompt += f"\n Return ONLY a JSON object that matches this Pydantic model: { json .dumps (schema_model .model_json_schema ())} "
724724
725+ # For XML format models, add tool descriptions to system prompt
726+ if tools and self ._supports_xml_tool_format ():
727+ system_prompt += "\n \n You have access to the following tools:"
728+ for tool in tools :
729+ if isinstance (tool , dict ) and 'function' in tool :
730+ func = tool ['function' ]
731+ name = func .get ('name' , 'unknown' )
732+ description = func .get ('description' , 'No description available' )
733+ system_prompt += f"\n - { name } : { description } "
734+
735+ system_prompt += "\n \n When you need to use a tool, wrap your tool call in XML tags like this:"
736+ system_prompt += "\n <tool_call>\n {\" name\" : \" tool_name\" , \" arguments\" : {\" param\" : \" value\" }}\n </tool_call>"
737+
725738 # Skip system messages for legacy o1 models as they don't support them
726739 if not self ._needs_system_message_skip ():
727740 messages .append ({"role" : "system" , "content" : system_prompt })
@@ -3155,19 +3168,28 @@ def _build_completion_params(self, **override_params) -> Dict[str, Any]:
31553168
31563169 logging .debug (f"Using Gemini native structured output with schema: { json .dumps (schema , indent = 2 )} " )
31573170
3158- # Add tool_choice="auto" when tools are provided (unless already specified)
3159- if 'tools' in params and params ['tools' ] and 'tool_choice' not in params :
3160- # For Gemini models, use tool_choice to encourage tool usage
3161- if self ._is_gemini_model ():
3162- try :
3163- import litellm
3164- # Check if model supports function calling before setting tool_choice
3165- if litellm .supports_function_calling (model = self .model ):
3166- params ['tool_choice' ] = 'auto'
3167- except Exception as e :
3168- # If check fails, still set tool_choice for known Gemini models
3169- logging .debug (f"Could not verify function calling support: { e } . Setting tool_choice anyway." )
3170- params ['tool_choice' ] = 'auto'
3171+ # Handle XML format models (like Qwen) differently for tool calls
3172+ if 'tools' in params and params ['tools' ]:
3173+ if self ._supports_xml_tool_format ():
3174+ # For XML format models, remove tools parameter to avoid OpenRouter routing issues
3175+ # Tools will be described in the system prompt instead
3176+ logging .debug ("Removing tools parameter for XML format model to avoid provider routing issues" )
3177+ params .pop ('tools' , None )
3178+ params .pop ('tool_choice' , None )
3179+ else :
3180+ # Add tool_choice="auto" when tools are provided (unless already specified)
3181+ if 'tool_choice' not in params :
3182+ # For Gemini models, use tool_choice to encourage tool usage
3183+ if self ._is_gemini_model ():
3184+ try :
3185+ import litellm
3186+ # Check if model supports function calling before setting tool_choice
3187+ if litellm .supports_function_calling (model = self .model ):
3188+ params ['tool_choice' ] = 'auto'
3189+ except Exception as e :
3190+ # If check fails, still set tool_choice for known Gemini models
3191+ logging .debug (f"Could not verify function calling support: { e } . Setting tool_choice anyway." )
3192+ params ['tool_choice' ] = 'auto'
31713193
31723194 return params
31733195
0 commit comments