Skip to content

Commit fd82497

Browse files
authored
Feat/support lg 06 context (#372)
* feat: provide backwards compatibility for the new runtime context property
1 parent 5585025 commit fd82497

File tree

5 files changed

+66
-17
lines changed

5 files changed

+66
-17
lines changed

typescript-sdk/integrations/langgraph/examples/python/poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typescript-sdk/integrations/langgraph/examples/python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ langchain-experimental = ">=0.0.11"
2121
langchain-google-genai = ">=2.1.9"
2222
langchain-openai = ">=0.0.1"
2323
langgraph = "^0.6.1"
24-
ag-ui-langgraph = { version = "0.0.10", extras = ["fastapi"] }
24+
ag-ui-langgraph = { version = "0.0.12a1", extras = ["fastapi"] }
2525
python-dotenv = "^1.0.0"
2626
fastapi = "^0.115.12"
2727

typescript-sdk/integrations/langgraph/python/ag_ui_langgraph/agent.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import uuid
22
import json
3-
from typing import Optional, List, Any, Union, AsyncGenerator, Generator
3+
from typing import Optional, List, Any, Union, AsyncGenerator, Generator, Literal, Dict
4+
import inspect
45

56
from langgraph.graph.state import CompiledStateGraph
67
from langchain.schema import BaseMessage, SystemMessage
@@ -335,13 +336,15 @@ async def prepare_stream(self, input: RunAgentInput, agent_state: State, config:
335336

336337
subgraphs_stream_enabled = input.forwarded_props.get('stream_subgraphs') if input.forwarded_props else False
337338

338-
stream = self.graph.astream_events(
339-
stream_input,
339+
kwargs = self.get_stream_kwargs(
340+
input=stream_input,
340341
config=config,
341-
subgraps=bool(subgraphs_stream_enabled),
342-
version="v2"
342+
subgraphs=bool(subgraphs_stream_enabled),
343+
version="v2",
343344
)
344345

346+
stream = self.graph.astream_events(**kwargs)
347+
345348
return {
346349
"stream": stream,
347350
"state": state,
@@ -369,12 +372,14 @@ async def prepare_regenerate_stream( # pylint: disable=too-many-arguments
369372

370373
stream_input = self.langgraph_default_merge_state(time_travel_checkpoint.values, [message_checkpoint], input)
371374
subgraphs_stream_enabled = input.forwarded_props.get('stream_subgraphs') if input.forwarded_props else False
372-
stream = self.graph.astream_events(
373-
stream_input,
374-
fork,
375-
subgraps=bool(subgraphs_stream_enabled),
376-
version="v2"
375+
376+
kwargs = self.get_stream_kwargs(
377+
input=stream_input,
378+
fork=fork,
379+
subgraphs=bool(subgraphs_stream_enabled),
380+
version="v2",
377381
)
382+
stream = self.graph.astream_events(**kwargs)
378383

379384
return {
380385
"stream": stream,
@@ -401,17 +406,25 @@ def get_schema_keys(self, config) -> SchemaKeys:
401406
input_schema_keys = list(input_schema["properties"].keys()) if "properties" in input_schema else []
402407
output_schema_keys = list(output_schema["properties"].keys()) if "properties" in output_schema else []
403408
config_schema_keys = list(config_schema["properties"].keys()) if "properties" in config_schema else []
409+
context_schema_keys = []
410+
411+
if hasattr(self.graph, "context_schema") and self.graph.context_schema is not None:
412+
context_schema = self.graph.context_schema().schema()
413+
context_schema_keys = list(context_schema["properties"].keys()) if "properties" in context_schema else []
414+
404415

405416
return {
406417
"input": [*input_schema_keys, *self.constant_schema_keys],
407418
"output": [*output_schema_keys, *self.constant_schema_keys],
408419
"config": config_schema_keys,
420+
"context": context_schema_keys,
409421
}
410422
except Exception:
411423
return {
412424
"input": self.constant_schema_keys,
413425
"output": self.constant_schema_keys,
414426
"config": [],
427+
"context": [],
415428
}
416429

417430
def langgraph_default_merge_state(self, state: State, messages: List[BaseMessage], input: RunAgentInput) -> State:
@@ -744,3 +757,38 @@ def end_step(self):
744757
self.active_run["node_name"] = None
745758
self.active_step = None
746759
return dispatch
760+
761+
# Check if some kwargs are enabled per LG version, to "catch all versions" and backwards compatibility
762+
def get_stream_kwargs(
763+
self,
764+
input: Any,
765+
subgraphs: bool = False,
766+
version: Literal["v1", "v2"] = "v2",
767+
config: Optional[RunnableConfig] = None,
768+
context: Optional[Dict[str, Any]] = None,
769+
fork: Optional[Any] = None,
770+
):
771+
kwargs = dict(
772+
input=input,
773+
subgraphs=subgraphs,
774+
version=version,
775+
)
776+
777+
# Only add context if supported
778+
sig = inspect.signature(self.graph.astream_events)
779+
if 'context' in sig.parameters:
780+
base_context = {}
781+
if isinstance(config, dict) and 'configurable' in config and isinstance(config['configurable'], dict):
782+
base_context.update(config['configurable'])
783+
if context: # context might be None or {}
784+
base_context.update(context)
785+
if base_context: # only add if there's something to pass
786+
kwargs['context'] = base_context
787+
788+
if config:
789+
kwargs['config'] = config
790+
791+
if fork:
792+
kwargs.update(fork)
793+
794+
return kwargs

typescript-sdk/integrations/langgraph/python/ag_ui_langgraph/types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class CustomEventNames(str, Enum):
2525
SchemaKeys = TypedDict("SchemaKeys", {
2626
"input": NotRequired[Optional[List[str]]],
2727
"output": NotRequired[Optional[List[str]]],
28-
"config": NotRequired[Optional[List[str]]]
28+
"config": NotRequired[Optional[List[str]]],
29+
"context": NotRequired[Optional[List[str]]]
2930
})
3031

3132
ThinkingProcess = TypedDict("ThinkingProcess", {

typescript-sdk/integrations/langgraph/python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ag-ui-langgraph"
3-
version = "0.0.11"
3+
version = "0.0.12-alpha.1"
44
description = "Implementation of the AG-UI protocol for LangGraph."
55
authors = ["Ran Shem Tov <[email protected]>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)