From 0192faa6bd68eea6319cd7f9bca856d1240c89b4 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Mon, 14 Sep 2026 10:33:25 -0700 Subject: [PATCH] fix(opencode): skip only the metadata map the proxy marked as generated --- litellm/llms/opencode/common_utils.py | 12 ++++++++---- .../llms/opencode/test_opencode_chat.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/litellm/llms/opencode/common_utils.py b/litellm/llms/opencode/common_utils.py index ffe0ab53abe..0613b210c52 100644 --- a/litellm/llms/opencode/common_utils.py +++ b/litellm/llms/opencode/common_utils.py @@ -208,19 +208,23 @@ def _conversation_session_id(litellm_params: Mapping[str, object]) -> str | None Trace ids and session ids the proxy generated for a session-less request are per request, so they are never used: they would change the session every turn. A ``litellm_session_id`` equal to the trace id was derived from it, not sent. + Only the metadata map marked as generated is skipped, so a session the caller + sent elsewhere still wins. """ metadata_maps: Final = tuple( metadata for metadata in (litellm_params.get("metadata"), litellm_params.get("litellm_metadata")) if isinstance(metadata, Mapping) ) - if any(metadata.get(SESSION_ID_GENERATED_METADATA_KEY) for metadata in metadata_maps): - return None + caller_maps: Final = tuple( + metadata for metadata in metadata_maps if not metadata.get(SESSION_ID_GENERATED_METADATA_KEY) + ) + proxy_generated: Final = len(caller_maps) != len(metadata_maps) session_id: Final = litellm_params.get("litellm_session_id") candidates: Final = ( - *(metadata.get("session_id") for metadata in metadata_maps), + *(metadata.get("session_id") for metadata in caller_maps), litellm_params.get("session_id"), - session_id if session_id != litellm_params.get("litellm_trace_id") else None, + session_id if not proxy_generated and session_id != litellm_params.get("litellm_trace_id") else None, ) return next((candidate for candidate in candidates if isinstance(candidate, str) and candidate), None) diff --git a/tests/test_litellm/llms/opencode/test_opencode_chat.py b/tests/test_litellm/llms/opencode/test_opencode_chat.py index 0029ede171e..4e046f1ea01 100644 --- a/tests/test_litellm/llms/opencode/test_opencode_chat.py +++ b/tests/test_litellm/llms/opencode/test_opencode_chat.py @@ -434,6 +434,22 @@ class TestMockedCompletion: assert sent != "trace-123" assert uuid.UUID(sent).version == 4 + def test_go_caller_session_survives_a_generated_marker_in_the_other_metadata(self): + """Only the metadata map the proxy marked as generated is ignored, so the caller's own session still wins.""" + from litellm.llms.opencode.common_utils import with_opencode_session_header + + headers = with_opencode_session_header( + "go", + {}, + { + "litellm_session_id": "trace-123", + "metadata": {"session_id": "conv-1"}, + "litellm_metadata": {"session_id": "trace-123", SESSION_ID_GENERATED_METADATA_KEY: True}, + }, + ) + + assert headers["x-opencode-session"] == "conv-1" + def test_zen_chat_sends_no_session_header(self, respx_mock, monkeypatch): """Only the Go surface requires the header.""" respx_mock.post("https://opencode.ai/zen/v1/chat/completions").mock(