Skip to content

Build and run agents you can see, understand and trust.

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.py
index 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 warnings
import base64
import io
import uuid
@@ -11,7 +12,7 @@ from typing import Literal, Any, AsyncGenerator, TYPE_CHECKING, List, Type
from pydantic import BaseModel, Field
from .._base import ChatModelBase, _TOOL_CHOICE_LITERAL_MODES
-from .._model_response import ChatResponse
+from .._model_response import ChatResponse, StructuredResponse
from .._model_usage import ChatUsage
from ..._utils._audio import _build_streaming_wav_header
from ...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,