Skip to content

Commit e8b32ca

Browse files
authored
Python: Prefer runtime kwargs for conversation_id in OpenAI Responses client (#3312)
* prefer kwargs conversation_id over options * addressed comments
1 parent e229dfa commit e8b32ca

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

python/packages/core/agent_framework/openai/_responses_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,12 @@ def _check_model_presence(self, options: dict[str, Any]) -> None:
590590
options["model"] = self.model_id
591591

592592
def _get_current_conversation_id(self, options: dict[str, Any], **kwargs: Any) -> str | None:
593-
"""Get the current conversation ID from options dict or kwargs."""
594-
return options.get("conversation_id") or kwargs.get("conversation_id")
593+
"""Get the current conversation ID, preferring kwargs over options.
594+
595+
This ensures runtime-updated conversation IDs (for example, from tool execution
596+
loops) take precedence over the initial configuration provided in options.
597+
"""
598+
return kwargs.get("conversation_id") or options.get("conversation_id")
595599

596600
def _prepare_messages_for_openai(self, chat_messages: Sequence[ChatMessage]) -> list[dict[str, Any]]:
597601
"""Prepare the chat messages for a request.

python/packages/core/tests/openai/test_openai_responses_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,20 @@ async def test_prepare_options_store_parameter_handling() -> None:
21142114
assert "previous_response_id" not in options
21152115

21162116

2117+
async def test_conversation_id_precedence_kwargs_over_options() -> None:
2118+
"""When both kwargs and options contain conversation_id, kwargs wins."""
2119+
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
2120+
messages = [ChatMessage(role="user", text="Hello")]
2121+
2122+
# options has a stale response id, kwargs carries the freshest one
2123+
opts = {"conversation_id": "resp_old_123"}
2124+
run_opts = await client._prepare_options(messages, opts, conversation_id="resp_new_456") # type: ignore
2125+
2126+
# Verify kwargs takes precedence and maps to previous_response_id for resp_* IDs
2127+
assert run_opts.get("previous_response_id") == "resp_new_456"
2128+
assert "conversation" not in run_opts
2129+
2130+
21172131
def test_with_callable_api_key() -> None:
21182132
"""Test OpenAIResponsesClient initialization with callable API key."""
21192133

0 commit comments

Comments
 (0)