From 998be75f3430d87f5fa361c790b41ae8314fc64e Mon Sep 17 00:00:00 2001 From: vbarda Date: Mon, 5 May 2025 20:36:04 -0400 Subject: [PATCH] langgraph: decouple name from assistant ID in RemoteGraph --- libs/langgraph/langgraph/pregel/remote.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/libs/langgraph/langgraph/pregel/remote.py b/libs/langgraph/langgraph/pregel/remote.py index 2707a7997..72b2afc20 100644 --- a/libs/langgraph/langgraph/pregel/remote.py +++ b/libs/langgraph/langgraph/pregel/remote.py @@ -93,11 +93,12 @@ class RemoteGraph(PregelProtocol): a node in another `Graph`. """ - name: str + assistant_id: str + name: Optional[str] def __init__( self, - name: str, # graph_id + assistant_id: str, # graph_id /, *, url: Optional[str] = None, @@ -106,6 +107,7 @@ class RemoteGraph(PregelProtocol): client: Optional[LangGraphClient] = None, sync_client: Optional[SyncLangGraphClient] = None, config: Optional[RunnableConfig] = None, + name: Optional[str] = None, ): """Specify `url`, `api_key`, and/or `headers` to create default sync and async clients. @@ -114,14 +116,18 @@ class RemoteGraph(PregelProtocol): one of `url`, `client`, or `sync_client` must be provided. Args: - name: The name of the graph. + assistant_id: The assistant ID or graph name of the remote graph to use. url: The URL of the remote API. api_key: The API key to use for authentication. If not provided, it will be read from the environment (`LANGGRAPH_API_KEY`, `LANGSMITH_API_KEY`, or `LANGCHAIN_API_KEY`). headers: Additional headers to include in the requests. client: A `LangGraphClient` instance to use instead of creating a default client. sync_client: A `SyncLangGraphClient` instance to use instead of creating a default client. config: An optional `RunnableConfig` instance with additional configuration. + name: Human-readable name to attach to the RemoteGraph instance. + Will be accessible via `RemoteGraph.name`. This is useful for adding `RemoteGraph` + as a subgraph via `graph.add_node(remote_graph)`. """ + self.assistant_id = assistant_id self.name = name self.config = config @@ -149,7 +155,7 @@ class RemoteGraph(PregelProtocol): def copy(self, update: dict[str, Any]) -> Self: attrs = {**self.__dict__, **update} - return self.__class__(attrs.pop("name"), **attrs) + return self.__class__(attrs.pop("assistant_id"), **attrs) def with_config( self, config: Optional[RunnableConfig] = None, **kwargs: Any @@ -203,7 +209,7 @@ class RemoteGraph(PregelProtocol): """ sync_client = self._validate_sync_client() graph = sync_client.assistants.get_graph( - assistant_id=self.name, + assistant_id=self.assistant_id, xray=xray, ) return DrawableGraph( @@ -232,7 +238,7 @@ class RemoteGraph(PregelProtocol): """ client = self._validate_client() graph = await client.assistants.get_graph( - assistant_id=self.name, + assistant_id=self.assistant_id, xray=xray, ) return DrawableGraph( @@ -642,7 +648,7 @@ class RemoteGraph(PregelProtocol): for chunk in sync_client.runs.stream( thread_id=sanitized_config["configurable"].get("thread_id"), - assistant_id=self.name, + assistant_id=self.assistant_id, input=input, command=command, config=sanitized_config, @@ -737,7 +743,7 @@ class RemoteGraph(PregelProtocol): async for chunk in client.runs.stream( thread_id=sanitized_config["configurable"].get("thread_id"), - assistant_id=self.name, + assistant_id=self.assistant_id, input=input, command=command, config=sanitized_config,