From 602ede4e53611c4e1faebee3e80bbc4fdbe15885 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 04a9f567b..44d3720d0 100644 --- a/backend/app/gateway/services.py +++ b/backend/app/gateway/services.py @@ -249,6 +249,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 167714ff5..596a002d0 100644 --- a/backend/tests/test_gateway_services.py +++ b/backend/tests/test_gateway_services.py @@ -641,17 +641,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