Skip to content

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

fix(model): unify `client_args` and `client_kwargs` arguments in the chat model classes into `client_kwargs` (#957)

qbcd2fcea077422
diff --git a/docs/tutorial/en/src/task_model.py b/docs/tutorial/en/src/task_model.py
index 3fffb2227..381ca130a 100644
--- a/docs/tutorial/en/src/task_model.py
+++ b/docs/tutorial/en/src/task_model.py
@@ -56,11 +56,11 @@ The supported model APIs and providers include:
.. note:: When using vLLM, you need to configure the appropriate tool calling parameters for different models during deployment, such as ``--enable-auto-tool-choice``, ``--tool-call-parser``, etc. For more details, refer to the `official vLLM documentation <https://docs.vllm.ai/en/latest/features/tool_calling.html>`_.
-.. note:: For OpenAI-compatible models (e.g. vLLM, Deepseek), developers can use the ``OpenAIChatModel`` class, and specify the API endpoint by the ``client_args`` parameter: ``client_args={"base_url": "http://your-api-endpoint"}``. For example:
+.. note:: For OpenAI-compatible models (e.g. vLLM, Deepseek), developers can use the ``OpenAIChatModel`` class, and specify the API endpoint by the ``client_kwargs`` parameter: ``client_kwargs={"base_url": "http://your-api-endpoint"}``. For example:
.. code-block:: python
- OpenAIChatModel(client_args={"base_url": "http://localhost:8000/v1"})
+ OpenAIChatModel(client_kwargs={"base_url": "http://localhost:8000/v1"})
.. note:: Model behavior parameters (such as temperature, maximum length, etc.) can be preset in the constructor function via the ``generate_kwargs`` parameter. For example:
diff --git a/docs/tutorial/zh_CN/src/task_model.py b/docs/tutorial/zh_CN/src/task_model.py
index 800b5a9b1..a4d3d4136 100644
--- a/docs/tutorial/zh_CN/src/task_model.py
+++ b/docs/tutorial/zh_CN/src/task_model.py
@@ -56,11 +56,11 @@ AgentScope 目前支持的模型 API 和模型提供商包括:
.. note:: 当使用 vLLM 时,需要在部署时为不同模型配置相应的工具调用参数,例如 ``--enable-auto-tool-choice``、``--tool-call-parser`` 等参数。更多详情请参考 `vLLM 官方文档 <https://docs.vllm.ai/en/latest/features/tool_calling.html>`_。
-.. note:: 兼容 OpenAI API 的模型(例如 vLLM 部署的模型),推荐使用 ``OpenAIChatModel``,并通过 ``client_args={"base_url": "http://your-api-endpoint"}`` 参数指定 API 端点。例如:
+.. note:: 兼容 OpenAI API 的模型(例如 vLLM 部署的模型),推荐使用 ``OpenAIChatModel``,并通过 ``client_kwargs={"base_url": "http://your-api-endpoint"}`` 参数指定 API 端点。例如:
.. code-block:: python
- OpenAIChatModel(client_args={"base_url": "http://localhost:8000/v1"})
+ OpenAIChatModel(client_kwargs={"base_url": "http://localhost:8000/v1"})
.. note:: 模型的行为参数(如温度、最大长度等)可以通过 ``generate_kwargs`` 参数在构造函数中提前设定。例如:
diff --git a/examples/agent/voice_agent/main.py b/examples/agent/voice_agent/main.py
index 0af37b176..445916ee2 100644
--- a/examples/agent/voice_agent/main.py
+++ b/examples/agent/voice_agent/main.py
@@ -20,7 +20,7 @@ async def main() -> None:
sys_prompt="You are a helpful assistant",
model=OpenAIChatModel(
model_name="qwen3-omni-flash",
- client_args={
+ client_kwargs={
"base_url": "https://dashscope.aliyuncs.com/"
"compatible-mode/v1",
},
diff --git a/src/agentscope/model/_anthropic_model.py b/src/agentscope/model/_anthropic_model.py
index 50aca1098..cf2329295 100644
--- a/src/agentscope/model/_anthropic_model.py
+++ b/src/agentscope/model/_anthropic_model.py
@@ -45,8 +45,9 @@ class AnthropicChatModel(ChatModelBase):
max_tokens: int = 2048,
stream: bool = True,
thinking: dict | None = None,
- client_args: dict | None = None,
+ client_kwargs: dict | None = None,
generate_kwargs: dict[str, JSONSerializableObject] | None = None,
+ **kwargs: Any,
) -> None:
"""Initialize the Anthropic chat model.
@@ -70,14 +71,39 @@ class AnthropicChatModel(ChatModelBase):
"budget_tokens": 1024
}
- client_args (`dict | None`, optional):
+ client_kwargs (`dict | None`, optional):
The extra keyword arguments to initialize the Anthropic client.
generate_kwargs (`dict[str, JSONSerializableObject] | None`, \
optional):
- The extra keyword arguments used in Gemini API generation,
+ The extra keyword arguments used in Anthropic API generation,
e.g. `temperature`, `seed`.
+ **kwargs (`Any`):
+ Additional keyword arguments.
"""
+ # Handle deprecated client_args parameter from kwargs
+ client_args = kwargs.pop("client_args", None)
+ if client_args is not None and client_kwargs is not None:
+ raise ValueError(
+ "Cannot specify both 'client_args' and 'client_kwargs'. "
+ "Please use only 'client_kwargs' (client_args is deprecated).",
+ )
+
+ if client_args is not None:
+ logger.warning(
+ "The parameter 'client_args' is deprecated and will be "
+ "removed in a future version. Please use 'client_kwargs' "
+ "instead. Automatically converting 'client_args' to "
+ "'client_kwargs'.",
+ )
+ client_kwargs = client_args
+
+ if kwargs:
+ logger.warning(
+ "Unknown keyword arguments: %s. These will be ignored.",
+ list(kwargs.keys()),
+ )
+
try:
import anthropic
except ImportError as e:
@@ -90,7 +116,7 @@ class AnthropicChatModel(ChatModelBase):
self.client = anthropic.AsyncAnthropic(
api_key=api_key,
- **(client_args or {}),
+ **(client_kwargs or {}),
)
self.max_tokens = max_tokens
self.thinking = thinking
diff --git a/src/agentscope/model/_gemini_model.py b/src/agentscope/model/_gemini_model.py
index 8eaacb972..bfa02557c 100644
--- a/src/agentscope/model/_gemini_model.py
+++ b/src/agentscope/model/_gemini_model.py
@@ -38,8 +38,9 @@ class GeminiChatModel(ChatModelBase):
api_key: str,
stream: bool = True,
thinking_config: dict | None = None,
- client_args: dict = None,
+ client_kwargs: dict = None,
generate_kwargs: dict[str, JSONSerializableObject] | None = None,
+ **kwargs: Any,
) -> None:
"""Initialize the Gemini chat model.
@@ -63,13 +64,39 @@ class GeminiChatModel(ChatModelBase):
"thinking_budget": 1024 # Max tokens for reasoning
}
- client_args (`dict`, default `None`):
- The extra keyword arguments to initialize the OpenAI client.
+ client_kwargs (`dict`, default `None`):
+ The extra keyword arguments to initialize the Gemini client.
generate_kwargs (`dict[str, JSONSerializableObject] | None`, \
optional):
The extra keyword arguments used in Gemini API generation,
e.g. `temperature`, `seed`.
+ **kwargs (`Any`):
+ Additional keyword arguments.
"""
+
+ # Handle deprecated client_args parameter from kwargs
+ client_args = kwargs.pop("client_args", None)
+ if client_args is not None and client_kwargs is not None:
+ raise ValueError(
+ "Cannot specify both 'client_args' and 'client_kwargs'. "
+ "Please use only 'client_kwargs' (client_args is deprecated).",
+ )
+
+ if client_args is not None:
+ logger.warning(
+ "The parameter 'client_args' is deprecated and will be "
+ "removed in a future version. Please use 'client_kwargs' "
+ "instead. Automatically converting 'client_args' to "
+ "'client_kwargs'.",
+ )
+ client_kwargs = client_args
+
+ if kwargs:
+ logger.warning(
+ "Unknown keyword arguments: %s. These will be ignored.",
+ list(kwargs.keys()),
+ )
+
try:
from google import genai
except ImportError as e:
@@ -82,7 +109,7 @@ class GeminiChatModel(ChatModelBase):
self.client = genai.Client(
api_key=api_key,
- **(client_args or {}),
+ **(client_kwargs or {}),
)
self.thinking_config = thinking_config
self.generate_kwargs = generate_kwargs or {}
diff --git a/src/agentscope/model/_openai_model.py b/src/agentscope/model/_openai_model.py
index 9bf5a8978..3a505f46f 100644
--- a/src/agentscope/model/_openai_model.py
+++ b/src/agentscope/model/_openai_model.py
@@ -72,8 +72,9 @@ class OpenAIChatModel(ChatModelBase):
stream: bool = True,
reasoning_effort: Literal["low", "medium", "high"] | None = None,
organization: str = None,
- client_args: dict = None,
+ client_kwargs: dict = None,
generate_kwargs: dict[str, JSONSerializableObject] | None = None,
+ **kwargs: Any,
) -> None:
"""Initialize the openai client.
@@ -94,14 +95,39 @@ class OpenAIChatModel(ChatModelBase):
organization (`str`, default `None`):
The organization ID for OpenAI API. If not specified, it will
be read from the environment variable `OPENAI_ORGANIZATION`.
- client_args (`dict`, default `None`):
+ client_kwargs (`dict`, default `None`):
The extra keyword arguments to initialize the OpenAI client.
generate_kwargs (`dict[str, JSONSerializableObject] | None`, \
optional):
The extra keyword arguments used in OpenAI API generation,
e.g. `temperature`, `seed`.
+ **kwargs (`Any`):
+ Additional keyword arguments.
"""
+ # Handle deprecated client_args parameter from kwargs
+ client_args = kwargs.pop("client_args", None)
+ if client_args is not None and client_kwargs is not None:
+ raise ValueError(
+ "Cannot specify both 'client_args' and 'client_kwargs'. "
+ "Please use only 'client_kwargs' (client_args is deprecated).",
+ )
+
+ if client_args is not None:
+ logger.warning(
+ "The parameter 'client_args' is deprecated and will be "
+ "removed in a future version. Please use 'client_kwargs' "
+ "instead. Automatically converting 'client_args' to "
+ "'client_kwargs'.",
+ )
+ client_kwargs = client_args
+
+ if kwargs:
+ logger.warning(
+ "Unknown keyword arguments: %s. These will be ignored.",
+ list(kwargs.keys()),
+ )
+
super().__init__(model_name, stream)
import openai
@@ -109,7 +135,7 @@ class OpenAIChatModel(ChatModelBase):
self.client = openai.AsyncClient(
api_key=api_key,
organization=organization,
- **(client_args or {}),
+ **(client_kwargs or {}),
)
self.reasoning_effort = reasoning_effort
diff --git a/tests/model_anthropic_test.py b/tests/model_anthropic_test.py
index a47520e74..49e0d3ddc 100644
--- a/tests/model_anthropic_test.py
+++ b/tests/model_anthropic_test.py
@@ -69,7 +69,7 @@ class TestAnthropicChatModel(IsolatedAsyncioTestCase):
"""Test initialization with custom parameters."""
thinking_config = {"type": "enabled", "budget_tokens": 1024}
generate_kwargs = {"temperature": 0.7, "top_p": 0.9}
- client_args = {"timeout": 30}
+ client_kwargs = {"timeout": 30}
with patch("anthropic.AsyncAnthropic") as mock_client:
model = AnthropicChatModel(
@@ -78,7 +78,7 @@ class TestAnthropicChatModel(IsolatedAsyncioTestCase):
max_tokens=4096,
stream=False,
thinking=thinking_config,
- client_args=client_args,
+ client_kwargs=client_kwargs,
generate_kwargs=generate_kwargs,
)
self.assertEqual(model.model_name, "claude-3-opus-20240229")
diff --git a/tests/model_gemini_test.py b/tests/model_gemini_test.py
index bb561e137..11a551350 100644
--- a/tests/model_gemini_test.py
+++ b/tests/model_gemini_test.py
@@ -85,7 +85,7 @@ class TestGeminiChatModel(IsolatedAsyncioTestCase):
"""Test initialization with custom parameters."""
thinking_config = {"include_thoughts": True, "thinking_budget": 1024}
generate_kwargs = {"temperature": 0.7, "top_p": 0.9}
- client_args = {"timeout": 30}
+ client_kwargs = {"timeout": 30}
with patch("google.genai.Client") as mock_client:
model = GeminiChatModel(
@@ -93,7 +93,7 @@ class TestGeminiChatModel(IsolatedAsyncioTestCase):
api_key="test_key",
stream=False,
thinking_config=thinking_config,
- client_args=client_args,
+ client_kwargs=client_kwargs,
generate_kwargs=generate_kwargs,
)
self.assertEqual(model.model_name, "gemini-2.5-pro")
diff --git a/tests/model_openai_test.py b/tests/model_openai_test.py
index 668e3577d..7ce1d6aa6 100644
--- a/tests/model_openai_test.py
+++ b/tests/model_openai_test.py
@@ -35,7 +35,7 @@ class TestOpenAIChatModel(IsolatedAsyncioTestCase):
def test_init_with_custom_params(self) -> None:
"""Test initialization with custom parameters."""
generate_kwargs = {"temperature": 0.7, "max_tokens": 1000}
- client_args = {"timeout": 30}
+ client_kwargs = {"timeout": 30}
with patch("openai.AsyncClient") as mock_client:
model = OpenAIChatModel(
model_name="gpt-4o",
@@ -43,7 +43,7 @@ class TestOpenAIChatModel(IsolatedAsyncioTestCase):
stream=False,
reasoning_effort="high",
organization="org-123",
- client_args=client_args,
+ client_kwargs=client_kwargs,
generate_kwargs=generate_kwargs,
)
self.assertEqual(model.model_name, "gpt-4o")