Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: add Responses API support for AzureOpenAIModel (#3941)

Adam Kuca109c6c3a5266
diff --git a/camel/models/azure_openai_model.py b/camel/models/azure_openai_model.py
index 79775a414..1efcf044a 100644
--- a/camel/models/azure_openai_model.py
+++ b/camel/models/azure_openai_model.py
@@ -13,49 +13,38 @@
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
import os
import warnings
-from typing import Any, Callable, Dict, List, Optional, Type, Union
-
-from openai import AsyncAzureOpenAI, AsyncStream, AzureOpenAI, Stream
-from openai.lib.streaming.chat import (
- AsyncChatCompletionStreamManager,
- ChatCompletionStreamManager,
+from typing import (
+ Any,
+ Callable,
+ Dict,
+ List,
+ Literal,
+ Optional,
+ Union,
)
-from pydantic import BaseModel
+
+from openai import AsyncAzureOpenAI, AzureOpenAI
from camel.configs import ChatGPTConfig
from camel.messages import OpenAIMessage
from camel.models.base_model import BaseModelBackend
-from camel.types import (
- ChatCompletion,
- ChatCompletionChunk,
- ModelType,
-)
+from camel.models.openai_model import OpenAIModel
+from camel.types import ModelType
from camel.utils import (
BaseTokenCounter,
- OpenAITokenCounter,
is_langfuse_available,
)
AzureADTokenProvider = Callable[[], str]
-if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":
- try:
- from langfuse.decorators import observe
- except ImportError:
- from camel.utils import observe
-elif os.environ.get("TRACEROOT_ENABLED", "False").lower() == "true":
- try:
- from traceroot import trace as observe # type: ignore[import]
- except ImportError:
- from camel.utils import observe
-else:
- from camel.utils import observe
-
-
-class AzureOpenAIModel(BaseModelBackend):
+class AzureOpenAIModel(OpenAIModel):
r"""Azure OpenAI API in a unified BaseModelBackend interface.
+ Inherits all Responses API, chat completion, and streaming methods
+ from :obj:`OpenAIModel`. Only the client initialization and
+ Azure-specific configuration are different.
+
Args:
model_type (Union[ModelType, str]): Model for which a backend is
created, Should be the deployment name you chose when you deployed
@@ -100,6 +89,12 @@ class AzureOpenAIModel(BaseModelBackend):
Use `model_type` parameter instead. This parameter is kept for
backward compatibility and will be removed in a future version.
(default: :obj:`None`)
+ api_mode (Literal["chat_completions", "responses"], optional):
+ Azure OpenAI API mode to use. Supported values:
+ `"chat_completions"` (default) and `"responses"`. The
+ `"responses"` mode uses the Azure OpenAI Responses API, which
+ supports stateful response chaining via
+ `previous_response_id`. (default: :obj:`"chat_completions"`)
**kwargs (Any): Additional arguments to pass to the client
initialization. Ignored if custom clients are provided.
@@ -122,6 +117,9 @@ class AzureOpenAIModel(BaseModelBackend):
client: Optional[Any] = None,
async_client: Optional[Any] = None,
azure_deployment_name: Optional[str] = None,
+ api_mode: Literal["chat_completions", "responses"] = (
+ "chat_completions"
+ ),
**kwargs: Any,
) -> None:
# Handle deprecated azure_deployment_name parameter
@@ -145,13 +143,30 @@ class AzureOpenAIModel(BaseModelBackend):
stacklevel=2,
)
+ if api_mode not in {"chat_completions", "responses"}:
+ raise ValueError(
+ "api_mode must be 'chat_completions' or 'responses', "
+ f"got: {api_mode}"
+ )
+ self._api_mode = api_mode
+ self._responses_previous_response_id_by_session: Dict[str, str] = {}
+ self._responses_last_message_count_by_session: Dict[str, int] = {}
+
if model_config_dict is None:
model_config_dict = ChatGPTConfig().as_dict()
api_key = api_key or os.environ.get("AZURE_OPENAI_API_KEY")
url = url or os.environ.get("AZURE_OPENAI_BASE_URL")
timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
- super().__init__(
- model_type, model_config_dict, api_key, url, token_counter, timeout
+ # Skip OpenAIModel.__init__ (requires OPENAI_API_KEY) and create
+ # BaseModelBackend directly for Azure-specific setup.
+ BaseModelBackend.__init__(
+ self,
+ model_type,
+ model_config_dict,
+ api_key,
+ url,
+ token_counter,
+ timeout,
)
self.api_version = api_version or os.environ.get("AZURE_API_VERSION")
@@ -232,231 +247,12 @@ class AzureOpenAIModel(BaseModelBackend):
**kwargs,
)
- @property
- def token_counter(self) -> BaseTokenCounter:
- r"""Initialize the token counter for the model backend.
-
- Returns:
- BaseTokenCounter: The token counter following the model's
- tokenization style.
- """
- if not self._token_counter:
- self._token_counter = OpenAITokenCounter(self.model_type)
- return self._token_counter
-
- @observe()
- def _run(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[Type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[
- ChatCompletion,
- Stream[ChatCompletionChunk],
- ChatCompletionStreamManager[BaseModel],
- ]:
- r"""Runs inference of Azure OpenAI chat completion.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
- response_format (Optional[Type[BaseModel]]): The format of the
- response.
- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
- use for the request.
-
- Returns:
- Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `Stream[ChatCompletionChunk]` in the stream mode.
- `ChatCompletionStreamManager[BaseModel]` for
- structured output streaming.
- """
- self._log_and_trace()
-
- response_format = response_format or self.model_config_dict.get(
- "response_format", None
- )
- is_streaming = self.model_config_dict.get("stream", False)
- if response_format:
- if is_streaming:
- return self._request_stream_parse(
- messages, response_format, tools
- )
- else:
- return self._request_parse(messages, response_format, tools)
- else:
- result = self._request_chat_completion(messages, tools)
-
- return result
-
- @observe()
- async def _arun(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[Type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[
- ChatCompletion,
- AsyncStream[ChatCompletionChunk],
- AsyncChatCompletionStreamManager[BaseModel],
- ]:
- r"""Runs inference of Azure OpenAI chat completion.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
- response_format (Optional[Type[BaseModel]]): The format of the
- response.
- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
- use for the request.
-
- Returns:
- Union[ChatCompletion, AsyncStream[ChatCompletionChunk],
- AsyncChatCompletionStreamManager[BaseModel]]:
- `ChatCompletion` in the non-stream mode, or
- `AsyncStream[ChatCompletionChunk]` in the stream mode.
- `AsyncChatCompletionStreamManager[BaseModel]` for
- structured output streaming.
- """
- self._log_and_trace()
-
- response_format = response_format or self.model_config_dict.get(
- "response_format", None
- )
- is_streaming = self.model_config_dict.get("stream", False)
- if response_format:
- if is_streaming:
- return await self._arequest_stream_parse(
- messages, response_format, tools
- )
- else:
- return await self._arequest_parse(
- messages, response_format, tools
- )
- else:
- result = await self._arequest_chat_completion(messages, tools)
-
- return result
-
- def _request_chat_completion(
- self,
- messages: List[OpenAIMessage],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- request_config = self._prepare_request_config(tools)
-
- return self._call_client(
- self._client.chat.completions.create,
- messages=messages,
- model=str(self.model_type),
- **request_config,
- )
-
- async def _arequest_chat_completion(
- self,
- messages: List[OpenAIMessage],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- request_config = self._prepare_request_config(tools)
-
- return await self._acall_client(
- self._async_client.chat.completions.create,
- messages=messages,
- model=str(self.model_type),
- **request_config,
- )
-
- def _request_parse(
- self,
- messages: List[OpenAIMessage],
- response_format: Type[BaseModel],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> ChatCompletion:
- request_config = self._prepare_request_config(tools)
- request_config["response_format"] = response_format
- # Remove stream from request config since OpenAI does not support it
- # with structured response
- request_config.pop("stream", None)
-
- return self._call_client(
- self._client.beta.chat.completions.parse,
- messages=messages,
- model=str(self.model_type),
- **request_config,
- )
-
- async def _arequest_parse(
- self,
- messages: List[OpenAIMessage],
- response_format: Type[BaseModel],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> ChatCompletion:
- request_config = self._prepare_request_config(tools)
- request_config["response_format"] = response_format
- # Remove stream from request config since OpenAI does not support it
- # with structured response
- request_config.pop("stream", None)
-
- return await self._acall_client(
- self._async_client.beta.chat.completions.parse,
- messages=messages,
- model=str(self.model_type),
- **request_config,
- )
-
- def _request_stream_parse(
- self,
- messages: List[OpenAIMessage],
- response_format: Type[BaseModel],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> ChatCompletionStreamManager[BaseModel]:
- r"""Request streaming structured output parsing.
-
- Note: This uses OpenAI's beta streaming API for structured outputs.
- """
- request_config = self._prepare_request_config(tools)
- # Remove stream from config as it's handled by the stream method
- request_config.pop("stream", None)
-
- # Use the beta streaming API for structured outputs
- return self._call_client(
- self._client.beta.chat.completions.stream,
- messages=messages,
- model=str(self.model_type),
- response_format=response_format,
- **request_config,
- )
-
- async def _arequest_stream_parse(
- self,
- messages: List[OpenAIMessage],
- response_format: Type[BaseModel],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> AsyncChatCompletionStreamManager[BaseModel]:
- r"""Request async streaming structured output parsing.
-
- Note: This uses OpenAI's beta streaming API for structured outputs.
- """
- request_config = self._prepare_request_config(tools)
- # Remove stream from config as it's handled by the stream method
- request_config.pop("stream", None)
-
- # Use the beta streaming API for structured outputs
- return self._call_client(
- self._async_client.beta.chat.completions.stream,
- messages=messages,
- model=str(self.model_type),
- response_format=response_format,
- **request_config,
- )
-
- @property
- def stream(self) -> bool:
- r"""Returns whether the model is in stream mode,
- which sends partial results each time.
+ def _sanitize_config(self, config_dict: Dict[str, Any]) -> Dict[str, Any]:
+ r"""No-op (Azure does not need)"""
+ return config_dict
- Returns:
- bool: Whether the model is in stream mode.
- """
- return self.model_config_dict.get("stream", False)
+ def _adapt_messages_for_o1_models(
+ self, messages: List[OpenAIMessage]
+ ) -> List[OpenAIMessage]:
+ r"""No-op (Azure does not need)"""
+ return messages
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index b90491104..33434c7cb 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -38,7 +38,7 @@ from pydantic import BaseModel
from camel.configs import ChatGPTConfig
from camel.logger import get_logger
from camel.messages import OpenAIMessage
-from camel.models import BaseModelBackend
+from camel.models.base_model import BaseModelBackend
from camel.types import (
ChatCompletion,
ChatCompletionChunk,
diff --git a/examples/models/azure_openai_responses_api_example.py b/examples/models/azure_openai_responses_api_example.py
new file mode 100644
index 000000000..82391fd51
--- /dev/null
+++ b/examples/models/azure_openai_responses_api_example.py
@@ -0,0 +1,197 @@
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+
+"""
+Azure OpenAI Responses API simple examples in CAMEL.
+
+This example demonstrates:
+1. Basic non-streaming chat
+2. Tool calling
+3. Streaming tool calling
+4. Structured output
+5. Streaming structured output
+
+Required environment variable:
+export AZURE_OPENAI_API_KEY="your_azure_openai_api_key"
+export AZURE_OPENAI_BASE_URL="https://<resource>.openai.azure.com/"
+export AZURE_API_VERSION="2025-03-01-preview"
+
+The deployment name passed as model_type must match Azure deployment name.
+"""
+
+from pydantic import BaseModel, Field
+
+from camel.agents import ChatAgent
+from camel.models import ModelFactory
+from camel.toolkits import FunctionTool
+from camel.types import ModelPlatformType, ModelType
+
+
+def get_weather(city: str) -> str:
+ r"""A tiny tool function for demo purposes."""
+ fake_weather = {
+ "beijing": "sunny, 28C",
+ "new york": "cloudy, 19C",
+ "san francisco": "foggy, 16C",
+ }
+ return fake_weather.get(city.lower(), f"unknown weather for {city}")
+
+
+class TravelAdvice(BaseModel):
+ city: str = Field(description="City name")
+ clothing: str = Field(description="Recommended clothing")
+ reason: str = Field(description="Brief reasoning")
+
+
+def create_responses_model(
+ stream: bool = False,
+ model_type: ModelType = ModelType.GPT_4_1_MINI,
+ temperature: float = 0.2,
+ tools: list | None = None,
+):
+ r"""Create an AzureOpenAIModel configured to use the Responses API."""
+ model_config = {
+ "temperature": temperature,
+ "stream": stream,
+ }
+ if tools:
+ model_config["tools"] = tools
+ if stream:
+ model_config["stream_options"] = {"include_usage": True}
+
+ return ModelFactory.create(
+ model_platform=ModelPlatformType.AZURE,
+ model_type=model_type,
+ model_config_dict=model_config,
+ api_mode="responses",
+ )
+
+
+def example_basic_non_stream() -> None:
+ print("\n=== Example 1: Basic Non-Streaming ===")
+ model = create_responses_model(stream=False)
+ agent = ChatAgent(
+ system_message="You are a concise assistant.",
+ model=model,
+ )
+ resp = agent.step("Say hello to CAMEL in one sentence.")
+ print(resp.msgs[0].content)
+ print("usage:", resp.info.get("usage"))
+
+
+def example_tool_calling() -> None:
+ print("\n=== Example 2: Tool Calling ===")
+ weather_tool = FunctionTool(get_weather)
+ model = create_responses_model(
+ stream=False,
+ model_type=ModelType.GPT_4_1,
+ temperature=0.0,
+ tools=[weather_tool.get_openai_tool_schema()],
+ )
+ agent = ChatAgent(
+ system_message="You are a weather assistant. Use tools when needed.",
+ model=model,
+ tools=[weather_tool],
+ )
+ resp = agent.step("How is the weather in Beijing today?")
+ print(resp.msgs[0].content)
+ print("tool_calls:", resp.info.get("tool_calls"))
+
+
+def example_streaming_tool_calling() -> None:
+ print("\n=== Example 3: Streaming Tool Calling ===")
+ weather_tool = FunctionTool(get_weather)
+ model = create_responses_model(
+ stream=True,
+ model_type=ModelType.GPT_4_1,
+ temperature=0.0,
+ tools=[weather_tool.get_openai_tool_schema()],
+ )
+ agent = ChatAgent(
+ system_message="You are a weather assistant. Use tools when needed.",
+ model=model,
+ tools=[weather_tool],
+ stream_accumulate=False,
+ )
+ stream_resp = agent.step("How is the weather in Beijing today?")
+
+ full_text = ""
+ for chunk in stream_resp:
+ msg = chunk.msgs[0]
+ if msg.content:
+ full_text += msg.content
+ print(msg.content, end="", flush=True)
+ print()
+ print("final:", full_text)
+ print("tool_calls:", stream_resp.info.get("tool_calls"))
+ print("usage:", stream_resp.info.get("usage"))
+
+
+def example_structured_output() -> None:
+ print("\n=== Example 4: Structured Output ===")
+ model = create_responses_model(stream=False)
+ agent = ChatAgent(
+ system_message="You are a travel assistant.",
+ model=model,
+ )
+ resp = agent.step(
+ "I am going to New York in autumn. Give advice as JSON.",
+ response_format=TravelAdvice,
+ )
+ print("raw:", resp.msgs[0].content)
+ print("parsed:", resp.msgs[0].parsed)
+
+
+def example_streaming_structured_output() -> None:
+ print("\n=== Example 5: Streaming Structured Output ===")
+ model = create_responses_model(
+ stream=True,
+ model_type=ModelType.GPT_4_1_MINI,
+ temperature=0.0,
+ )
+ agent = ChatAgent(
+ system_message=(
+ "You are a travel assistant and must return strict JSON matching "
+ "the schema."
+ ),
+ model=model,
+ stream_accumulate=False,
+ )
+
+ stream_resp = agent.step(
+ "I am going to New York in autumn. Give travel advice in JSON.",
+ response_format=TravelAdvice,
+ )
+ full_text = ""
+ for chunk in stream_resp:
+ msg = chunk.msgs[0]
+ if msg.content:
+ full_text += msg.content
+ print(msg.content, end="", flush=True)
+ print()
+ print("raw:", full_text)
+ try:
+ parsed = TravelAdvice.model_validate_json(full_text)
+ print("parsed:", parsed)
+ except Exception as exc:
+ print("parsed error:", exc)
+ print("usage:", stream_resp.info.get("usage"))
+
+
+if __name__ == "__main__":
+ example_basic_non_stream()
+ example_tool_calling()
+ example_streaming_tool_calling()
+ example_structured_output()
+ example_streaming_structured_output()
diff --git a/test/models/test_azure_openai_model.py b/test/models/test_azure_openai_model.py
index 54424a8ea..61079ab89 100644
--- a/test/models/test_azure_openai_model.py
+++ b/test/models/test_azure_openai_model.py
@@ -20,13 +20,22 @@ export AZURE_API_VERSION=""
export AZURE_OPENAI_API_KEY=""
"""
+from unittest.mock import MagicMock
+
import pytest
+from pydantic import BaseModel
from camel.configs import ChatGPTConfig
from camel.models import AzureOpenAIModel, ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.utils import OpenAITokenCounter
+AZURE_INIT_KWARGS = dict(
+ api_version="2024-02-15-preview",
+ api_key="test-key",
+ url="https://test.openai.azure.com/",
+)
+
@pytest.mark.model_backend
@pytest.mark.parametrize(
@@ -44,9 +53,7 @@ def test_openai_model(model_type):
model = AzureOpenAIModel(
model_type,
model_config_dict,
- api_version="2024-02-15-preview",
- api_key="test-key",
- url="https://test.openai.azure.com/",
+ **AZURE_INIT_KWARGS,
)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -76,3 +83,168 @@ def test_openai_model_create(model_type: ModelType):
url="https://test.openai.azure.com/",
)
assert model.model_type == model_type
+
+
+def test_api_mode_default_is_chat_completions():
+ model = AzureOpenAIModel(ModelType.GPT_4O, **AZURE_INIT_KWARGS)
+ assert model._api_mode == "chat_completions"
+
+
+def test_api_mode_responses_sets_mode():
+ model = AzureOpenAIModel(
+ ModelType.GPT_4O, **AZURE_INIT_KWARGS, api_mode="responses"
+ )
+ assert model._api_mode == "responses"
+ assert model._responses_previous_response_id_by_session == {}
+ assert model._responses_last_message_count_by_session == {}
+
+
+def test_api_mode_invalid_raises():
+ with pytest.raises(ValueError, match="api_mode must be"):
+ AzureOpenAIModel(
+ ModelType.GPT_4O, **AZURE_INIT_KWARGS, api_mode="invalid"
+ )
+
+
+def test_normalize_tools_for_responses_api():
+ tools = [
+ {
+ "type": "function",
+ "function": {"name": "get_weather", "parameters": {}},
+ }
+ ]
+ result = AzureOpenAIModel._normalize_tools_for_responses_api(tools)
+ assert result == [
+ {"type": "function", "name": "get_weather", "parameters": {}}
+ ]
+
+
+def test_convert_messages_tool_call_history():
+ messages = [
+ {"role": "user", "content": "What is the weather?"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "id": "call_abc",
+ "type": "function",
+ "function": {
+ "name": "get_weather",
+ "arguments": '{"city": "London"}',
+ },
+ }
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_abc",
+ "content": "Sunny, 20C",
+ },
+ ]
+ result = AzureOpenAIModel._convert_messages_to_responses_input(messages)
+ assert result[0] == {"role": "user", "content": "What is the weather?"}
+ assert result[1] == {
+ "type": "function_call",
+ "call_id": "call_abc",
+ "name": "get_weather",
+ "arguments": '{"city": "London"}',
+ }
+ assert result[2] == {
+ "type": "function_call_output",
+ "call_id": "call_abc",
+ "output": "Sunny, 20C",
+ }
+
+
+def test_run_routes_to_responses_api():
+ r"""Test that _run calls responses.create when api_mode='responses'."""
+ model = AzureOpenAIModel(
+ ModelType.GPT_4O, **AZURE_INIT_KWARGS, api_mode="responses"
+ )
+
+ fake_response = MagicMock()
+ fake_response.id = "resp_123"
+ fake_response.output = []
+ fake_response.usage = None
+ fake_response.created_at = 0
+
+ model._client = MagicMock()
+ model._client.responses.create.return_value = fake_response
+
+ messages = [{"role": "user", "content": "Hello"}]
+ model._run(messages)
+
+ model._client.responses.create.assert_called_once()
+ call_kwargs = model._client.responses.create.call_args.kwargs
+ assert str(call_kwargs["model"]) == str(ModelType.GPT_4O)
+ assert call_kwargs["stream"] is False
+
+
+def test_run_chat_completions_mode_does_not_call_responses():
+ r"""Test that _run does not call responses.create in default mode."""
+ model = AzureOpenAIModel(ModelType.GPT_4O, **AZURE_INIT_KWARGS)
+
+ fake_completion = MagicMock()
+ model._client = MagicMock()
+ model._client.chat.completions.create.return_value = fake_completion
+
+ messages = [{"role": "user", "content": "Hello"}]
+ model._run(messages)
+
+ model._client.chat.completions.create.assert_called_once()
+ model._client.responses.create.assert_not_called()
+
+
+def test_responses_chain_state_saved_after_run():
+ r"""Test that response ID is saved for chaining after a successful call."""
+ model = AzureOpenAIModel(
+ ModelType.GPT_4O, **AZURE_INIT_KWARGS, api_mode="responses"
+ )
+
+ fake_response = MagicMock()
+ fake_response.id = "resp_chain_001"
+ fake_response.output = []
+ fake_response.usage = None
+ fake_response.created_at = 0
+
+ model._client = MagicMock()
+ model._client.responses.create.return_value = fake_response
+
+ messages = [{"role": "user", "content": "Hello"}]
+ model._run(messages)
+
+ assert (
+ "resp_chain_001"
+ in model._responses_previous_response_id_by_session.values()
+ )
+
+
+def test_prepare_responses_request_config_structured_output():
+ r"""Test that a JSON schema is attached when response_format is given."""
+
+ class MySchema(BaseModel):
+ answer: str
+
+ model = AzureOpenAIModel(
+ ModelType.GPT_4O, **AZURE_INIT_KWARGS, api_mode="responses"
+ )
+ config = model._prepare_responses_request_config(
+ response_format=MySchema, stream=False
+ )
+ assert "text" in config
+ assert config["text"]["format"]["type"] == "json_schema"
+ assert config["text"]["format"]["name"] == "MySchema"
+
+
+def test_prepare_responses_request_config_n_warning():
+ r"""Test that a warning is emitted when n > 1 in responses mode."""
+ model = AzureOpenAIModel(
+ ModelType.GPT_4O,
+ model_config_dict=ChatGPTConfig(n=3).as_dict(),
+ **AZURE_INIT_KWARGS,
+ api_mode="responses",
+ )
+ with pytest.warns(UserWarning, match="does not support `n`"):
+ config = model._prepare_responses_request_config()
+ assert "n" not in config