From caf54938db138458fc06ed41fcbc510828fe7c07 Mon Sep 17 00:00:00 2001 From: Vibhu Dixit Date: Tue, 23 Jun 2026 01:18:43 -0700 Subject: [PATCH] fix(gateway): propagate thread_id for LangGraph context API (#3735) --- backend/app/gateway/services.py | 1 + backend/tests/test_gateway_services.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/app/gateway/services.py b/backend/app/gateway/services.py index 0650df69f..b03b8bd79 100644 --- a/backend/app/gateway/services.py +++ b/backend/app/gateway/services.py @@ -252,6 +252,7 @@ def build_run_config( context = dict(context_value) else: raise ValueError("request config 'context' must be a mapping or null.") + context["thread_id"] = thread_id config["context"] = context else: configurable = {"thread_id": thread_id} diff --git a/backend/tests/test_gateway_services.py b/backend/tests/test_gateway_services.py index 14cfeb5f4..f47fee92c 100644 --- a/backend/tests/test_gateway_services.py +++ b/backend/tests/test_gateway_services.py @@ -705,17 +705,33 @@ def test_build_run_config_with_context(): ) assert "context" in config assert config["context"]["user_id"] == "u-42" + assert config["context"]["thread_id"] == "thread-1" assert "configurable" not in config assert config["recursion_limit"] == 100 +def test_build_run_config_context_injects_thread_id(): + from app.gateway.services import build_run_config + + config = build_run_config( + "T-deadbeef-42", + {"context": {"user_id": "u-1", "thinking_enabled": True}}, + None, + ) + + assert config["context"]["user_id"] == "u-1" + assert config["context"]["thinking_enabled"] is True + assert config["context"]["thread_id"] == "T-deadbeef-42" + assert "configurable" not in config + + def test_build_run_config_null_context_becomes_empty_context(): """When caller sends context=null, treat it as an empty context object.""" from app.gateway.services import build_run_config config = build_run_config("thread-1", {"context": None}, None) - assert config["context"] == {} + assert config["context"] == {"thread_id": "thread-1"} assert "configurable" not in config