Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(model): fallback tool_choice to "auto" for OpenAI-compatible structured output in thinking mode (#1830)
qbc09fd209bd19e
diff --git a/src/agentscope/model/_openai_chat/_model.py b/src/agentscope/model/_openai_chat/_model.pyindex 6056f2db4..8d7627cf4 100644--- a/src/agentscope/model/_openai_chat/_model.py+++ b/src/agentscope/model/_openai_chat/_model.py@@ -1,5 +1,6 @@# -*- coding: utf-8 -*-"""The OpenAI Chat Completions model implementation."""+import warningsimport base64import ioimport uuid@@ -11,7 +12,7 @@ from typing import Literal, Any, AsyncGenerator, TYPE_CHECKING, List, Typefrom pydantic import BaseModel, Fieldfrom .._base import ChatModelBase, _TOOL_CHOICE_LITERAL_MODES-from .._model_response import ChatResponse+from .._model_response import ChatResponse, StructuredResponsefrom .._model_usage import ChatUsagefrom ..._utils._audio import _build_streaming_wav_headerfrom ...credential import OpenAICredential@@ -593,6 +594,48 @@ class OpenAIChatModel(ChatModelBase):return ChatResponse(**resp_kwargs)+ async def _call_api_with_structured_output(+ self,+ model_name: str,+ messages: list[Msg],+ structured_model: Type[BaseModel] | dict,+ tool_choice: ToolChoice | None = None,+ **kwargs: Any,+ ) -> StructuredResponse:+ """OpenAI-compatible override for structured output.++ Some third-party providers that expose an OpenAI-compatible API (e.g.+ DeepSeek via DashScope) reject forced ``tool_choice`` when thinking+ mode is active. When such a ``BadRequestError`` is encountered, this+ method automatically retries with ``tool_choice="auto"``.+ """+ import openai++ try:+ return await super()._call_api_with_structured_output(+ model_name=model_name,+ messages=messages,+ structured_model=structured_model,+ tool_choice=tool_choice,+ **kwargs,+ )+ except openai.BadRequestError as e:+ if "tool_choice" not in str(e):+ raise+ # Thinking mode rejects forced tool_choice; fall back to auto+ warnings.warn(+ f"Forced tool_choice rejected by provider ({e}), "+ "retrying with tool_choice='auto'.",+ stacklevel=2,+ )+ return await super()._call_api_with_structured_output(+ model_name=model_name,+ messages=messages,+ structured_model=structured_model,+ tool_choice=ToolChoice(mode="auto"),+ **kwargs,+ )+def _format_tools(self,tools: list[dict] | None,