diff --git a/libs/langgraph/package.json b/libs/langgraph/package.json index 8a8574afa..8404d4af6 100644 --- a/libs/langgraph/package.json +++ b/libs/langgraph/package.json @@ -34,6 +34,7 @@ "dependencies": { "@langchain/langgraph-checkpoint": "^0.1.1", "@langchain/langgraph-sdk": "~0.1.0", + "@standard-schema/spec": "^1.0.0", "uuid": "^10.0.0" }, "peerDependencies": { diff --git a/libs/langgraph/src/graph/annotation.ts b/libs/langgraph/src/graph/annotation.ts index 925dd69c0..52675c8a2 100644 --- a/libs/langgraph/src/graph/annotation.ts +++ b/libs/langgraph/src/graph/annotation.ts @@ -1,3 +1,4 @@ +import type { StandardSchemaV1 } from "@standard-schema/spec"; import { RunnableLike } from "../pregel/runnable_types.js"; import { BaseChannel } from "../channels/base.js"; import { BinaryOperator, BinaryOperatorAggregate } from "../channels/binop.js"; @@ -5,6 +6,15 @@ import { LastValue } from "../channels/last_value.js"; export type SingleReducer = | { + state?: StandardSchemaV1; + update?: StandardSchemaV1; + + jsonSchemaExtra?: { + langgraph_nodes?: string[]; + langgraph_type?: "prompt" | "messages"; + [key: string]: unknown; + }; + reducer: BinaryOperator; default?: () => ValueType; } @@ -18,19 +28,23 @@ export type SingleReducer = | null; export interface StateDefinition { - [key: string]: BaseChannel | (() => BaseChannel); + [key: string]: BaseChannel | (() => BaseChannel) | StandardSchemaV1; } type ExtractValueType = C extends BaseChannel ? C["ValueType"] : C extends () => BaseChannel ? ReturnType["ValueType"] + : C extends StandardSchemaV1 + ? StandardSchemaV1.InferOutput : never; type ExtractUpdateType = C extends BaseChannel ? C["UpdateType"] : C extends () => BaseChannel ? ReturnType["UpdateType"] + : C extends StandardSchemaV1 + ? StandardSchemaV1.InferInput : never; export type StateType = { diff --git a/libs/langgraph/src/graph/state.ts b/libs/langgraph/src/graph/state.ts index 188178659..1fe47f91b 100644 --- a/libs/langgraph/src/graph/state.ts +++ b/libs/langgraph/src/graph/state.ts @@ -279,18 +279,18 @@ export class StateGraph< contextSchema?: C | AnnotationRoot> ); - /** @deprecated Use `Annotation.Root` or `zod` for state definition instead. */ constructor( - fields: SD extends StateDefinition - ? SD | StateGraphArgs - : StateGraphArgs, + fields: SD extends InteropZodObject + ? SD | ZodStateGraphArgsWithStateSchema + : never, contextSchema?: C | AnnotationRoot> ); + /** @deprecated Use `Annotation.Root` or `zod` for state definition instead. */ constructor( - fields: SD extends InteropZodObject - ? SD | ZodStateGraphArgsWithStateSchema - : never, + fields: SD extends StateDefinition + ? SD | StateGraphArgs + : StateGraphArgs, contextSchema?: C | AnnotationRoot> ); diff --git a/yarn.lock b/yarn.lock index b5084f6cd..efac311d5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2342,6 +2342,7 @@ __metadata: "@langchain/langgraph-sdk": "workspace:*" "@langchain/openai": "npm:^0.4.0" "@langchain/scripts": "npm:>=0.1.3 <0.2.0" + "@standard-schema/spec": "npm:^1.0.0" "@swc/core": "npm:^1.3.90" "@testing-library/dom": "npm:^10.4.0" "@tsconfig/recommended": "npm:^1.0.3" @@ -3542,6 +3543,13 @@ __metadata: languageName: node linkType: hard +"@standard-schema/spec@npm:^1.0.0": + version: 1.0.0 + resolution: "@standard-schema/spec@npm:1.0.0" + checksum: 10/aee780cc1431888ca4b9aba9b24ffc8f3073fc083acc105e3951481478a2f4dc957796931b2da9e2d8329584cf211e4542275f188296c1cdff3ed44fd93a8bc8 + languageName: node + linkType: hard + "@swc/core-darwin-arm64@npm:1.4.16": version: 1.4.16 resolution: "@swc/core-darwin-arm64@npm:1.4.16"