From 7fba675e8aa1be8cbe28772ce04b903023063fbb Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Sun, 29 Sep 2024 19:28:52 -0700 Subject: [PATCH] sdk-py: Add arg to get history for subgraphs --- libs/sdk-py/langgraph_sdk/client.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/libs/sdk-py/langgraph_sdk/client.py b/libs/sdk-py/langgraph_sdk/client.py index a75305479..9fe8a6d7e 100644 --- a/libs/sdk-py/langgraph_sdk/client.py +++ b/libs/sdk-py/langgraph_sdk/client.py @@ -1031,17 +1031,20 @@ async def update_state( async def get_history( self, thread_id: str, + *, limit: int = 10, - before: Optional[str] = None, + before: Optional[str | Checkpoint] = None, metadata: Optional[dict] = None, + checkpoint: Optional[Checkpoint] = None, ) -> list[ThreadState]: """Get the state history of a thread. Args: thread_id: The ID of the thread to get the state of. + checkpoint: Get history for this subgraph. If empty defaults to root. limit: The maximum number of results to return. - before: Thread timestamp to get history before. - metadata: The metadata of the thread history to get. + before: Get history before this checkpoint. + metadata: Filter checkpoints by metadata. Returns: list[ThreadState]: the state history of the thread. @@ -1051,8 +1054,6 @@ async def get_history( thread_state = await client.threads.get_history( thread_id="my_thread_id", limit=5, - before="my_timestamp", - metadata={"name":"my_name"} ) """ # noqa: E501 @@ -1063,6 +1064,8 @@ async def get_history( payload["before"] = before if metadata: payload["metadata"] = metadata + if checkpoint: + payload["checkpoint"] = checkpoint return await self.http.post(f"/threads/{thread_id}/history", json=payload) @@ -2994,17 +2997,20 @@ def update_state( def get_history( self, thread_id: str, + *, limit: int = 10, - before: Optional[str] = None, + before: Optional[str | Checkpoint] = None, metadata: Optional[dict] = None, + checkpoint: Optional[Checkpoint] = None, ) -> list[ThreadState]: """Get the state history of a thread. Args: thread_id: The ID of the thread to get the state of. + checkpoint: Get history for this subgraph. If empty defaults to root. limit: The maximum number of results to return. - before: Thread timestamp to get history before. - metadata: The metadata of the thread history to get. + before: Get history before this checkpoint. + metadata: Filter checkpoints by metadata. Returns: list[ThreadState]: the state history of the thread. @@ -3026,6 +3032,8 @@ def get_history( payload["before"] = before if metadata: payload["metadata"] = metadata + if checkpoint: + payload["checkpoint"] = checkpoint return self.http.post(f"/threads/{thread_id}/history", json=payload)