@@ -84,11 +84,6 @@ class _SystemPromptFromContext(AgentMiddleware[Any, TFAContext]):
8484 location context (city/state) from the agent state, mirroring what
8585 the web app does via prepare_system_prompt().
8686
87- Note: direct attribute assignment to ModelRequest.system_message is
88- deprecated by langchain in favour of request.override(), but override()
89- triggers execution_info patching that crashes in LangSmith Studio before
90- a run context exists. Direct mutation is the correct approach here until
91- the library resolves that bug.
9287 """
9388
9489 def _build (self , request : ModelRequest [TFAContext ]) -> SystemMessage :
@@ -108,16 +103,14 @@ def wrap_model_call(
108103 request : ModelRequest [TFAContext ],
109104 handler : Callable [[ModelRequest [TFAContext ]], ModelResponse ],
110105 ) -> ModelResponse :
111- request .system_message = self ._build (request ) # type: ignore[misc]
112- return handler (request )
106+ return handler (request .override (system_message = self ._build (request )))
113107
114108 async def awrap_model_call (
115109 self ,
116110 request : ModelRequest [TFAContext ],
117111 handler : Callable [[ModelRequest [TFAContext ]], Awaitable [ModelResponse ]],
118112 ) -> ModelResponse :
119- request .system_message = self ._build (request ) # type: ignore[misc]
120- return await handler (request )
113+ return await handler (request .override (system_message = self ._build (request )))
121114
122115
123116def _build_system_message (
@@ -163,12 +156,15 @@ def create_graph(
163156 )
164157
165158 # LangGraph deployment path: middleware injects the prompt from
166- # Studio's editable configuration panel.
159+ # Studio's editable configuration panel. context_schema is NOT set here
160+ # because this graph runs as a subgraph inside graph() — the outer graph
161+ # owns the context and propagates it. Declaring it on both levels causes
162+ # LangSmith to patch execution_info during __start__ before a run context
163+ # exists.
167164 return create_agent (
168165 model ,
169166 tools ,
170167 middleware = [_SystemPromptFromContext ()],
171- context_schema = TFAContext ,
172168 state_schema = TFAAgentStateSchema ,
173169 checkpointer = checkpointer ,
174170 )
0 commit comments