From f83c840e9de2d7b86bac9dc70c7b520f719fc09e Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Mon, 14 Sep 2026 08:40:56 -0700 Subject: [PATCH] fix(opencode): never use a per-request trace id as the Go session id --- litellm/llms/opencode/common_utils.py | 27 ++++++- .../llms/opencode/test_opencode_chat.py | 72 +++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/litellm/llms/opencode/common_utils.py b/litellm/llms/opencode/common_utils.py index 185f35eaadc..ffe0ab53abe 100644 --- a/litellm/llms/opencode/common_utils.py +++ b/litellm/llms/opencode/common_utils.py @@ -4,6 +4,7 @@ from functools import lru_cache from types import MappingProxyType from typing import Final +from litellm.constants import SESSION_ID_GENERATED_METADATA_KEY from litellm.llms.base_llm.chat.transformation import BaseLLMException from litellm.secret_managers.main import get_secret_str @@ -201,6 +202,29 @@ def resolve_opencode_api_base(surface: str, api_base: str | None = None) -> str OPENCODE_SESSION_HEADER: Final = "x-opencode-session" +def _conversation_session_id(litellm_params: Mapping[str, object]) -> str | None: + """The caller's own session id, or None when the request carries 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. + """ + 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 + session_id: Final = litellm_params.get("litellm_session_id") + candidates: Final = ( + *(metadata.get("session_id") for metadata in metadata_maps), + litellm_params.get("session_id"), + session_id if session_id != litellm_params.get("litellm_trace_id") else None, + ) + return next((candidate for candidate in candidates if isinstance(candidate, str) and candidate), None) + + def with_opencode_session_header( surface: str, headers: Mapping[str, str], @@ -214,6 +238,5 @@ def with_opencode_session_header( """ if surface != "go" or any(name.lower() == OPENCODE_SESSION_HEADER for name in headers): return {**headers} # mutable-ok: request handlers keep mutating the headers they are given - known: Final = litellm_params.get("litellm_session_id") or litellm_params.get("litellm_trace_id") - session_id: Final = known if isinstance(known, str) else str(uuid.uuid4()) + session_id: Final = _conversation_session_id(litellm_params) or str(uuid.uuid4()) return {**headers, OPENCODE_SESSION_HEADER: session_id} # mutable-ok: request handlers keep mutating the headers diff --git a/tests/test_litellm/llms/opencode/test_opencode_chat.py b/tests/test_litellm/llms/opencode/test_opencode_chat.py index 5e925a0cf6f..0029ede171e 100644 --- a/tests/test_litellm/llms/opencode/test_opencode_chat.py +++ b/tests/test_litellm/llms/opencode/test_opencode_chat.py @@ -15,6 +15,7 @@ from httpx import Response import litellm import pytest +from litellm.constants import SESSION_ID_GENERATED_METADATA_KEY from litellm.llms.openai.chat.gpt_transformation import ( OpenAIChatCompletionStreamingHandler, ) @@ -362,6 +363,77 @@ class TestMockedCompletion: assert uuid.UUID(respx_mock.calls[0].request.headers["x-opencode-session"]).version == 4 + def test_go_chat_uses_session_id_from_metadata(self, respx_mock, monkeypatch): + """A session id sent in request metadata keeps the Go session stable across turns.""" + respx_mock.post("https://opencode.ai/zen/go/v1/chat/completions").mock( + return_value=Response(200, json=_make_response("deepseek-v4-flash", "ok")) + ) + + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + litellm.completion( + model="opencode_go/deepseek-v4-flash", + messages=[{"role": "user", "content": "hi"}], + api_key="sk-fake", + metadata={"session_id": "conv-1"}, + ) + + assert respx_mock.calls[0].request.headers["x-opencode-session"] == "conv-1" + + def test_go_chat_never_sends_the_per_request_trace_id(self, respx_mock, monkeypatch): + """A trace id changes every request, so sending it would break prompt caching on every turn.""" + respx_mock.post("https://opencode.ai/zen/go/v1/chat/completions").mock( + return_value=Response(200, json=_make_response("deepseek-v4-flash", "ok")) + ) + + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + litellm.completion( + model="opencode_go/deepseek-v4-flash", + messages=[{"role": "user", "content": "hi"}], + api_key="sk-fake", + litellm_trace_id="trace-123", + ) + + sent = respx_mock.calls[0].request.headers["x-opencode-session"] + assert sent != "trace-123" + assert uuid.UUID(sent).version == 4 + + def test_go_chat_never_sends_a_trace_id_from_metadata(self, respx_mock, monkeypatch): + """A metadata trace id also stands in for a missing session id, but it is still per request.""" + respx_mock.post("https://opencode.ai/zen/go/v1/chat/completions").mock( + return_value=Response(200, json=_make_response("deepseek-v4-flash", "ok")) + ) + + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + litellm.completion( + model="opencode_go/deepseek-v4-flash", + messages=[{"role": "user", "content": "hi"}], + api_key="sk-fake", + metadata={"trace_id": "trace-abc"}, + ) + + sent = respx_mock.calls[0].request.headers["x-opencode-session"] + assert sent != "trace-abc" + assert uuid.UUID(sent).version == 4 + + def test_go_chat_ignores_a_session_id_the_proxy_generated(self, respx_mock, monkeypatch): + """The proxy fills a missing session id with the per-request trace id, which is no stable session.""" + respx_mock.post("https://opencode.ai/zen/go/v1/chat/completions").mock( + return_value=Response(200, json=_make_response("deepseek-v4-flash", "ok")) + ) + + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + litellm.completion( + model="opencode_go/deepseek-v4-flash", + messages=[{"role": "user", "content": "hi"}], + api_key="sk-fake", + litellm_session_id="trace-123", + metadata={"session_id": "trace-123", SESSION_ID_GENERATED_METADATA_KEY: True}, + ) + + sent = respx_mock.calls[0].request.headers["x-opencode-session"] + assert sent != "trace-123" + assert uuid.UUID(sent).version == 4 + 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(