Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions python/packages/openai/agent_framework_openai/_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,16 @@ def _convert_response_format(self, response_format: Mapping[str, Any]) -> dict[s
def _get_conversation_id(
self, response: OpenAIResponse | ParsedResponse[BaseModel], store: bool | None
) -> str | None:
"""Get the conversation ID from the response if store is True."""
if store is False:
"""Get the server-managed conversation identifier when ``store=True`` is set.

Only returns a non-None value when ``store=True`` is explicitly set to opt into
server-managed conversation mode (OpenAI native). In that mode, this returns
``response.conversation.id`` when present; otherwise it falls back to
``response.id``. When ``store`` is ``None`` (the default) or ``False``,
returns ``None`` so the executor uses full-history mode, which is compatible
with providers that do not support ``previous_response_id`` (e.g. OpenRouter).
"""
if store is not True:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is not the way, this client is designed to connect to the Responses API and that API (at least in both OpenAI and Foundry) uses service side storage by default, so only when store is explicitly set to False do we not store anything, the assumption for these clients is that when store is not set, the value is True, hence the check for store=False above. In order to use this as expected with other providers, either create a issue on their end that they adhere to the responses API which means they would have to support the store=True mechanism, or that they raise when store=None, or in your own code, set the options to store=False

return None
# If conversation ID exists, it means that we operate with conversation
# so we use conversation ID as input and output.
Expand Down
42 changes: 34 additions & 8 deletions python/packages/openai/tests/openai/test_openai_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,24 @@ def test_parse_response_with_store_false() -> None:
assert conversation_id is None


def test_parse_response_with_store_none_returns_none() -> None:
"""Test _get_conversation_id returns None when store=None (default).

Regression for https://github.com/microsoft/agent-framework/issues/5105 —
ensures full-history mode is used by default, which works with providers
that don't support previous_response_id (e.g. OpenRouter).
"""
client = OpenAIChatClient(model="test-model", api_key="test-key")

mock_response = MagicMock()
mock_response.id = "resp_123"
mock_response.conversation = MagicMock()
mock_response.conversation.id = "conv_456"

assert client._get_conversation_id(mock_response, store=None) is None
assert client._get_conversation_id(mock_response, store=False) is None


def test_parse_response_uses_response_id_when_no_conversation() -> None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test covers the store=None and store=False negative cases well, but doesn't verify the positive case (store=True returning conv_456). Adding assert client._get_conversation_id(mock_response, store=True) == 'conv_456' would make this a complete tri-state test and guard against future regressions that might accidentally break the store=True path.

Suggested change
def test_parse_response_uses_response_id_when_no_conversation() -> None:
assert client._get_conversation_id(mock_response, store=None) is None
assert client._get_conversation_id(mock_response, store=False) is None
assert client._get_conversation_id(mock_response, store=True) == "conv_456"

"""Test _get_conversation_id returns response ID when no conversation exists."""
client = OpenAIChatClient(model="test-model", api_key="test-key")
Expand Down Expand Up @@ -3259,9 +3277,8 @@ def test_streaming_response_basic_structure() -> None:


def test_streaming_response_created_type() -> None:
"""Test streaming response with created type"""
"""Test streaming response with created type sets conversation_id only when store=True."""
client = OpenAIChatClient(model="test-model", api_key="test-key")
chat_options = ChatOptions()
function_call_ids: dict[int, tuple[str, str]] = {}

mock_event = MagicMock()
Expand All @@ -3271,16 +3288,20 @@ def test_streaming_response_created_type() -> None:
mock_event.response.conversation = MagicMock()
mock_event.response.conversation.id = "conv_5678"

response = client._parse_chunk_from_openai(mock_event, chat_options, function_call_ids)

# store=True: server-managed mode — conversation_id is returned
response = client._parse_chunk_from_openai(mock_event, ChatOptions(store=True), function_call_ids)
assert response.response_id == "resp_1234"
assert response.conversation_id == "conv_5678"

# store=None (default): full-history mode — conversation_id is NOT returned
response = client._parse_chunk_from_openai(mock_event, ChatOptions(), function_call_ids)
assert response.response_id == "resp_1234"
assert response.conversation_id is None


def test_streaming_response_in_progress_type() -> None:
"""Test streaming response with in_progress type"""
"""Test streaming response with in_progress type sets conversation_id only when store=True."""
client = OpenAIChatClient(model="test-model", api_key="test-key")
chat_options = ChatOptions()
function_call_ids: dict[int, tuple[str, str]] = {}

mock_event = MagicMock()
Expand All @@ -3290,11 +3311,16 @@ def test_streaming_response_in_progress_type() -> None:
mock_event.response.conversation = MagicMock()
mock_event.response.conversation.id = "conv_5678"

response = client._parse_chunk_from_openai(mock_event, chat_options, function_call_ids)

# store=True: server-managed mode — conversation_id is returned
response = client._parse_chunk_from_openai(mock_event, ChatOptions(store=True), function_call_ids)
assert response.response_id == "resp_1234"
assert response.conversation_id == "conv_5678"

# store=None (default): full-history mode — conversation_id is NOT returned
response = client._parse_chunk_from_openai(mock_event, ChatOptions(), function_call_ids)
assert response.response_id == "resp_1234"
assert response.conversation_id is None


def test_streaming_annotation_added_with_file_path() -> None:
"""Streaming `file_path` should attach as a text annotation, matching non-streaming."""
Expand Down