diff --git a/docs/Makefile b/docs/Makefile index 95d256241..b43ca0b3c 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -13,10 +13,10 @@ build-prebuilt: uv run python -m _scripts.third_party_page.get_download_stats --fake stats.yml; \ set +x; \ fi - uv run python -m _scripts.third_party_page.create_third_party_page stats.yml docs/agents/prebuilt.md --language python + uv run python -m _scripts.third_party_page.create_third_party_page stats.yml docs/agents/prebuilt.md build-docs: build-prebuilt - uv run python -m mkdocs build --clean -f mkdocs.yml --strict + TARGET_LANGUAGE=python uv run python -m mkdocs build --clean -f mkdocs.yml --strict llms-text: uv run python -m _scripts.generate_llms_text docs/llms-full.txt diff --git a/docs/_scripts/handle_auto_links.py b/docs/_scripts/handle_auto_links.py index 1e2c8ad4d..5c3187a82 100644 --- a/docs/_scripts/handle_auto_links.py +++ b/docs/_scripts/handle_auto_links.py @@ -100,16 +100,15 @@ CONDITIONAL_FENCE_PATTERN = re.compile( ) CROSS_REFERENCE_PATTERN = re.compile( r""" - @ # Literal @ symbol (?: # Non-capturing group for two possible formats: - \[ # Opening bracket for title + @\[ # @ symbol followed by opening bracket for title (?P[^\]]+) # Custom title - one or more non-bracket characters \] # Closing bracket for title \[ # Opening bracket for link name (?P<link_name_with_title>[^\]]+) # Link name - one or more non-bracket characters \] # Closing bracket for link name | # OR - \[ # Opening bracket + @\[ # @ symbol followed by opening bracket (?P<link_name>[^\]]+) # Link name - one or more non-bracket characters \] # Closing bracket ) @@ -118,7 +117,7 @@ CROSS_REFERENCE_PATTERN = re.compile( ) -def _replace_autolinks(markdown: str, file_path: str) -> str: +def _replace_autolinks(markdown: str, file_path: str, *, default_scope: str = "python") -> str: """Preprocess markdown lines to handle @[links] with conditional fence scopes. This function processes markdown content to transform @[link_name] references @@ -128,6 +127,7 @@ def _replace_autolinks(markdown: str, file_path: str) -> str: Args: markdown: The markdown content to process. file_path: The file path for error reporting. + default_scope: The default scope to use if no scope is matched. Returns: Processed markdown content with @[references] transformed to proper @@ -140,7 +140,7 @@ def _replace_autolinks(markdown: str, file_path: str) -> str: "[StateGraph](url)\\n:::python\\n[Command](url)\\n:::\\n" """ # Track the current scope context - current_scope = "global" + current_scope = default_scope lines = markdown.splitlines(keepends=True) processed_lines = [] @@ -152,7 +152,7 @@ def _replace_autolinks(markdown: str, file_path: str) -> str: if fence_match: language = fence_match.group("language") # Set scope to the specified language, or reset to global if no language - current_scope = language.lower() if language else "global" + current_scope = language.lower() if language else default_scope processed_lines.append(line) continue diff --git a/docs/_scripts/add_translation.py b/docs/_scripts/js_translation/add_translation.py similarity index 69% rename from docs/_scripts/add_translation.py rename to docs/_scripts/js_translation/add_translation.py index a02812bc9..fce3ad059 100644 --- a/docs/_scripts/add_translation.py +++ b/docs/_scripts/js_translation/add_translation.py @@ -4,9 +4,11 @@ import argparse import requests from langchain_anthropic import ChatAnthropic +from textwrap import dedent + # Load reference TypeScript snippets -URL = "https://gist.githubusercontent.com/eyurtsev/e7486731415463a9bc5b4682358859c8/raw/b5a5fda9c7e3387cfcb781f25082814d43675d50/gistfile1.txt" +URL = "https://gist.githubusercontent.com/dqbd/b35d49e2ceec80e654fe1c5ab61ec477/raw/f4768aeedb67628190a4e06d063a938afc8e7672/snippets.md" response = requests.get(URL) response.raise_for_status() reference_snippets = response.text @@ -14,6 +16,80 @@ reference_snippets = response.text # Initialize model model = ChatAnthropic(model="claude-sonnet-4-0", max_tokens=64_000) + +FLUENT_INTERFACE_PROMPT = ( + "CRITICAL: Always use method chaining (fluent interface) for StateGraph operations in TypeScript. " + "Never create separate variables for the graph builder or call methods individually. " + "The fluent interface provides better type safety and is the preferred pattern.\n\n" + "CORRECT examples with fluent interface:\n" + + dedent( + """ + ```typescript + const graph = new StateGraph(MyState) + .addNode('node1', node1) + .addNode('node2', node2) + .addEdge(START, 'node1') + .addEdge('node1', 'node2') + .addEdge('node2', END) + .compile() + ``` + + ```typescript + const graph = new StateGraph(MyState) + .addNode('chatbot', chatbot) + .addEdge(START, 'chatbot') + .addEdge('chatbot', END) + .compile() + ``` + + ```typescript + const graph = new StateGraph(MyState) + .addNode('chatbot', chatbot) + .addEdge(START, 'chatbot') + .addEdge('chatbot', END) + .compile() + ``` + """ + ) + + "\n" + + "INCORRECT examples to avoid:\n" + + dedent( + """ + ```typescript + // WRONG: Creating separate builder variable + const graphBuilder = new StateGraph(MyState) + graphBuilder.addNode('node1', node1) + graphBuilder.addEdge(START, 'node1') + const graph = graphBuilder.compile() + ``` + + ```typescript + // WRONG: Using Python-style method names + const workflow = new StateGraph(MyState) + workflow.add_node('node1', node1) + workflow.add_edge(START, 'node1') + const graph = workflow.compile() + ``` + + ```typescript + // WRONG: Calling methods individually + const graphBuilder = new StateGraph(MyState) + graphBuilder.addNode('chatbot', chatbot) + graphBuilder.addEdge(START, 'chatbot') + graphBuilder.addEdge('chatbot', END) + const graph = graphBuilder.compile() + ``` + """ + ) + + "\n" + + "Key rules:\n" + + "- Always chain methods directly on the StateGraph constructor\n" + + "- Use camelCase method names (addNode, addEdge, not add_node, add_edge)\n" + + "- Always end with .compile()\n" + + "- Never store the builder in a separate variable\n" +) + + TRANSLATION_PROMPT = ( "You are a helpful assistant that translates Python-based technical " "documentation written in Markdown to equivalent TypeScript-based documentation. " @@ -32,6 +108,12 @@ TRANSLATION_PROMPT = ( "the translation. " "Use the reference TypeScript snippets as guidance whenever possible to " "maintain alignment with existing conventions.\n\n" + "IMPORTANT REQUIREMENTS:\n" + "- Use Zod for state definition for StateGraph. Avoid using Annotation since it will be deprecated in the future.\n" + "- ALWAYS use fluent interface (method chaining) for StateGraph operations - this is CRITICAL\n" + "- Never create separate variables for graph builders\n" + "- Always chain methods directly on the StateGraph constructor and end with .compile()\n\n" + f"{FLUENT_INTERFACE_PROMPT}\n\n" f"Here are the reference TypeScript snippets:\n\n{reference_snippets}\n\n" ) diff --git a/docs/_scripts/js_translation/codeblocks/.gitkeep b/docs/_scripts/js_translation/codeblocks/.gitkeep new file mode 100644 index 000000000..15b34cb41 --- /dev/null +++ b/docs/_scripts/js_translation/codeblocks/.gitkeep @@ -0,0 +1,6 @@ +.prettierrc +.eslint.config.mjs +package.json +README.md +tsconfig.json +yarn.lock \ No newline at end of file diff --git a/docs/_scripts/js_translation/codeblocks/.prettierrc b/docs/_scripts/js_translation/codeblocks/.prettierrc new file mode 100644 index 000000000..74c13fdf2 --- /dev/null +++ b/docs/_scripts/js_translation/codeblocks/.prettierrc @@ -0,0 +1,19 @@ +{ + "$schema": "https://json.schemastore.org/prettierrc", + "printWidth": 80, + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": false, + "quoteProps": "as-needed", + "jsxSingleQuote": false, + "trailingComma": "es5", + "bracketSpacing": true, + "arrowParens": "always", + "requirePragma": false, + "insertPragma": false, + "proseWrap": "preserve", + "htmlWhitespaceSensitivity": "css", + "vueIndentScriptAndStyle": false, + "endOfLine": "lf" +} \ No newline at end of file diff --git a/docs/_scripts/js_translation/codeblocks/README.md b/docs/_scripts/js_translation/codeblocks/README.md new file mode 100644 index 000000000..8160c6d4c --- /dev/null +++ b/docs/_scripts/js_translation/codeblocks/README.md @@ -0,0 +1 @@ +# \_codeblocks diff --git a/docs/_scripts/js_translation/codeblocks/eslint.config.mjs b/docs/_scripts/js_translation/codeblocks/eslint.config.mjs new file mode 100644 index 000000000..a385535f0 --- /dev/null +++ b/docs/_scripts/js_translation/codeblocks/eslint.config.mjs @@ -0,0 +1,14 @@ +import js from "@eslint/js"; +import globals from "globals"; +import tseslint from "typescript-eslint"; +import { defineConfig } from "eslint/config"; + +export default defineConfig([ + { + files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], + plugins: { js }, + extends: ["js/recommended"], + languageOptions: { globals: globals.browser }, + }, + tseslint.configs.recommended, +]); diff --git a/docs/_scripts/js_translation/codeblocks/package.json b/docs/_scripts/js_translation/codeblocks/package.json new file mode 100644 index 000000000..bb92504b7 --- /dev/null +++ b/docs/_scripts/js_translation/codeblocks/package.json @@ -0,0 +1,27 @@ +{ + "name": "_codeblocks", + "packageManager": "yarn@4.6.0", + "scripts": { + "lint": "eslint .", + "lint:fix": "eslint . --fix", + "format": "prettier --write .", + "format:fix": "prettier --write . --fix" + }, + "dependencies": { + "@langchain/anthropic": "^0.3.24", + "@langchain/core": "^0.3.66", + "@langchain/langgraph": "^0.3.11", + "@langchain/langgraph-api": "^0.0.52", + "@langchain/langgraph-sdk": "^0.0.102", + "@langchain/openai": "^0.6.3", + "zod": "^4.0.10" + }, + "devDependencies": { + "@eslint/js": "^9.32.0", + "eslint": "^9.32.0", + "globals": "^16.3.0", + "jiti": "^2.5.1", + "typescript": "^5.8.3", + "typescript-eslint": "^8.38.0" + } +} diff --git a/docs/_scripts/js_translation/codeblocks/tsconfig.json b/docs/_scripts/js_translation/codeblocks/tsconfig.json new file mode 100644 index 000000000..e32c6fdb5 --- /dev/null +++ b/docs/_scripts/js_translation/codeblocks/tsconfig.json @@ -0,0 +1,114 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "libReplacement": true, /* Enable lib replacement. */ + // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "nodenext", /* Specify what module code is generated. */ + // "rootDir": "./", /* Specify the root folder within your source files. */ + "moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "noUncheckedSideEffectImports": true, /* Check side effect imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */ + // "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + + /* Type Checking */ + "strict": false, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + "" + } +} diff --git a/docs/_scripts/js_translation/codeblocks/yarn.lock b/docs/_scripts/js_translation/codeblocks/yarn.lock new file mode 100644 index 000000000..0c6068c46 --- /dev/null +++ b/docs/_scripts/js_translation/codeblocks/yarn.lock @@ -0,0 +1,3770 @@ +# This file is generated by running "yarn install" inside your project. +# Manual changes might be lost - proceed with caution! + +__metadata: + version: 8 + cacheKey: 10c0 + +"@alloc/quick-lru@npm:^5.2.0": + version: 5.2.0 + resolution: "@alloc/quick-lru@npm:5.2.0" + checksum: 10c0/7b878c48b9d25277d0e1a9b8b2f2312a314af806b4129dc902f2bc29ab09b58236e53964689feec187b28c80d2203aff03829754773a707a8a5987f1b7682d92 + languageName: node + linkType: hard + +"@ampproject/remapping@npm:^2.3.0": + version: 2.3.0 + resolution: "@ampproject/remapping@npm:2.3.0" + dependencies: + "@jridgewell/gen-mapping": "npm:^0.3.5" + "@jridgewell/trace-mapping": "npm:^0.3.24" + checksum: 10c0/81d63cca5443e0f0c72ae18b544cc28c7c0ec2cea46e7cb888bb0e0f411a1191d0d6b7af798d54e30777d8d1488b2ec0732aac2be342d3d7d3ffd271c6f489ed + languageName: node + linkType: hard + +"@anthropic-ai/sdk@npm:^0.56.0": + version: 0.56.0 + resolution: "@anthropic-ai/sdk@npm:0.56.0" + bin: + anthropic-ai-sdk: bin/cli + checksum: 10c0/b8506daa740b3700c56cf7e7cd16c5f3c092b96ad0bca893530d3e12ed543bdae174ea5e34b270ba86958f193a8ac31559f17ed4a79ba9219771d3c457e15c06 + languageName: node + linkType: hard + +"@babel/code-frame@npm:^7.26.2": + version: 7.27.1 + resolution: "@babel/code-frame@npm:7.27.1" + dependencies: + "@babel/helper-validator-identifier": "npm:^7.27.1" + js-tokens: "npm:^4.0.0" + picocolors: "npm:^1.1.1" + checksum: 10c0/5dd9a18baa5fce4741ba729acc3a3272c49c25cb8736c4b18e113099520e7ef7b545a4096a26d600e4416157e63e87d66db46aa3fbf0a5f2286da2705c12da00 + languageName: node + linkType: hard + +"@babel/helper-validator-identifier@npm:^7.27.1": + version: 7.27.1 + resolution: "@babel/helper-validator-identifier@npm:7.27.1" + checksum: 10c0/c558f11c4871d526498e49d07a84752d1800bf72ac0d3dad100309a2eaba24efbf56ea59af5137ff15e3a00280ebe588560534b0e894a4750f8b1411d8f78b84 + languageName: node + linkType: hard + +"@cfworker/json-schema@npm:^4.0.2": + version: 4.1.1 + resolution: "@cfworker/json-schema@npm:4.1.1" + checksum: 10c0/b5253486d346b7de6feec9c73954f612b11019dacb9023d710a5666df2f5fc145dd88b6b913c88726c6d97e2e258a515fa2cab177f58b18da6bac3738cbc4739 + languageName: node + linkType: hard + +"@colors/colors@npm:1.6.0, @colors/colors@npm:^1.6.0": + version: 1.6.0 + resolution: "@colors/colors@npm:1.6.0" + checksum: 10c0/9328a0778a5b0db243af54455b79a69e3fb21122d6c15ef9e9fcc94881d8d17352d8b2b2590f9bdd46fac5c2d6c1636dcfc14358a20c70e22daf89e1a759b629 + languageName: node + linkType: hard + +"@commander-js/extra-typings@npm:^13.0.0": + version: 13.1.0 + resolution: "@commander-js/extra-typings@npm:13.1.0" + peerDependencies: + commander: ~13.1.0 + checksum: 10c0/ff799f0641f68855aa73c976912a607c25d564df34fd8e262927a80b19f6cccd882fe7ce098a0e072a497fd0020cbd19fd1e4d5cc98a461cd6623abf8ed5f4e7 + languageName: node + linkType: hard + +"@dabh/diagnostics@npm:^2.0.2": + version: 2.0.3 + resolution: "@dabh/diagnostics@npm:2.0.3" + dependencies: + colorspace: "npm:1.1.x" + enabled: "npm:2.0.x" + kuler: "npm:^2.0.0" + checksum: 10c0/a5133df8492802465ed01f2f0a5784585241a1030c362d54a602ed1839816d6c93d71dde05cf2ddb4fd0796238c19774406bd62fa2564b637907b495f52425fe + languageName: node + linkType: hard + +"@emnapi/core@npm:^1.4.3": + version: 1.4.5 + resolution: "@emnapi/core@npm:1.4.5" + dependencies: + "@emnapi/wasi-threads": "npm:1.0.4" + tslib: "npm:^2.4.0" + checksum: 10c0/da4a57f65f325d720d0e0d1a9c6618b90c4c43a5027834a110476984e1d47c95ebaed4d316b5dddb9c0ed9a493ffeb97d1934f9677035f336d8a36c1f3b2818f + languageName: node + linkType: hard + +"@emnapi/runtime@npm:^1.4.3": + version: 1.4.5 + resolution: "@emnapi/runtime@npm:1.4.5" + dependencies: + tslib: "npm:^2.4.0" + checksum: 10c0/37a0278be5ac81e918efe36f1449875cbafba947039c53c65a1f8fc238001b866446fc66041513b286baaff5d6f9bec667f5164b3ca481373a8d9cb65bfc984b + languageName: node + linkType: hard + +"@emnapi/wasi-threads@npm:1.0.4, @emnapi/wasi-threads@npm:^1.0.2": + version: 1.0.4 + resolution: "@emnapi/wasi-threads@npm:1.0.4" + dependencies: + tslib: "npm:^2.4.0" + checksum: 10c0/2c91a53e62f875800baf035c4d42c9c0d18e5afd9a31ca2aac8b435aeaeaeaac386b5b3d0d0e70aa7a5a9852bbe05106b1f680cd82cce03145c703b423d41313 + languageName: node + linkType: hard + +"@esbuild/aix-ppc64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/aix-ppc64@npm:0.25.8" + conditions: os=aix & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/android-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/android-arm64@npm:0.25.8" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/android-arm@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/android-arm@npm:0.25.8" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + +"@esbuild/android-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/android-x64@npm:0.25.8" + conditions: os=android & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/darwin-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/darwin-arm64@npm:0.25.8" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/darwin-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/darwin-x64@npm:0.25.8" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/freebsd-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/freebsd-arm64@npm:0.25.8" + conditions: os=freebsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/freebsd-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/freebsd-x64@npm:0.25.8" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/linux-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-arm64@npm:0.25.8" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/linux-arm@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-arm@npm:0.25.8" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@esbuild/linux-ia32@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-ia32@npm:0.25.8" + conditions: os=linux & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/linux-loong64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-loong64@npm:0.25.8" + conditions: os=linux & cpu=loong64 + languageName: node + linkType: hard + +"@esbuild/linux-mips64el@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-mips64el@npm:0.25.8" + conditions: os=linux & cpu=mips64el + languageName: node + linkType: hard + +"@esbuild/linux-ppc64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-ppc64@npm:0.25.8" + conditions: os=linux & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/linux-riscv64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-riscv64@npm:0.25.8" + conditions: os=linux & cpu=riscv64 + languageName: node + linkType: hard + +"@esbuild/linux-s390x@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-s390x@npm:0.25.8" + conditions: os=linux & cpu=s390x + languageName: node + linkType: hard + +"@esbuild/linux-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/linux-x64@npm:0.25.8" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/netbsd-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/netbsd-arm64@npm:0.25.8" + conditions: os=netbsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/netbsd-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/netbsd-x64@npm:0.25.8" + conditions: os=netbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/openbsd-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/openbsd-arm64@npm:0.25.8" + conditions: os=openbsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/openbsd-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/openbsd-x64@npm:0.25.8" + conditions: os=openbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/openharmony-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/openharmony-arm64@npm:0.25.8" + conditions: os=openharmony & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/sunos-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/sunos-x64@npm:0.25.8" + conditions: os=sunos & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/win32-arm64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/win32-arm64@npm:0.25.8" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/win32-ia32@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/win32-ia32@npm:0.25.8" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/win32-x64@npm:0.25.8": + version: 0.25.8 + resolution: "@esbuild/win32-x64@npm:0.25.8" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + +"@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.7.0": + version: 4.7.0 + resolution: "@eslint-community/eslint-utils@npm:4.7.0" + dependencies: + eslint-visitor-keys: "npm:^3.4.3" + peerDependencies: + eslint: ^6.0.0 || ^7.0.0 || >=8.0.0 + checksum: 10c0/c0f4f2bd73b7b7a9de74b716a664873d08ab71ab439e51befe77d61915af41a81ecec93b408778b3a7856185244c34c2c8ee28912072ec14def84ba2dec70adf + languageName: node + linkType: hard + +"@eslint-community/regexpp@npm:^4.10.0, @eslint-community/regexpp@npm:^4.12.1": + version: 4.12.1 + resolution: "@eslint-community/regexpp@npm:4.12.1" + checksum: 10c0/a03d98c246bcb9109aec2c08e4d10c8d010256538dcb3f56610191607214523d4fb1b00aa81df830b6dffb74c5fa0be03642513a289c567949d3e550ca11cdf6 + languageName: node + linkType: hard + +"@eslint/config-array@npm:^0.21.0": + version: 0.21.0 + resolution: "@eslint/config-array@npm:0.21.0" + dependencies: + "@eslint/object-schema": "npm:^2.1.6" + debug: "npm:^4.3.1" + minimatch: "npm:^3.1.2" + checksum: 10c0/0ea801139166c4aa56465b309af512ef9b2d3c68f9198751bbc3e21894fe70f25fbf26e1b0e9fffff41857bc21bfddeee58649ae6d79aadcd747db0c5dca771f + languageName: node + linkType: hard + +"@eslint/config-helpers@npm:^0.3.0": + version: 0.3.0 + resolution: "@eslint/config-helpers@npm:0.3.0" + checksum: 10c0/013ae7b189eeae8b30cc2ee87bc5c9c091a9cd615579003290eb28bebad5d78806a478e74ba10b3fe08ed66975b52af7d2cd4b4b43990376412b14e5664878c8 + languageName: node + linkType: hard + +"@eslint/core@npm:^0.15.0, @eslint/core@npm:^0.15.1": + version: 0.15.1 + resolution: "@eslint/core@npm:0.15.1" + dependencies: + "@types/json-schema": "npm:^7.0.15" + checksum: 10c0/abaf641940776638b8c15a38d99ce0dac551a8939310ec81b9acd15836a574cf362588eaab03ab11919bc2a0f9648b19ea8dee33bf12675eb5b6fd38bda6f25e + languageName: node + linkType: hard + +"@eslint/eslintrc@npm:^3.3.1": + version: 3.3.1 + resolution: "@eslint/eslintrc@npm:3.3.1" + dependencies: + ajv: "npm:^6.12.4" + debug: "npm:^4.3.2" + espree: "npm:^10.0.1" + globals: "npm:^14.0.0" + ignore: "npm:^5.2.0" + import-fresh: "npm:^3.2.1" + js-yaml: "npm:^4.1.0" + minimatch: "npm:^3.1.2" + strip-json-comments: "npm:^3.1.1" + checksum: 10c0/b0e63f3bc5cce4555f791a4e487bf999173fcf27c65e1ab6e7d63634d8a43b33c3693e79f192cbff486d7df1be8ebb2bd2edc6e70ddd486cbfa84a359a3e3b41 + languageName: node + linkType: hard + +"@eslint/js@npm:9.32.0, @eslint/js@npm:^9.32.0": + version: 9.32.0 + resolution: "@eslint/js@npm:9.32.0" + checksum: 10c0/f71e8f9146638d11fb15238279feff98801120a4d4130f1c587c4f09b024ff5ec01af1ba88e97ba6b7013488868898a668f77091300cc3d4394c7a8ed32d2667 + languageName: node + linkType: hard + +"@eslint/object-schema@npm:^2.1.6": + version: 2.1.6 + resolution: "@eslint/object-schema@npm:2.1.6" + checksum: 10c0/b8cdb7edea5bc5f6a96173f8d768d3554a628327af536da2fc6967a93b040f2557114d98dbcdbf389d5a7b290985ad6a9ce5babc547f36fc1fde42e674d11a56 + languageName: node + linkType: hard + +"@eslint/plugin-kit@npm:^0.3.4": + version: 0.3.4 + resolution: "@eslint/plugin-kit@npm:0.3.4" + dependencies: + "@eslint/core": "npm:^0.15.1" + levn: "npm:^0.4.1" + checksum: 10c0/64331ca100f62a0115d10419a28059d0f377e390192163b867b9019517433d5073d10b4ec21f754fa01faf832aceb34178745924baab2957486f8bf95fd628d2 + languageName: node + linkType: hard + +"@hono/node-server@npm:^1.12.0": + version: 1.17.1 + resolution: "@hono/node-server@npm:1.17.1" + peerDependencies: + hono: ^4 + checksum: 10c0/a313e389a34782dbf310fc180dbd62bc3b32f41eeb5810718b560416122cdf81cd1bec1c31a3117e7fae26b596f9d8ffb2f7ac37a3a0194e2db89d4d4126998d + languageName: node + linkType: hard + +"@hono/zod-validator@npm:^0.2.2": + version: 0.2.2 + resolution: "@hono/zod-validator@npm:0.2.2" + peerDependencies: + hono: ">=3.9.0" + zod: ^3.19.1 + checksum: 10c0/3d6d03d28287e6f05e4cf5b86f3fa5fa386429a4212881f7344fe93272a69732ca8dd98634eb51df434b002230d2be2f3c6822f3b1ab320676932583856b30a5 + languageName: node + linkType: hard + +"@humanfs/core@npm:^0.19.1": + version: 0.19.1 + resolution: "@humanfs/core@npm:0.19.1" + checksum: 10c0/aa4e0152171c07879b458d0e8a704b8c3a89a8c0541726c6b65b81e84fd8b7564b5d6c633feadc6598307d34564bd53294b533491424e8e313d7ab6c7bc5dc67 + languageName: node + linkType: hard + +"@humanfs/node@npm:^0.16.6": + version: 0.16.6 + resolution: "@humanfs/node@npm:0.16.6" + dependencies: + "@humanfs/core": "npm:^0.19.1" + "@humanwhocodes/retry": "npm:^0.3.0" + checksum: 10c0/8356359c9f60108ec204cbd249ecd0356667359b2524886b357617c4a7c3b6aace0fd5a369f63747b926a762a88f8a25bc066fa1778508d110195ce7686243e1 + languageName: node + linkType: hard + +"@humanwhocodes/module-importer@npm:^1.0.1": + version: 1.0.1 + resolution: "@humanwhocodes/module-importer@npm:1.0.1" + checksum: 10c0/909b69c3b86d482c26b3359db16e46a32e0fb30bd306a3c176b8313b9e7313dba0f37f519de6aa8b0a1921349e505f259d19475e123182416a506d7f87e7f529 + languageName: node + linkType: hard + +"@humanwhocodes/retry@npm:^0.3.0": + version: 0.3.1 + resolution: "@humanwhocodes/retry@npm:0.3.1" + checksum: 10c0/f0da1282dfb45e8120480b9e2e275e2ac9bbe1cf016d046fdad8e27cc1285c45bb9e711681237944445157b430093412b4446c1ab3fc4bb037861b5904101d3b + languageName: node + linkType: hard + +"@humanwhocodes/retry@npm:^0.4.2": + version: 0.4.3 + resolution: "@humanwhocodes/retry@npm:0.4.3" + checksum: 10c0/3775bb30087d4440b3f7406d5a057777d90e4b9f435af488a4923ef249e93615fb78565a85f173a186a076c7706a81d0d57d563a2624e4de2c5c9c66c486ce42 + languageName: node + linkType: hard + +"@isaacs/cliui@npm:^8.0.2": + version: 8.0.2 + resolution: "@isaacs/cliui@npm:8.0.2" + dependencies: + string-width: "npm:^5.1.2" + string-width-cjs: "npm:string-width@^4.2.0" + strip-ansi: "npm:^7.0.1" + strip-ansi-cjs: "npm:strip-ansi@^6.0.1" + wrap-ansi: "npm:^8.1.0" + wrap-ansi-cjs: "npm:wrap-ansi@^7.0.0" + checksum: 10c0/b1bf42535d49f11dc137f18d5e4e63a28c5569de438a221c369483731e9dac9fb797af554e8bf02b6192d1e5eba6e6402cf93900c3d0ac86391d00d04876789e + languageName: node + linkType: hard + +"@isaacs/fs-minipass@npm:^4.0.0": + version: 4.0.1 + resolution: "@isaacs/fs-minipass@npm:4.0.1" + dependencies: + minipass: "npm:^7.0.4" + checksum: 10c0/c25b6dc1598790d5b55c0947a9b7d111cfa92594db5296c3b907e2f533c033666f692a3939eadac17b1c7c40d362d0b0635dc874cbfe3e70db7c2b07cc97a5d2 + languageName: node + linkType: hard + +"@jridgewell/gen-mapping@npm:^0.3.5": + version: 0.3.12 + resolution: "@jridgewell/gen-mapping@npm:0.3.12" + dependencies: + "@jridgewell/sourcemap-codec": "npm:^1.5.0" + "@jridgewell/trace-mapping": "npm:^0.3.24" + checksum: 10c0/32f771ae2467e4d440be609581f7338d786d3d621bac3469e943b9d6d116c23c4becb36f84898a92bbf2f3c0511365c54a945a3b86a83141547a2a360a5ec0c7 + languageName: node + linkType: hard + +"@jridgewell/resolve-uri@npm:^3.1.0": + version: 3.1.2 + resolution: "@jridgewell/resolve-uri@npm:3.1.2" + checksum: 10c0/d502e6fb516b35032331406d4e962c21fe77cdf1cbdb49c6142bcbd9e30507094b18972778a6e27cbad756209cfe34b1a27729e6fa08a2eb92b33943f680cf1e + languageName: node + linkType: hard + +"@jridgewell/sourcemap-codec@npm:^1.4.14, @jridgewell/sourcemap-codec@npm:^1.5.0": + version: 1.5.4 + resolution: "@jridgewell/sourcemap-codec@npm:1.5.4" + checksum: 10c0/c5aab3e6362a8dd94ad80ab90845730c825fc4c8d9cf07ebca7a2eb8a832d155d62558800fc41d42785f989ddbb21db6df004d1786e8ecb65e428ab8dff71309 + languageName: node + linkType: hard + +"@jridgewell/trace-mapping@npm:^0.3.24": + version: 0.3.29 + resolution: "@jridgewell/trace-mapping@npm:0.3.29" + dependencies: + "@jridgewell/resolve-uri": "npm:^3.1.0" + "@jridgewell/sourcemap-codec": "npm:^1.4.14" + checksum: 10c0/fb547ba31658c4d74eb17e7389f4908bf7c44cef47acb4c5baa57289daf68e6fe53c639f41f751b3923aca67010501264f70e7b49978ad1f040294b22c37b333 + languageName: node + linkType: hard + +"@langchain/anthropic@npm:^0.3.24": + version: 0.3.24 + resolution: "@langchain/anthropic@npm:0.3.24" + dependencies: + "@anthropic-ai/sdk": "npm:^0.56.0" + fast-xml-parser: "npm:^4.4.1" + peerDependencies: + "@langchain/core": ">=0.3.58 <0.4.0" + checksum: 10c0/6b042413881929262e43a10b8fb0d4960c1cac00434813036e7495801e362934f759d66a7067d6a9ae912263b5d3355906564614290259aa8cc7eccb125e948f + languageName: node + linkType: hard + +"@langchain/core@npm:^0.3.66": + version: 0.3.66 + resolution: "@langchain/core@npm:0.3.66" + dependencies: + "@cfworker/json-schema": "npm:^4.0.2" + ansi-styles: "npm:^5.0.0" + camelcase: "npm:6" + decamelize: "npm:1.2.0" + js-tiktoken: "npm:^1.0.12" + langsmith: "npm:^0.3.46" + mustache: "npm:^4.2.0" + p-queue: "npm:^6.6.2" + p-retry: "npm:4" + uuid: "npm:^10.0.0" + zod: "npm:^3.25.32" + zod-to-json-schema: "npm:^3.22.3" + checksum: 10c0/c6689e860ac037e799cb41f6ca756086b8673f5c1ae9090db1a060934755c832827f6bb317ec0fe7677505549b1ae40e1bf0bd45778f8bab34bb2e52e87bef11 + languageName: node + linkType: hard + +"@langchain/langgraph-api@npm:^0.0.52": + version: 0.0.52 + resolution: "@langchain/langgraph-api@npm:0.0.52" + dependencies: + "@babel/code-frame": "npm:^7.26.2" + "@hono/node-server": "npm:^1.12.0" + "@hono/zod-validator": "npm:^0.2.2" + "@langchain/langgraph-ui": "npm:0.0.52" + "@types/json-schema": "npm:^7.0.15" + "@typescript/vfs": "npm:^1.6.0" + dedent: "npm:^1.5.3" + dotenv: "npm:^16.4.7" + exit-hook: "npm:^4.0.0" + hono: "npm:^4.5.4" + langsmith: "npm:^0.3.33" + open: "npm:^10.1.0" + semver: "npm:^7.7.1" + stacktrace-parser: "npm:^0.1.10" + superjson: "npm:^2.2.2" + tsx: "npm:^4.19.3" + uuid: "npm:^10.0.0" + winston: "npm:^3.17.0" + winston-console-format: "npm:^1.0.8" + zod: "npm:^3.23.8" + peerDependencies: + "@langchain/core": ^0.3.59 + "@langchain/langgraph": ^0.2.57 || ^0.3.0 + "@langchain/langgraph-checkpoint": ~0.0.16 + "@langchain/langgraph-sdk": ~0.0.70 + typescript: ^5.5.4 + peerDependenciesMeta: + "@langchain/langgraph-sdk": + optional: true + checksum: 10c0/018e65b39ce4324dd956da1e5c14040a7293bc5b4affc1297f821c09016c4ae6d0170b9ae86454166bb87485218d76ca475b9fa4f558e6a754a10755fcb349ce + languageName: node + linkType: hard + +"@langchain/langgraph-checkpoint@npm:~0.0.18": + version: 0.0.18 + resolution: "@langchain/langgraph-checkpoint@npm:0.0.18" + dependencies: + uuid: "npm:^10.0.0" + peerDependencies: + "@langchain/core": ">=0.2.31 <0.4.0" + checksum: 10c0/d0a1525a7e45044953ddaafeeb702f7ddfb3d30632ac93842f8f940b1fe410f6e132080db6f6843e0a19bc59f8e4fef6b55d59f2812bc708f50cbec5fdcd6ce6 + languageName: node + linkType: hard + +"@langchain/langgraph-sdk@npm:^0.0.102, @langchain/langgraph-sdk@npm:~0.0.100": + version: 0.0.102 + resolution: "@langchain/langgraph-sdk@npm:0.0.102" + dependencies: + "@types/json-schema": "npm:^7.0.15" + p-queue: "npm:^6.6.2" + p-retry: "npm:4" + uuid: "npm:^9.0.0" + peerDependencies: + "@langchain/core": ">=0.2.31 <0.4.0" + react: ^18 || ^19 + react-dom: ^18 || ^19 + peerDependenciesMeta: + "@langchain/core": + optional: true + react: + optional: true + react-dom: + optional: true + checksum: 10c0/5567fe250e1f90f1d302f1a37dbc109e3a880743788026e35b7e395bd70aab8d8409a4e5b0d2ac09af4d02ffe045e33aed372aa108aa733ad8cfb76889c6bb36 + languageName: node + linkType: hard + +"@langchain/langgraph-ui@npm:0.0.52": + version: 0.0.52 + resolution: "@langchain/langgraph-ui@npm:0.0.52" + dependencies: + "@commander-js/extra-typings": "npm:^13.0.0" + commander: "npm:^13.0.0" + esbuild: "npm:^0.25.0" + esbuild-plugin-tailwindcss: "npm:^2.0.1" + zod: "npm:^3.23.8" + bin: + langgraphjs-ui: ./dist/cli.mjs + checksum: 10c0/1ce1e0a16234c4e0603578a41b9c66dc24b99a655f9ef8d08642d9265d9da8f237078ef1b82ccb7dee2b1d06ccb46a28caba46eb80068b126054ca4060bcbc74 + languageName: node + linkType: hard + +"@langchain/langgraph@npm:^0.3.11": + version: 0.3.11 + resolution: "@langchain/langgraph@npm:0.3.11" + dependencies: + "@langchain/langgraph-checkpoint": "npm:~0.0.18" + "@langchain/langgraph-sdk": "npm:~0.0.100" + uuid: "npm:^10.0.0" + zod: "npm:^3.25.32" + peerDependencies: + "@langchain/core": ">=0.3.58 < 0.4.0" + zod-to-json-schema: ^3.x + peerDependenciesMeta: + zod-to-json-schema: + optional: true + checksum: 10c0/55122bcb6e76fd1debafc9e57ecb5cdc47eefa983aba2468649d80abb588f98a9090fbe87bf460ce4a88a4aa9b4250af4fe1e9ed85ca436e22862bd1c2cb6ec8 + languageName: node + linkType: hard + +"@langchain/openai@npm:^0.6.3": + version: 0.6.3 + resolution: "@langchain/openai@npm:0.6.3" + dependencies: + js-tiktoken: "npm:^1.0.12" + openai: "npm:^5.3.0" + zod: "npm:^3.25.32" + peerDependencies: + "@langchain/core": ">=0.3.58 <0.4.0" + checksum: 10c0/cda9199446d241ba67da9bcf33571955043b69e5f21883e8f9052cfb5063871cac2e1a30d889b7b04e5cf4ab8f55643fe3cd13c9cfb5ab8ab514a190fa8104f0 + languageName: node + linkType: hard + +"@napi-rs/wasm-runtime@npm:^0.2.11": + version: 0.2.12 + resolution: "@napi-rs/wasm-runtime@npm:0.2.12" + dependencies: + "@emnapi/core": "npm:^1.4.3" + "@emnapi/runtime": "npm:^1.4.3" + "@tybys/wasm-util": "npm:^0.10.0" + checksum: 10c0/6d07922c0613aab30c6a497f4df297ca7c54e5b480e00035e0209b872d5c6aab7162fc49477267556109c2c7ed1eb9c65a174e27e9b87568106a87b0a6e3ca7d + languageName: node + linkType: hard + +"@nodelib/fs.scandir@npm:2.1.5": + version: 2.1.5 + resolution: "@nodelib/fs.scandir@npm:2.1.5" + dependencies: + "@nodelib/fs.stat": "npm:2.0.5" + run-parallel: "npm:^1.1.9" + checksum: 10c0/732c3b6d1b1e967440e65f284bd06e5821fedf10a1bea9ed2bb75956ea1f30e08c44d3def9d6a230666574edbaf136f8cfd319c14fd1f87c66e6a44449afb2eb + languageName: node + linkType: hard + +"@nodelib/fs.stat@npm:2.0.5, @nodelib/fs.stat@npm:^2.0.2": + version: 2.0.5 + resolution: "@nodelib/fs.stat@npm:2.0.5" + checksum: 10c0/88dafe5e3e29a388b07264680dc996c17f4bda48d163a9d4f5c1112979f0ce8ec72aa7116122c350b4e7976bc5566dc3ddb579be1ceaacc727872eb4ed93926d + languageName: node + linkType: hard + +"@nodelib/fs.walk@npm:^1.2.3": + version: 1.2.8 + resolution: "@nodelib/fs.walk@npm:1.2.8" + dependencies: + "@nodelib/fs.scandir": "npm:2.1.5" + fastq: "npm:^1.6.0" + checksum: 10c0/db9de047c3bb9b51f9335a7bb46f4fcfb6829fb628318c12115fbaf7d369bfce71c15b103d1fc3b464812d936220ee9bc1c8f762d032c9f6be9acc99249095b1 + languageName: node + linkType: hard + +"@npmcli/agent@npm:^3.0.0": + version: 3.0.0 + resolution: "@npmcli/agent@npm:3.0.0" + dependencies: + agent-base: "npm:^7.1.0" + http-proxy-agent: "npm:^7.0.0" + https-proxy-agent: "npm:^7.0.1" + lru-cache: "npm:^10.0.1" + socks-proxy-agent: "npm:^8.0.3" + checksum: 10c0/efe37b982f30740ee77696a80c196912c274ecd2cb243bc6ae7053a50c733ce0f6c09fda085145f33ecf453be19654acca74b69e81eaad4c90f00ccffe2f9271 + languageName: node + linkType: hard + +"@npmcli/fs@npm:^4.0.0": + version: 4.0.0 + resolution: "@npmcli/fs@npm:4.0.0" + dependencies: + semver: "npm:^7.3.5" + checksum: 10c0/c90935d5ce670c87b6b14fab04a965a3b8137e585f8b2a6257263bd7f97756dd736cb165bb470e5156a9e718ecd99413dccc54b1138c1a46d6ec7cf325982fe5 + languageName: node + linkType: hard + +"@pkgjs/parseargs@npm:^0.11.0": + version: 0.11.0 + resolution: "@pkgjs/parseargs@npm:0.11.0" + checksum: 10c0/5bd7576bb1b38a47a7fc7b51ac9f38748e772beebc56200450c4a817d712232b8f1d3ef70532c80840243c657d491cf6a6be1e3a214cff907645819fdc34aadd + languageName: node + linkType: hard + +"@tailwindcss/node@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/node@npm:4.1.11" + dependencies: + "@ampproject/remapping": "npm:^2.3.0" + enhanced-resolve: "npm:^5.18.1" + jiti: "npm:^2.4.2" + lightningcss: "npm:1.30.1" + magic-string: "npm:^0.30.17" + source-map-js: "npm:^1.2.1" + tailwindcss: "npm:4.1.11" + checksum: 10c0/1a433aecd80d0c6d07d468ed69b696e4e02996e6b77cc5ed66e3c91b02f5fa9a26320fb321e4b1aa107003b401d7a4ffeb2986966dc022ec329a44e54493a2aa + languageName: node + linkType: hard + +"@tailwindcss/oxide-android-arm64@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-android-arm64@npm:4.1.11" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"@tailwindcss/oxide-darwin-arm64@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-darwin-arm64@npm:4.1.11" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@tailwindcss/oxide-darwin-x64@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-darwin-x64@npm:4.1.11" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@tailwindcss/oxide-freebsd-x64@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-freebsd-x64@npm:4.1.11" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"@tailwindcss/oxide-linux-arm-gnueabihf@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-linux-arm-gnueabihf@npm:4.1.11" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@tailwindcss/oxide-linux-arm64-gnu@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-linux-arm64-gnu@npm:4.1.11" + conditions: os=linux & cpu=arm64 & libc=glibc + languageName: node + linkType: hard + +"@tailwindcss/oxide-linux-arm64-musl@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-linux-arm64-musl@npm:4.1.11" + conditions: os=linux & cpu=arm64 & libc=musl + languageName: node + linkType: hard + +"@tailwindcss/oxide-linux-x64-gnu@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-linux-x64-gnu@npm:4.1.11" + conditions: os=linux & cpu=x64 & libc=glibc + languageName: node + linkType: hard + +"@tailwindcss/oxide-linux-x64-musl@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-linux-x64-musl@npm:4.1.11" + conditions: os=linux & cpu=x64 & libc=musl + languageName: node + linkType: hard + +"@tailwindcss/oxide-wasm32-wasi@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-wasm32-wasi@npm:4.1.11" + dependencies: + "@emnapi/core": "npm:^1.4.3" + "@emnapi/runtime": "npm:^1.4.3" + "@emnapi/wasi-threads": "npm:^1.0.2" + "@napi-rs/wasm-runtime": "npm:^0.2.11" + "@tybys/wasm-util": "npm:^0.9.0" + tslib: "npm:^2.8.0" + conditions: cpu=wasm32 + languageName: node + linkType: hard + +"@tailwindcss/oxide-win32-arm64-msvc@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-win32-arm64-msvc@npm:4.1.11" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@tailwindcss/oxide-win32-x64-msvc@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide-win32-x64-msvc@npm:4.1.11" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + +"@tailwindcss/oxide@npm:4.1.11": + version: 4.1.11 + resolution: "@tailwindcss/oxide@npm:4.1.11" + dependencies: + "@tailwindcss/oxide-android-arm64": "npm:4.1.11" + "@tailwindcss/oxide-darwin-arm64": "npm:4.1.11" + "@tailwindcss/oxide-darwin-x64": "npm:4.1.11" + "@tailwindcss/oxide-freebsd-x64": "npm:4.1.11" + "@tailwindcss/oxide-linux-arm-gnueabihf": "npm:4.1.11" + "@tailwindcss/oxide-linux-arm64-gnu": "npm:4.1.11" + "@tailwindcss/oxide-linux-arm64-musl": "npm:4.1.11" + "@tailwindcss/oxide-linux-x64-gnu": "npm:4.1.11" + "@tailwindcss/oxide-linux-x64-musl": "npm:4.1.11" + "@tailwindcss/oxide-wasm32-wasi": "npm:4.1.11" + "@tailwindcss/oxide-win32-arm64-msvc": "npm:4.1.11" + "@tailwindcss/oxide-win32-x64-msvc": "npm:4.1.11" + detect-libc: "npm:^2.0.4" + tar: "npm:^7.4.3" + dependenciesMeta: + "@tailwindcss/oxide-android-arm64": + optional: true + "@tailwindcss/oxide-darwin-arm64": + optional: true + "@tailwindcss/oxide-darwin-x64": + optional: true + "@tailwindcss/oxide-freebsd-x64": + optional: true + "@tailwindcss/oxide-linux-arm-gnueabihf": + optional: true + "@tailwindcss/oxide-linux-arm64-gnu": + optional: true + "@tailwindcss/oxide-linux-arm64-musl": + optional: true + "@tailwindcss/oxide-linux-x64-gnu": + optional: true + "@tailwindcss/oxide-linux-x64-musl": + optional: true + "@tailwindcss/oxide-wasm32-wasi": + optional: true + "@tailwindcss/oxide-win32-arm64-msvc": + optional: true + "@tailwindcss/oxide-win32-x64-msvc": + optional: true + checksum: 10c0/0455483b0e52885a3f36ecbec5409c360159bb0ee969f3a64c2d93dbd94d0d769c1351b7031f4d4b9d8bed997d04d685ca9519160714f432d63f4e824ce1406d + languageName: node + linkType: hard + +"@tailwindcss/postcss@npm:^4.0.5": + version: 4.1.11 + resolution: "@tailwindcss/postcss@npm:4.1.11" + dependencies: + "@alloc/quick-lru": "npm:^5.2.0" + "@tailwindcss/node": "npm:4.1.11" + "@tailwindcss/oxide": "npm:4.1.11" + postcss: "npm:^8.4.41" + tailwindcss: "npm:4.1.11" + checksum: 10c0/e449e1992d0723061aa9452979cd01727db4d1e81b2c16762b01899d06a6c9015792d10d3db4cb553e2e59f307593dc4ccf679ef1add5f774da73d3a091f7227 + languageName: node + linkType: hard + +"@tybys/wasm-util@npm:^0.10.0": + version: 0.10.0 + resolution: "@tybys/wasm-util@npm:0.10.0" + dependencies: + tslib: "npm:^2.4.0" + checksum: 10c0/044feba55c1e2af703aa4946139969badb183ce1a659a75ed60bc195a90e73a3f3fc53bcd643497c9954597763ddb051fec62f80962b2ca6fc716ba897dc696e + languageName: node + linkType: hard + +"@tybys/wasm-util@npm:^0.9.0": + version: 0.9.0 + resolution: "@tybys/wasm-util@npm:0.9.0" + dependencies: + tslib: "npm:^2.4.0" + checksum: 10c0/f9fde5c554455019f33af6c8215f1a1435028803dc2a2825b077d812bed4209a1a64444a4ca0ce2ea7e1175c8d88e2f9173a36a33c199e8a5c671aa31de8242d + languageName: node + linkType: hard + +"@types/estree@npm:^1.0.6": + version: 1.0.8 + resolution: "@types/estree@npm:1.0.8" + checksum: 10c0/39d34d1afaa338ab9763f37ad6066e3f349444f9052b9676a7cc0252ef9485a41c6d81c9c4e0d26e9077993354edf25efc853f3224dd4b447175ef62bdcc86a5 + languageName: node + linkType: hard + +"@types/json-schema@npm:^7.0.15": + version: 7.0.15 + resolution: "@types/json-schema@npm:7.0.15" + checksum: 10c0/a996a745e6c5d60292f36731dd41341339d4eeed8180bb09226e5c8d23759067692b1d88e5d91d72ee83dfc00d3aca8e7bd43ea120516c17922cbcb7c3e252db + languageName: node + linkType: hard + +"@types/retry@npm:0.12.0": + version: 0.12.0 + resolution: "@types/retry@npm:0.12.0" + checksum: 10c0/7c5c9086369826f569b83a4683661557cab1361bac0897a1cefa1a915ff739acd10ca0d62b01071046fe3f5a3f7f2aec80785fe283b75602dc6726781ea3e328 + languageName: node + linkType: hard + +"@types/triple-beam@npm:^1.3.2": + version: 1.3.5 + resolution: "@types/triple-beam@npm:1.3.5" + checksum: 10c0/d5d7f25da612f6d79266f4f1bb9c1ef8f1684e9f60abab251e1261170631062b656ba26ff22631f2760caeafd372abc41e64867cde27fba54fafb73a35b9056a + languageName: node + linkType: hard + +"@types/uuid@npm:^10.0.0": + version: 10.0.0 + resolution: "@types/uuid@npm:10.0.0" + checksum: 10c0/9a1404bf287164481cb9b97f6bb638f78f955be57c40c6513b7655160beb29df6f84c915aaf4089a1559c216557dc4d2f79b48d978742d3ae10b937420ddac60 + languageName: node + linkType: hard + +"@typescript-eslint/eslint-plugin@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/eslint-plugin@npm:8.38.0" + dependencies: + "@eslint-community/regexpp": "npm:^4.10.0" + "@typescript-eslint/scope-manager": "npm:8.38.0" + "@typescript-eslint/type-utils": "npm:8.38.0" + "@typescript-eslint/utils": "npm:8.38.0" + "@typescript-eslint/visitor-keys": "npm:8.38.0" + graphemer: "npm:^1.4.0" + ignore: "npm:^7.0.0" + natural-compare: "npm:^1.4.0" + ts-api-utils: "npm:^2.1.0" + peerDependencies: + "@typescript-eslint/parser": ^8.38.0 + eslint: ^8.57.0 || ^9.0.0 + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/199b82e9f0136baecf515df7c31bfed926a7c6d4e6298f64ee1a77c8bdd7a8cb92a2ea55a5a345c9f2948a02f7be6d72530efbe803afa1892b593fbd529d0c27 + languageName: node + linkType: hard + +"@typescript-eslint/parser@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/parser@npm:8.38.0" + dependencies: + "@typescript-eslint/scope-manager": "npm:8.38.0" + "@typescript-eslint/types": "npm:8.38.0" + "@typescript-eslint/typescript-estree": "npm:8.38.0" + "@typescript-eslint/visitor-keys": "npm:8.38.0" + debug: "npm:^4.3.4" + peerDependencies: + eslint: ^8.57.0 || ^9.0.0 + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/5580c2a328f0c15f85e4a0961a07584013cc0aca85fe868486187f7c92e9e3f6602c6e3dab917b092b94cd492ed40827c6f5fea42730bef88eb17592c947adf4 + languageName: node + linkType: hard + +"@typescript-eslint/project-service@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/project-service@npm:8.38.0" + dependencies: + "@typescript-eslint/tsconfig-utils": "npm:^8.38.0" + "@typescript-eslint/types": "npm:^8.38.0" + debug: "npm:^4.3.4" + peerDependencies: + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/87d2f55521e289bbcdc666b1f4587ee2d43039cee927310b05abaa534b528dfb1b5565c1545bb4996d7fbdf9d5a3b0aa0e6c93a8f1289e3fcfd60d246364a884 + languageName: node + linkType: hard + +"@typescript-eslint/scope-manager@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/scope-manager@npm:8.38.0" + dependencies: + "@typescript-eslint/types": "npm:8.38.0" + "@typescript-eslint/visitor-keys": "npm:8.38.0" + checksum: 10c0/ceaf489ea1f005afb187932a7ee363dfe1e0f7cc3db921283991e20e4c756411a5e25afbec72edd2095d6a4384f73591f4c750cf65b5eaa650c90f64ef9fe809 + languageName: node + linkType: hard + +"@typescript-eslint/tsconfig-utils@npm:8.38.0, @typescript-eslint/tsconfig-utils@npm:^8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/tsconfig-utils@npm:8.38.0" + peerDependencies: + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/1a90da16bf1f7cfbd0303640a8ead64a0080f2b1d5969994bdac3b80abfa1177f0c6fbf61250bae082e72cf5014308f2f5cc98edd6510202f13420a7ffd07a84 + languageName: node + linkType: hard + +"@typescript-eslint/type-utils@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/type-utils@npm:8.38.0" + dependencies: + "@typescript-eslint/types": "npm:8.38.0" + "@typescript-eslint/typescript-estree": "npm:8.38.0" + "@typescript-eslint/utils": "npm:8.38.0" + debug: "npm:^4.3.4" + ts-api-utils: "npm:^2.1.0" + peerDependencies: + eslint: ^8.57.0 || ^9.0.0 + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/27795c4bd0be395dda3424e57d746639c579b7522af1c17731b915298a6378fd78869e8e141526064b6047db2c86ba06444469ace19c98cda5779d06f4abd37c + languageName: node + linkType: hard + +"@typescript-eslint/types@npm:8.38.0, @typescript-eslint/types@npm:^8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/types@npm:8.38.0" + checksum: 10c0/f0ac0060c98c0f3d1871f107177b6ae25a0f1846ca8bd8cfc7e1f1dd0ddce293cd8ac4a5764d6a767de3503d5d01defcd68c758cb7ba6de52f82b209a918d0d2 + languageName: node + linkType: hard + +"@typescript-eslint/typescript-estree@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/typescript-estree@npm:8.38.0" + dependencies: + "@typescript-eslint/project-service": "npm:8.38.0" + "@typescript-eslint/tsconfig-utils": "npm:8.38.0" + "@typescript-eslint/types": "npm:8.38.0" + "@typescript-eslint/visitor-keys": "npm:8.38.0" + debug: "npm:^4.3.4" + fast-glob: "npm:^3.3.2" + is-glob: "npm:^4.0.3" + minimatch: "npm:^9.0.4" + semver: "npm:^7.6.0" + ts-api-utils: "npm:^2.1.0" + peerDependencies: + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/00a00f6549877f4ae5c2847fa5ac52bf42cbd59a87533856c359e2746e448ed150b27a6137c92fd50c06e6a4b39e386d6b738fac97d80d05596e81ce55933230 + languageName: node + linkType: hard + +"@typescript-eslint/utils@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/utils@npm:8.38.0" + dependencies: + "@eslint-community/eslint-utils": "npm:^4.7.0" + "@typescript-eslint/scope-manager": "npm:8.38.0" + "@typescript-eslint/types": "npm:8.38.0" + "@typescript-eslint/typescript-estree": "npm:8.38.0" + peerDependencies: + eslint: ^8.57.0 || ^9.0.0 + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/e97a45bf44f315f9ed8c2988429e18c88e3369c9ee3227ee86446d2d49f7325abebbbc9ce801e178f676baa986d3e1fd4b5391f1640c6eb8944c123423ae43bb + languageName: node + linkType: hard + +"@typescript-eslint/visitor-keys@npm:8.38.0": + version: 8.38.0 + resolution: "@typescript-eslint/visitor-keys@npm:8.38.0" + dependencies: + "@typescript-eslint/types": "npm:8.38.0" + eslint-visitor-keys: "npm:^4.2.1" + checksum: 10c0/071a756e383f41a6c9e51d78c8c64bd41cd5af68b0faef5fbaec4fa5dbd65ec9e4cd610c2e2cdbe9e2facc362995f202850622b78e821609a277b5b601a1d4ec + languageName: node + linkType: hard + +"@typescript/vfs@npm:^1.6.0": + version: 1.6.1 + resolution: "@typescript/vfs@npm:1.6.1" + dependencies: + debug: "npm:^4.1.1" + peerDependencies: + typescript: "*" + checksum: 10c0/3878686aff4bf26813dad9242aa8e01c5c9734f4d37f31035f93e9c8b850f15ec6a4480f04cf3a3a1cbf78a4e796ae1be5d6c54f7f7c91556eafee913a8d0da4 + languageName: node + linkType: hard + +"_codeblocks@workspace:.": + version: 0.0.0-use.local + resolution: "_codeblocks@workspace:." + dependencies: + "@eslint/js": "npm:^9.32.0" + "@langchain/anthropic": "npm:^0.3.24" + "@langchain/core": "npm:^0.3.66" + "@langchain/langgraph": "npm:^0.3.11" + "@langchain/langgraph-api": "npm:^0.0.52" + "@langchain/langgraph-sdk": "npm:^0.0.102" + "@langchain/openai": "npm:^0.6.3" + eslint: "npm:^9.32.0" + globals: "npm:^16.3.0" + jiti: "npm:^2.5.1" + typescript: "npm:^5.8.3" + typescript-eslint: "npm:^8.38.0" + zod: "npm:^4.0.10" + languageName: unknown + linkType: soft + +"abbrev@npm:^3.0.0": + version: 3.0.1 + resolution: "abbrev@npm:3.0.1" + checksum: 10c0/21ba8f574ea57a3106d6d35623f2c4a9111d9ee3e9a5be47baed46ec2457d2eac46e07a5c4a60186f88cb98abbe3e24f2d4cca70bc2b12f1692523e2209a9ccf + languageName: node + linkType: hard + +"acorn-jsx@npm:^5.3.2": + version: 5.3.2 + resolution: "acorn-jsx@npm:5.3.2" + peerDependencies: + acorn: ^6.0.0 || ^7.0.0 || ^8.0.0 + checksum: 10c0/4c54868fbef3b8d58927d5e33f0a4de35f59012fe7b12cf9dfbb345fb8f46607709e1c4431be869a23fb63c151033d84c4198fa9f79385cec34fcb1dd53974c1 + languageName: node + linkType: hard + +"acorn@npm:^8.15.0": + version: 8.15.0 + resolution: "acorn@npm:8.15.0" + bin: + acorn: bin/acorn + checksum: 10c0/dec73ff59b7d6628a01eebaece7f2bdb8bb62b9b5926dcad0f8931f2b8b79c2be21f6c68ac095592adb5adb15831a3635d9343e6a91d028bbe85d564875ec3ec + languageName: node + linkType: hard + +"agent-base@npm:^7.1.0, agent-base@npm:^7.1.2": + version: 7.1.4 + resolution: "agent-base@npm:7.1.4" + checksum: 10c0/c2c9ab7599692d594b6a161559ada307b7a624fa4c7b03e3afdb5a5e31cd0e53269115b620fcab024c5ac6a6f37fa5eb2e004f076ad30f5f7e6b8b671f7b35fe + languageName: node + linkType: hard + +"ajv@npm:^6.12.4": + version: 6.12.6 + resolution: "ajv@npm:6.12.6" + dependencies: + fast-deep-equal: "npm:^3.1.1" + fast-json-stable-stringify: "npm:^2.0.0" + json-schema-traverse: "npm:^0.4.1" + uri-js: "npm:^4.2.2" + checksum: 10c0/41e23642cbe545889245b9d2a45854ebba51cda6c778ebced9649420d9205f2efb39cb43dbc41e358409223b1ea43303ae4839db682c848b891e4811da1a5a71 + languageName: node + linkType: hard + +"ansi-regex@npm:^5.0.1": + version: 5.0.1 + resolution: "ansi-regex@npm:5.0.1" + checksum: 10c0/9a64bb8627b434ba9327b60c027742e5d17ac69277960d041898596271d992d4d52ba7267a63ca10232e29f6107fc8a835f6ce8d719b88c5f8493f8254813737 + languageName: node + linkType: hard + +"ansi-regex@npm:^6.0.1": + version: 6.1.0 + resolution: "ansi-regex@npm:6.1.0" + checksum: 10c0/a91daeddd54746338478eef88af3439a7edf30f8e23196e2d6ed182da9add559c601266dbef01c2efa46a958ad6f1f8b176799657616c702b5b02e799e7fd8dc + languageName: node + linkType: hard + +"ansi-styles@npm:^4.0.0, ansi-styles@npm:^4.1.0": + version: 4.3.0 + resolution: "ansi-styles@npm:4.3.0" + dependencies: + color-convert: "npm:^2.0.1" + checksum: 10c0/895a23929da416f2bd3de7e9cb4eabd340949328ab85ddd6e484a637d8f6820d485f53933446f5291c3b760cbc488beb8e88573dd0f9c7daf83dccc8fe81b041 + languageName: node + linkType: hard + +"ansi-styles@npm:^5.0.0": + version: 5.2.0 + resolution: "ansi-styles@npm:5.2.0" + checksum: 10c0/9c4ca80eb3c2fb7b33841c210d2f20807f40865d27008d7c3f707b7f95cab7d67462a565e2388ac3285b71cb3d9bb2173de8da37c57692a362885ec34d6e27df + languageName: node + linkType: hard + +"ansi-styles@npm:^6.1.0": + version: 6.2.1 + resolution: "ansi-styles@npm:6.2.1" + checksum: 10c0/5d1ec38c123984bcedd996eac680d548f31828bd679a66db2bdf11844634dde55fec3efa9c6bb1d89056a5e79c1ac540c4c784d592ea1d25028a92227d2f2d5c + languageName: node + linkType: hard + +"argparse@npm:^2.0.1": + version: 2.0.1 + resolution: "argparse@npm:2.0.1" + checksum: 10c0/c5640c2d89045371c7cedd6a70212a04e360fd34d6edeae32f6952c63949e3525ea77dbec0289d8213a99bbaeab5abfa860b5c12cf88a2e6cf8106e90dd27a7e + languageName: node + linkType: hard + +"async@npm:^3.2.3": + version: 3.2.6 + resolution: "async@npm:3.2.6" + checksum: 10c0/36484bb15ceddf07078688d95e27076379cc2f87b10c03b6dd8a83e89475a3c8df5848859dd06a4c95af1e4c16fc973de0171a77f18ea00be899aca2a4f85e70 + languageName: node + linkType: hard + +"autoprefixer@npm:^10.4.20": + version: 10.4.21 + resolution: "autoprefixer@npm:10.4.21" + dependencies: + browserslist: "npm:^4.24.4" + caniuse-lite: "npm:^1.0.30001702" + fraction.js: "npm:^4.3.7" + normalize-range: "npm:^0.1.2" + picocolors: "npm:^1.1.1" + postcss-value-parser: "npm:^4.2.0" + peerDependencies: + postcss: ^8.1.0 + bin: + autoprefixer: bin/autoprefixer + checksum: 10c0/de5b71d26d0baff4bbfb3d59f7cf7114a6030c9eeb66167acf49a32c5b61c68e308f1e0f869d92334436a221035d08b51cd1b2f2c4689b8d955149423c16d4d4 + languageName: node + linkType: hard + +"balanced-match@npm:^1.0.0": + version: 1.0.2 + resolution: "balanced-match@npm:1.0.2" + checksum: 10c0/9308baf0a7e4838a82bbfd11e01b1cb0f0cf2893bc1676c27c2a8c0e70cbae1c59120c3268517a8ae7fb6376b4639ef81ca22582611dbee4ed28df945134aaee + languageName: node + linkType: hard + +"base64-js@npm:^1.5.1": + version: 1.5.1 + resolution: "base64-js@npm:1.5.1" + checksum: 10c0/f23823513b63173a001030fae4f2dabe283b99a9d324ade3ad3d148e218134676f1ee8568c877cd79ec1c53158dcf2d2ba527a97c606618928ba99dd930102bf + languageName: node + linkType: hard + +"brace-expansion@npm:^1.1.7": + version: 1.1.12 + resolution: "brace-expansion@npm:1.1.12" + dependencies: + balanced-match: "npm:^1.0.0" + concat-map: "npm:0.0.1" + checksum: 10c0/975fecac2bb7758c062c20d0b3b6288c7cc895219ee25f0a64a9de662dbac981ff0b6e89909c3897c1f84fa353113a721923afdec5f8b2350255b097f12b1f73 + languageName: node + linkType: hard + +"brace-expansion@npm:^2.0.1": + version: 2.0.2 + resolution: "brace-expansion@npm:2.0.2" + dependencies: + balanced-match: "npm:^1.0.0" + checksum: 10c0/6d117a4c793488af86b83172deb6af143e94c17bc53b0b3cec259733923b4ca84679d506ac261f4ba3c7ed37c46018e2ff442f9ce453af8643ecd64f4a54e6cf + languageName: node + linkType: hard + +"braces@npm:^3.0.3": + version: 3.0.3 + resolution: "braces@npm:3.0.3" + dependencies: + fill-range: "npm:^7.1.1" + checksum: 10c0/7c6dfd30c338d2997ba77500539227b9d1f85e388a5f43220865201e407e076783d0881f2d297b9f80951b4c957fcf0b51c1d2d24227631643c3f7c284b0aa04 + languageName: node + linkType: hard + +"browserslist@npm:^4.24.4": + version: 4.25.1 + resolution: "browserslist@npm:4.25.1" + dependencies: + caniuse-lite: "npm:^1.0.30001726" + electron-to-chromium: "npm:^1.5.173" + node-releases: "npm:^2.0.19" + update-browserslist-db: "npm:^1.1.3" + bin: + browserslist: cli.js + checksum: 10c0/acba5f0bdbd5e72dafae1e6ec79235b7bad305ed104e082ed07c34c38c7cb8ea1bc0f6be1496958c40482e40166084458fc3aee15111f15faa79212ad9081b2a + languageName: node + linkType: hard + +"bundle-name@npm:^4.1.0": + version: 4.1.0 + resolution: "bundle-name@npm:4.1.0" + dependencies: + run-applescript: "npm:^7.0.0" + checksum: 10c0/8e575981e79c2bcf14d8b1c027a3775c095d362d1382312f444a7c861b0e21513c0bd8db5bd2b16e50ba0709fa622d4eab6b53192d222120305e68359daece29 + languageName: node + linkType: hard + +"cacache@npm:^19.0.1": + version: 19.0.1 + resolution: "cacache@npm:19.0.1" + dependencies: + "@npmcli/fs": "npm:^4.0.0" + fs-minipass: "npm:^3.0.0" + glob: "npm:^10.2.2" + lru-cache: "npm:^10.0.1" + minipass: "npm:^7.0.3" + minipass-collect: "npm:^2.0.1" + minipass-flush: "npm:^1.0.5" + minipass-pipeline: "npm:^1.2.4" + p-map: "npm:^7.0.2" + ssri: "npm:^12.0.0" + tar: "npm:^7.4.3" + unique-filename: "npm:^4.0.0" + checksum: 10c0/01f2134e1bd7d3ab68be851df96c8d63b492b1853b67f2eecb2c37bb682d37cb70bb858a16f2f0554d3c0071be6dfe21456a1ff6fa4b7eed996570d6a25ffe9c + languageName: node + linkType: hard + +"callsites@npm:^3.0.0": + version: 3.1.0 + resolution: "callsites@npm:3.1.0" + checksum: 10c0/fff92277400eb06c3079f9e74f3af120db9f8ea03bad0e84d9aede54bbe2d44a56cccb5f6cf12211f93f52306df87077ecec5b712794c5a9b5dac6d615a3f301 + languageName: node + linkType: hard + +"camelcase@npm:6": + version: 6.3.0 + resolution: "camelcase@npm:6.3.0" + checksum: 10c0/0d701658219bd3116d12da3eab31acddb3f9440790c0792e0d398f0a520a6a4058018e546862b6fba89d7ae990efaeb97da71e1913e9ebf5a8b5621a3d55c710 + languageName: node + linkType: hard + +"caniuse-lite@npm:^1.0.30001702, caniuse-lite@npm:^1.0.30001726": + version: 1.0.30001727 + resolution: "caniuse-lite@npm:1.0.30001727" + checksum: 10c0/f0a441c05d8925d728c2d02ce23b001935f52183a3bf669556f302568fe258d1657940c7ac0b998f92bc41383e185b390279a7d779e6d96a2b47881f56400221 + languageName: node + linkType: hard + +"chalk@npm:^4.0.0, chalk@npm:^4.1.2": + version: 4.1.2 + resolution: "chalk@npm:4.1.2" + dependencies: + ansi-styles: "npm:^4.1.0" + supports-color: "npm:^7.1.0" + checksum: 10c0/4a3fef5cc34975c898ffe77141450f679721df9dde00f6c304353fa9c8b571929123b26a0e4617bde5018977eb655b31970c297b91b63ee83bb82aeb04666880 + languageName: node + linkType: hard + +"chownr@npm:^3.0.0": + version: 3.0.0 + resolution: "chownr@npm:3.0.0" + checksum: 10c0/43925b87700f7e3893296c8e9c56cc58f926411cce3a6e5898136daaf08f08b9a8eb76d37d3267e707d0dcc17aed2e2ebdf5848c0c3ce95cf910a919935c1b10 + languageName: node + linkType: hard + +"color-convert@npm:^1.9.3": + version: 1.9.3 + resolution: "color-convert@npm:1.9.3" + dependencies: + color-name: "npm:1.1.3" + checksum: 10c0/5ad3c534949a8c68fca8fbc6f09068f435f0ad290ab8b2f76841b9e6af7e0bb57b98cb05b0e19fe33f5d91e5a8611ad457e5f69e0a484caad1f7487fd0e8253c + languageName: node + linkType: hard + +"color-convert@npm:^2.0.1": + version: 2.0.1 + resolution: "color-convert@npm:2.0.1" + dependencies: + color-name: "npm:~1.1.4" + checksum: 10c0/37e1150172f2e311fe1b2df62c6293a342ee7380da7b9cfdba67ea539909afbd74da27033208d01d6d5cfc65ee7868a22e18d7e7648e004425441c0f8a15a7d7 + languageName: node + linkType: hard + +"color-name@npm:1.1.3": + version: 1.1.3 + resolution: "color-name@npm:1.1.3" + checksum: 10c0/566a3d42cca25b9b3cd5528cd7754b8e89c0eb646b7f214e8e2eaddb69994ac5f0557d9c175eb5d8f0ad73531140d9c47525085ee752a91a2ab15ab459caf6d6 + languageName: node + linkType: hard + +"color-name@npm:^1.0.0, color-name@npm:~1.1.4": + version: 1.1.4 + resolution: "color-name@npm:1.1.4" + checksum: 10c0/a1a3f914156960902f46f7f56bc62effc6c94e84b2cae157a526b1c1f74b677a47ec602bf68a61abfa2b42d15b7c5651c6dbe72a43af720bc588dff885b10f95 + languageName: node + linkType: hard + +"color-string@npm:^1.6.0": + version: 1.9.1 + resolution: "color-string@npm:1.9.1" + dependencies: + color-name: "npm:^1.0.0" + simple-swizzle: "npm:^0.2.2" + checksum: 10c0/b0bfd74c03b1f837f543898b512f5ea353f71630ccdd0d66f83028d1f0924a7d4272deb278b9aef376cacf1289b522ac3fb175e99895283645a2dc3a33af2404 + languageName: node + linkType: hard + +"color@npm:^3.1.3": + version: 3.2.1 + resolution: "color@npm:3.2.1" + dependencies: + color-convert: "npm:^1.9.3" + color-string: "npm:^1.6.0" + checksum: 10c0/39345d55825884c32a88b95127d417a2c24681d8b57069413596d9fcbb721459ef9d9ec24ce3e65527b5373ce171b73e38dbcd9c830a52a6487e7f37bf00e83c + languageName: node + linkType: hard + +"colors@npm:^1.4.0": + version: 1.4.0 + resolution: "colors@npm:1.4.0" + checksum: 10c0/9af357c019da3c5a098a301cf64e3799d27549d8f185d86f79af23069e4f4303110d115da98483519331f6fb71c8568d5688fa1c6523600044fd4a54e97c4efb + languageName: node + linkType: hard + +"colorspace@npm:1.1.x": + version: 1.1.4 + resolution: "colorspace@npm:1.1.4" + dependencies: + color: "npm:^3.1.3" + text-hex: "npm:1.0.x" + checksum: 10c0/af5f91ff7f8e146b96e439ac20ed79b197210193bde721b47380a75b21751d90fa56390c773bb67c0aedd34ff85091883a437ab56861c779bd507d639ba7e123 + languageName: node + linkType: hard + +"commander@npm:^13.0.0": + version: 13.1.0 + resolution: "commander@npm:13.1.0" + checksum: 10c0/7b8c5544bba704fbe84b7cab2e043df8586d5c114a4c5b607f83ae5060708940ed0b5bd5838cf8ce27539cde265c1cbd59ce3c8c6b017ed3eec8943e3a415164 + languageName: node + linkType: hard + +"concat-map@npm:0.0.1": + version: 0.0.1 + resolution: "concat-map@npm:0.0.1" + checksum: 10c0/c996b1cfdf95b6c90fee4dae37e332c8b6eb7d106430c17d538034c0ad9a1630cb194d2ab37293b1bdd4d779494beee7786d586a50bd9376fd6f7bcc2bd4c98f + languageName: node + linkType: hard + +"console-table-printer@npm:^2.12.1": + version: 2.14.6 + resolution: "console-table-printer@npm:2.14.6" + dependencies: + simple-wcswidth: "npm:^1.0.1" + checksum: 10c0/af4f7f18d2a70130ea9fd76ffd9c56351329665fe3bec96cfab26d71dd2de6b983b09ec163c9346c72fa6fb7fdce350e09ee132b1c89db83ac50b23742cdb10f + languageName: node + linkType: hard + +"copy-anything@npm:^3.0.2": + version: 3.0.5 + resolution: "copy-anything@npm:3.0.5" + dependencies: + is-what: "npm:^4.1.8" + checksum: 10c0/01eadd500c7e1db71d32d95a3bfaaedcb839ef891c741f6305ab0461398056133de08f2d1bf4c392b364e7bdb7ce498513896e137a7a183ac2516b065c28a4fe + languageName: node + linkType: hard + +"cross-spawn@npm:^7.0.6": + version: 7.0.6 + resolution: "cross-spawn@npm:7.0.6" + dependencies: + path-key: "npm:^3.1.0" + shebang-command: "npm:^2.0.0" + which: "npm:^2.0.1" + checksum: 10c0/053ea8b2135caff68a9e81470e845613e374e7309a47731e81639de3eaeb90c3d01af0e0b44d2ab9d50b43467223b88567dfeb3262db942dc063b9976718ffc1 + languageName: node + linkType: hard + +"cssesc@npm:^3.0.0": + version: 3.0.0 + resolution: "cssesc@npm:3.0.0" + bin: + cssesc: bin/cssesc + checksum: 10c0/6bcfd898662671be15ae7827120472c5667afb3d7429f1f917737f3bf84c4176003228131b643ae74543f17a394446247df090c597bb9a728cce298606ed0aa7 + languageName: node + linkType: hard + +"debug@npm:4, debug@npm:^4.1.1, debug@npm:^4.3.1, debug@npm:^4.3.2, debug@npm:^4.3.4": + version: 4.4.1 + resolution: "debug@npm:4.4.1" + dependencies: + ms: "npm:^2.1.3" + peerDependenciesMeta: + supports-color: + optional: true + checksum: 10c0/d2b44bc1afd912b49bb7ebb0d50a860dc93a4dd7d946e8de94abc957bb63726b7dd5aa48c18c2386c379ec024c46692e15ed3ed97d481729f929201e671fcd55 + languageName: node + linkType: hard + +"decamelize@npm:1.2.0": + version: 1.2.0 + resolution: "decamelize@npm:1.2.0" + checksum: 10c0/85c39fe8fbf0482d4a1e224ef0119db5c1897f8503bcef8b826adff7a1b11414972f6fef2d7dec2ee0b4be3863cf64ac1439137ae9e6af23a3d8dcbe26a5b4b2 + languageName: node + linkType: hard + +"dedent@npm:^1.5.3": + version: 1.6.0 + resolution: "dedent@npm:1.6.0" + peerDependencies: + babel-plugin-macros: ^3.1.0 + peerDependenciesMeta: + babel-plugin-macros: + optional: true + checksum: 10c0/671b8f5e390dd2a560862c4511dd6d2638e71911486f78cb32116551f8f2aa6fcaf50579ffffb2f866d46b5b80fd72470659ca5760ede8f967619ef7df79e8a5 + languageName: node + linkType: hard + +"deep-is@npm:^0.1.3": + version: 0.1.4 + resolution: "deep-is@npm:0.1.4" + checksum: 10c0/7f0ee496e0dff14a573dc6127f14c95061b448b87b995fc96c017ce0a1e66af1675e73f1d6064407975bc4ea6ab679497a29fff7b5b9c4e99cb10797c1ad0b4c + languageName: node + linkType: hard + +"default-browser-id@npm:^5.0.0": + version: 5.0.0 + resolution: "default-browser-id@npm:5.0.0" + checksum: 10c0/957fb886502594c8e645e812dfe93dba30ed82e8460d20ce39c53c5b0f3e2afb6ceaec2249083b90bdfbb4cb0f34e1f73fde3d68cac00becdbcfd894156b5ead + languageName: node + linkType: hard + +"default-browser@npm:^5.2.1": + version: 5.2.1 + resolution: "default-browser@npm:5.2.1" + dependencies: + bundle-name: "npm:^4.1.0" + default-browser-id: "npm:^5.0.0" + checksum: 10c0/73f17dc3c58026c55bb5538749597db31f9561c0193cd98604144b704a981c95a466f8ecc3c2db63d8bfd04fb0d426904834cfc91ae510c6aeb97e13c5167c4d + languageName: node + linkType: hard + +"define-lazy-prop@npm:^3.0.0": + version: 3.0.0 + resolution: "define-lazy-prop@npm:3.0.0" + checksum: 10c0/5ab0b2bf3fa58b3a443140bbd4cd3db1f91b985cc8a246d330b9ac3fc0b6a325a6d82bddc0b055123d745b3f9931afeea74a5ec545439a1630b9c8512b0eeb49 + languageName: node + linkType: hard + +"detect-libc@npm:^2.0.3, detect-libc@npm:^2.0.4": + version: 2.0.4 + resolution: "detect-libc@npm:2.0.4" + checksum: 10c0/c15541f836eba4b1f521e4eecc28eefefdbc10a94d3b8cb4c507689f332cc111babb95deda66f2de050b22122113189986d5190be97d51b5a2b23b938415e67c + languageName: node + linkType: hard + +"dotenv@npm:^16.4.7": + version: 16.6.1 + resolution: "dotenv@npm:16.6.1" + checksum: 10c0/15ce56608326ea0d1d9414a5c8ee6dcf0fffc79d2c16422b4ac2268e7e2d76ff5a572d37ffe747c377de12005f14b3cc22361e79fc7f1061cce81f77d2c973dc + languageName: node + linkType: hard + +"eastasianwidth@npm:^0.2.0": + version: 0.2.0 + resolution: "eastasianwidth@npm:0.2.0" + checksum: 10c0/26f364ebcdb6395f95124fda411f63137a4bfb5d3a06453f7f23dfe52502905bd84e0488172e0f9ec295fdc45f05c23d5d91baf16bd26f0fe9acd777a188dc39 + languageName: node + linkType: hard + +"electron-to-chromium@npm:^1.5.173": + version: 1.5.191 + resolution: "electron-to-chromium@npm:1.5.191" + checksum: 10c0/26b22ec2ae2a152da09f062d8582e54384a15ddc2a27149cdc2747a0c3f46154370a37b9e687de2d6d71ea1ebc1319f8394283ffb1581f1d4495cdefffd7a2a6 + languageName: node + linkType: hard + +"emoji-regex@npm:^8.0.0": + version: 8.0.0 + resolution: "emoji-regex@npm:8.0.0" + checksum: 10c0/b6053ad39951c4cf338f9092d7bfba448cdfd46fe6a2a034700b149ac9ffbc137e361cbd3c442297f86bed2e5f7576c1b54cc0a6bf8ef5106cc62f496af35010 + languageName: node + linkType: hard + +"emoji-regex@npm:^9.2.2": + version: 9.2.2 + resolution: "emoji-regex@npm:9.2.2" + checksum: 10c0/af014e759a72064cf66e6e694a7fc6b0ed3d8db680427b021a89727689671cefe9d04151b2cad51dbaf85d5ba790d061cd167f1cf32eb7b281f6368b3c181639 + languageName: node + linkType: hard + +"enabled@npm:2.0.x": + version: 2.0.0 + resolution: "enabled@npm:2.0.0" + checksum: 10c0/3b2c2af9bc7f8b9e291610f2dde4a75cf6ee52a68f4dd585482fbdf9a55d65388940e024e56d40bb03e05ef6671f5f53021fa8b72a20e954d7066ec28166713f + languageName: node + linkType: hard + +"encoding@npm:^0.1.13": + version: 0.1.13 + resolution: "encoding@npm:0.1.13" + dependencies: + iconv-lite: "npm:^0.6.2" + checksum: 10c0/36d938712ff00fe1f4bac88b43bcffb5930c1efa57bbcdca9d67e1d9d6c57cfb1200fb01efe0f3109b2ce99b231f90779532814a81370a1bd3274a0f58585039 + languageName: node + linkType: hard + +"enhanced-resolve@npm:^5.18.1": + version: 5.18.2 + resolution: "enhanced-resolve@npm:5.18.2" + dependencies: + graceful-fs: "npm:^4.2.4" + tapable: "npm:^2.2.0" + checksum: 10c0/2a45105daded694304b0298d1c0351a981842249a9867513d55e41321a4ccf37dfd35b0c1e9ceae290eab73654b09aa7a910d618ea6f9441e97c52bc424a2372 + languageName: node + linkType: hard + +"env-paths@npm:^2.2.0": + version: 2.2.1 + resolution: "env-paths@npm:2.2.1" + checksum: 10c0/285325677bf00e30845e330eec32894f5105529db97496ee3f598478e50f008c5352a41a30e5e72ec9de8a542b5a570b85699cd63bd2bc646dbcb9f311d83bc4 + languageName: node + linkType: hard + +"err-code@npm:^2.0.2": + version: 2.0.3 + resolution: "err-code@npm:2.0.3" + checksum: 10c0/b642f7b4dd4a376e954947550a3065a9ece6733ab8e51ad80db727aaae0817c2e99b02a97a3d6cecc648a97848305e728289cf312d09af395403a90c9d4d8a66 + languageName: node + linkType: hard + +"esbuild-plugin-tailwindcss@npm:^2.0.1": + version: 2.0.1 + resolution: "esbuild-plugin-tailwindcss@npm:2.0.1" + dependencies: + "@tailwindcss/postcss": "npm:^4.0.5" + autoprefixer: "npm:^10.4.20" + postcss: "npm:^8.5.1" + postcss-modules: "npm:^6.0.1" + checksum: 10c0/74a7ea474c87f4b99cd31873bc56ca269d48990bd9084d473c71d81503b2cebd365062316e8647c3d293425377b315578268d19769c4c3b461bc05e7b5417b6c + languageName: node + linkType: hard + +"esbuild@npm:^0.25.0, esbuild@npm:~0.25.0": + version: 0.25.8 + resolution: "esbuild@npm:0.25.8" + dependencies: + "@esbuild/aix-ppc64": "npm:0.25.8" + "@esbuild/android-arm": "npm:0.25.8" + "@esbuild/android-arm64": "npm:0.25.8" + "@esbuild/android-x64": "npm:0.25.8" + "@esbuild/darwin-arm64": "npm:0.25.8" + "@esbuild/darwin-x64": "npm:0.25.8" + "@esbuild/freebsd-arm64": "npm:0.25.8" + "@esbuild/freebsd-x64": "npm:0.25.8" + "@esbuild/linux-arm": "npm:0.25.8" + "@esbuild/linux-arm64": "npm:0.25.8" + "@esbuild/linux-ia32": "npm:0.25.8" + "@esbuild/linux-loong64": "npm:0.25.8" + "@esbuild/linux-mips64el": "npm:0.25.8" + "@esbuild/linux-ppc64": "npm:0.25.8" + "@esbuild/linux-riscv64": "npm:0.25.8" + "@esbuild/linux-s390x": "npm:0.25.8" + "@esbuild/linux-x64": "npm:0.25.8" + "@esbuild/netbsd-arm64": "npm:0.25.8" + "@esbuild/netbsd-x64": "npm:0.25.8" + "@esbuild/openbsd-arm64": "npm:0.25.8" + "@esbuild/openbsd-x64": "npm:0.25.8" + "@esbuild/openharmony-arm64": "npm:0.25.8" + "@esbuild/sunos-x64": "npm:0.25.8" + "@esbuild/win32-arm64": "npm:0.25.8" + "@esbuild/win32-ia32": "npm:0.25.8" + "@esbuild/win32-x64": "npm:0.25.8" + dependenciesMeta: + "@esbuild/aix-ppc64": + optional: true + "@esbuild/android-arm": + optional: true + "@esbuild/android-arm64": + optional: true + "@esbuild/android-x64": + optional: true + "@esbuild/darwin-arm64": + optional: true + "@esbuild/darwin-x64": + optional: true + "@esbuild/freebsd-arm64": + optional: true + "@esbuild/freebsd-x64": + optional: true + "@esbuild/linux-arm": + optional: true + "@esbuild/linux-arm64": + optional: true + "@esbuild/linux-ia32": + optional: true + "@esbuild/linux-loong64": + optional: true + "@esbuild/linux-mips64el": + optional: true + "@esbuild/linux-ppc64": + optional: true + "@esbuild/linux-riscv64": + optional: true + "@esbuild/linux-s390x": + optional: true + "@esbuild/linux-x64": + optional: true + "@esbuild/netbsd-arm64": + optional: true + "@esbuild/netbsd-x64": + optional: true + "@esbuild/openbsd-arm64": + optional: true + "@esbuild/openbsd-x64": + optional: true + "@esbuild/openharmony-arm64": + optional: true + "@esbuild/sunos-x64": + optional: true + "@esbuild/win32-arm64": + optional: true + "@esbuild/win32-ia32": + optional: true + "@esbuild/win32-x64": + optional: true + bin: + esbuild: bin/esbuild + checksum: 10c0/43747a25e120d5dd9ce75c82f57306580d715647c8db4f4a0a84e73b04cf16c27572d3937d3cfb95d5ac3266a4d1bbd3913e3d76ae719693516289fc86f8a5fd + languageName: node + linkType: hard + +"escalade@npm:^3.2.0": + version: 3.2.0 + resolution: "escalade@npm:3.2.0" + checksum: 10c0/ced4dd3a78e15897ed3be74e635110bbf3b08877b0a41be50dcb325ee0e0b5f65fc2d50e9845194d7c4633f327e2e1c6cce00a71b617c5673df0374201d67f65 + languageName: node + linkType: hard + +"escape-string-regexp@npm:^4.0.0": + version: 4.0.0 + resolution: "escape-string-regexp@npm:4.0.0" + checksum: 10c0/9497d4dd307d845bd7f75180d8188bb17ea8c151c1edbf6b6717c100e104d629dc2dfb687686181b0f4b7d732c7dfdc4d5e7a8ff72de1b0ca283a75bbb3a9cd9 + languageName: node + linkType: hard + +"eslint-scope@npm:^8.4.0": + version: 8.4.0 + resolution: "eslint-scope@npm:8.4.0" + dependencies: + esrecurse: "npm:^4.3.0" + estraverse: "npm:^5.2.0" + checksum: 10c0/407f6c600204d0f3705bd557f81bd0189e69cd7996f408f8971ab5779c0af733d1af2f1412066b40ee1588b085874fc37a2333986c6521669cdbdd36ca5058e0 + languageName: node + linkType: hard + +"eslint-visitor-keys@npm:^3.4.3": + version: 3.4.3 + resolution: "eslint-visitor-keys@npm:3.4.3" + checksum: 10c0/92708e882c0a5ffd88c23c0b404ac1628cf20104a108c745f240a13c332a11aac54f49a22d5762efbffc18ecbc9a580d1b7ad034bf5f3cc3307e5cbff2ec9820 + languageName: node + linkType: hard + +"eslint-visitor-keys@npm:^4.2.1": + version: 4.2.1 + resolution: "eslint-visitor-keys@npm:4.2.1" + checksum: 10c0/fcd43999199d6740db26c58dbe0c2594623e31ca307e616ac05153c9272f12f1364f5a0b1917a8e962268fdecc6f3622c1c2908b4fcc2e047a106fe6de69dc43 + languageName: node + linkType: hard + +"eslint@npm:^9.32.0": + version: 9.32.0 + resolution: "eslint@npm:9.32.0" + dependencies: + "@eslint-community/eslint-utils": "npm:^4.2.0" + "@eslint-community/regexpp": "npm:^4.12.1" + "@eslint/config-array": "npm:^0.21.0" + "@eslint/config-helpers": "npm:^0.3.0" + "@eslint/core": "npm:^0.15.0" + "@eslint/eslintrc": "npm:^3.3.1" + "@eslint/js": "npm:9.32.0" + "@eslint/plugin-kit": "npm:^0.3.4" + "@humanfs/node": "npm:^0.16.6" + "@humanwhocodes/module-importer": "npm:^1.0.1" + "@humanwhocodes/retry": "npm:^0.4.2" + "@types/estree": "npm:^1.0.6" + "@types/json-schema": "npm:^7.0.15" + ajv: "npm:^6.12.4" + chalk: "npm:^4.0.0" + cross-spawn: "npm:^7.0.6" + debug: "npm:^4.3.2" + escape-string-regexp: "npm:^4.0.0" + eslint-scope: "npm:^8.4.0" + eslint-visitor-keys: "npm:^4.2.1" + espree: "npm:^10.4.0" + esquery: "npm:^1.5.0" + esutils: "npm:^2.0.2" + fast-deep-equal: "npm:^3.1.3" + file-entry-cache: "npm:^8.0.0" + find-up: "npm:^5.0.0" + glob-parent: "npm:^6.0.2" + ignore: "npm:^5.2.0" + imurmurhash: "npm:^0.1.4" + is-glob: "npm:^4.0.0" + json-stable-stringify-without-jsonify: "npm:^1.0.1" + lodash.merge: "npm:^4.6.2" + minimatch: "npm:^3.1.2" + natural-compare: "npm:^1.4.0" + optionator: "npm:^0.9.3" + peerDependencies: + jiti: "*" + peerDependenciesMeta: + jiti: + optional: true + bin: + eslint: bin/eslint.js + checksum: 10c0/e8a23924ec5f8b62e95483002ca25db74e25c23bd9c6d98a9f656ee32f820169bee3bfdf548ec728b16694f198b3db857d85a49210ee4a035242711d08fdc602 + languageName: node + linkType: hard + +"espree@npm:^10.0.1, espree@npm:^10.4.0": + version: 10.4.0 + resolution: "espree@npm:10.4.0" + dependencies: + acorn: "npm:^8.15.0" + acorn-jsx: "npm:^5.3.2" + eslint-visitor-keys: "npm:^4.2.1" + checksum: 10c0/c63fe06131c26c8157b4083313cb02a9a54720a08e21543300e55288c40e06c3fc284bdecf108d3a1372c5934a0a88644c98714f38b6ae8ed272b40d9ea08d6b + languageName: node + linkType: hard + +"esquery@npm:^1.5.0": + version: 1.6.0 + resolution: "esquery@npm:1.6.0" + dependencies: + estraverse: "npm:^5.1.0" + checksum: 10c0/cb9065ec605f9da7a76ca6dadb0619dfb611e37a81e318732977d90fab50a256b95fee2d925fba7c2f3f0523aa16f91587246693bc09bc34d5a59575fe6e93d2 + languageName: node + linkType: hard + +"esrecurse@npm:^4.3.0": + version: 4.3.0 + resolution: "esrecurse@npm:4.3.0" + dependencies: + estraverse: "npm:^5.2.0" + checksum: 10c0/81a37116d1408ded88ada45b9fb16dbd26fba3aadc369ce50fcaf82a0bac12772ebd7b24cd7b91fc66786bf2c1ac7b5f196bc990a473efff972f5cb338877cf5 + languageName: node + linkType: hard + +"estraverse@npm:^5.1.0, estraverse@npm:^5.2.0": + version: 5.3.0 + resolution: "estraverse@npm:5.3.0" + checksum: 10c0/1ff9447b96263dec95d6d67431c5e0771eb9776427421260a3e2f0fdd5d6bd4f8e37a7338f5ad2880c9f143450c9b1e4fc2069060724570a49cf9cf0312bd107 + languageName: node + linkType: hard + +"esutils@npm:^2.0.2": + version: 2.0.3 + resolution: "esutils@npm:2.0.3" + checksum: 10c0/9a2fe69a41bfdade834ba7c42de4723c97ec776e40656919c62cbd13607c45e127a003f05f724a1ea55e5029a4cf2de444b13009f2af71271e42d93a637137c7 + languageName: node + linkType: hard + +"eventemitter3@npm:^4.0.4": + version: 4.0.7 + resolution: "eventemitter3@npm:4.0.7" + checksum: 10c0/5f6d97cbcbac47be798e6355e3a7639a84ee1f7d9b199a07017f1d2f1e2fe236004d14fa5dfaeba661f94ea57805385e326236a6debbc7145c8877fbc0297c6b + languageName: node + linkType: hard + +"exit-hook@npm:^4.0.0": + version: 4.0.0 + resolution: "exit-hook@npm:4.0.0" + checksum: 10c0/7fb33eaeb9050aee9479da9c93d42b796fb409c40e1d2b6ea2f40786ae7d7db6dc6a0f6ecc7bc24e479f957b7844bcb880044ded73320334743c64e3ecef48d7 + languageName: node + linkType: hard + +"exponential-backoff@npm:^3.1.1": + version: 3.1.2 + resolution: "exponential-backoff@npm:3.1.2" + checksum: 10c0/d9d3e1eafa21b78464297df91f1776f7fbaa3d5e3f7f0995648ca5b89c069d17055033817348d9f4a43d1c20b0eab84f75af6991751e839df53e4dfd6f22e844 + languageName: node + linkType: hard + +"fast-deep-equal@npm:^3.1.1, fast-deep-equal@npm:^3.1.3": + version: 3.1.3 + resolution: "fast-deep-equal@npm:3.1.3" + checksum: 10c0/40dedc862eb8992c54579c66d914635afbec43350afbbe991235fdcb4e3a8d5af1b23ae7e79bef7d4882d0ecee06c3197488026998fb19f72dc95acff1d1b1d0 + languageName: node + linkType: hard + +"fast-glob@npm:^3.3.2": + version: 3.3.3 + resolution: "fast-glob@npm:3.3.3" + dependencies: + "@nodelib/fs.stat": "npm:^2.0.2" + "@nodelib/fs.walk": "npm:^1.2.3" + glob-parent: "npm:^5.1.2" + merge2: "npm:^1.3.0" + micromatch: "npm:^4.0.8" + checksum: 10c0/f6aaa141d0d3384cf73cbcdfc52f475ed293f6d5b65bfc5def368b09163a9f7e5ec2b3014d80f733c405f58e470ee0cc451c2937685045cddcdeaa24199c43fe + languageName: node + linkType: hard + +"fast-json-stable-stringify@npm:^2.0.0": + version: 2.1.0 + resolution: "fast-json-stable-stringify@npm:2.1.0" + checksum: 10c0/7f081eb0b8a64e0057b3bb03f974b3ef00135fbf36c1c710895cd9300f13c94ba809bb3a81cf4e1b03f6e5285610a61abbd7602d0652de423144dfee5a389c9b + languageName: node + linkType: hard + +"fast-levenshtein@npm:^2.0.6": + version: 2.0.6 + resolution: "fast-levenshtein@npm:2.0.6" + checksum: 10c0/111972b37338bcb88f7d9e2c5907862c280ebf4234433b95bc611e518d192ccb2d38119c4ac86e26b668d75f7f3894f4ff5c4982899afced7ca78633b08287c4 + languageName: node + linkType: hard + +"fast-xml-parser@npm:^4.4.1": + version: 4.5.3 + resolution: "fast-xml-parser@npm:4.5.3" + dependencies: + strnum: "npm:^1.1.1" + bin: + fxparser: src/cli/cli.js + checksum: 10c0/bf9ccadacfadc95f6e3f0e7882a380a7f219cf0a6f96575149f02cb62bf44c3b7f0daee75b8ff3847bcfd7fbcb201e402c71045936c265cf6d94b141ec4e9327 + languageName: node + linkType: hard + +"fastq@npm:^1.6.0": + version: 1.19.1 + resolution: "fastq@npm:1.19.1" + dependencies: + reusify: "npm:^1.0.4" + checksum: 10c0/ebc6e50ac7048daaeb8e64522a1ea7a26e92b3cee5cd1c7f2316cdca81ba543aa40a136b53891446ea5c3a67ec215fbaca87ad405f102dd97012f62916905630 + languageName: node + linkType: hard + +"fdir@npm:^6.4.4": + version: 6.4.6 + resolution: "fdir@npm:6.4.6" + peerDependencies: + picomatch: ^3 || ^4 + peerDependenciesMeta: + picomatch: + optional: true + checksum: 10c0/45b559cff889934ebb8bc498351e5acba40750ada7e7d6bde197768d2fa67c149be8ae7f8ff34d03f4e1eb20f2764116e56440aaa2f6689e9a4aa7ef06acafe9 + languageName: node + linkType: hard + +"fecha@npm:^4.2.0": + version: 4.2.3 + resolution: "fecha@npm:4.2.3" + checksum: 10c0/0e895965959cf6a22bb7b00f0bf546f2783836310f510ddf63f463e1518d4c96dec61ab33fdfd8e79a71b4856a7c865478ce2ee8498d560fe125947703c9b1cf + languageName: node + linkType: hard + +"file-entry-cache@npm:^8.0.0": + version: 8.0.0 + resolution: "file-entry-cache@npm:8.0.0" + dependencies: + flat-cache: "npm:^4.0.0" + checksum: 10c0/9e2b5938b1cd9b6d7e3612bdc533afd4ac17b2fc646569e9a8abbf2eb48e5eb8e316bc38815a3ef6a1b456f4107f0d0f055a614ca613e75db6bf9ff4d72c1638 + languageName: node + linkType: hard + +"fill-range@npm:^7.1.1": + version: 7.1.1 + resolution: "fill-range@npm:7.1.1" + dependencies: + to-regex-range: "npm:^5.0.1" + checksum: 10c0/b75b691bbe065472f38824f694c2f7449d7f5004aa950426a2c28f0306c60db9b880c0b0e4ed819997ffb882d1da02cfcfc819bddc94d71627f5269682edf018 + languageName: node + linkType: hard + +"find-up@npm:^5.0.0": + version: 5.0.0 + resolution: "find-up@npm:5.0.0" + dependencies: + locate-path: "npm:^6.0.0" + path-exists: "npm:^4.0.0" + checksum: 10c0/062c5a83a9c02f53cdd6d175a37ecf8f87ea5bbff1fdfb828f04bfa021441bc7583e8ebc0872a4c1baab96221fb8a8a275a19809fb93fbc40bd69ec35634069a + languageName: node + linkType: hard + +"flat-cache@npm:^4.0.0": + version: 4.0.1 + resolution: "flat-cache@npm:4.0.1" + dependencies: + flatted: "npm:^3.2.9" + keyv: "npm:^4.5.4" + checksum: 10c0/2c59d93e9faa2523e4fda6b4ada749bed432cfa28c8e251f33b25795e426a1c6dbada777afb1f74fcfff33934fdbdea921ee738fcc33e71adc9d6eca984a1cfc + languageName: node + linkType: hard + +"flatted@npm:^3.2.9": + version: 3.3.3 + resolution: "flatted@npm:3.3.3" + checksum: 10c0/e957a1c6b0254aa15b8cce8533e24165abd98fadc98575db082b786b5da1b7d72062b81bfdcd1da2f4d46b6ed93bec2434e62333e9b4261d79ef2e75a10dd538 + languageName: node + linkType: hard + +"fn.name@npm:1.x.x": + version: 1.1.0 + resolution: "fn.name@npm:1.1.0" + checksum: 10c0/8ad62aa2d4f0b2a76d09dba36cfec61c540c13a0fd72e5d94164e430f987a7ce6a743112bbeb14877c810ef500d1f73d7f56e76d029d2e3413f20d79e3460a9a + languageName: node + linkType: hard + +"foreground-child@npm:^3.1.0": + version: 3.3.1 + resolution: "foreground-child@npm:3.3.1" + dependencies: + cross-spawn: "npm:^7.0.6" + signal-exit: "npm:^4.0.1" + checksum: 10c0/8986e4af2430896e65bc2788d6679067294d6aee9545daefc84923a0a4b399ad9c7a3ea7bd8c0b2b80fdf4a92de4c69df3f628233ff3224260e9c1541a9e9ed3 + languageName: node + linkType: hard + +"fraction.js@npm:^4.3.7": + version: 4.3.7 + resolution: "fraction.js@npm:4.3.7" + checksum: 10c0/df291391beea9ab4c263487ffd9d17fed162dbb736982dee1379b2a8cc94e4e24e46ed508c6d278aded9080ba51872f1bc5f3a5fd8d7c74e5f105b508ac28711 + languageName: node + linkType: hard + +"fs-minipass@npm:^3.0.0": + version: 3.0.3 + resolution: "fs-minipass@npm:3.0.3" + dependencies: + minipass: "npm:^7.0.3" + checksum: 10c0/63e80da2ff9b621e2cb1596abcb9207f1cf82b968b116ccd7b959e3323144cce7fb141462200971c38bbf2ecca51695069db45265705bed09a7cd93ae5b89f94 + languageName: node + linkType: hard + +"fsevents@npm:~2.3.3": + version: 2.3.3 + resolution: "fsevents@npm:2.3.3" + dependencies: + node-gyp: "npm:latest" + checksum: 10c0/a1f0c44595123ed717febbc478aa952e47adfc28e2092be66b8ab1635147254ca6cfe1df792a8997f22716d4cbafc73309899ff7bfac2ac3ad8cf2e4ecc3ec60 + conditions: os=darwin + languageName: node + linkType: hard + +"fsevents@patch:fsevents@npm%3A~2.3.3#optional!builtin<compat/fsevents>": + version: 2.3.3 + resolution: "fsevents@patch:fsevents@npm%3A2.3.3#optional!builtin<compat/fsevents>::version=2.3.3&hash=df0bf1" + dependencies: + node-gyp: "npm:latest" + conditions: os=darwin + languageName: node + linkType: hard + +"generic-names@npm:^4.0.0": + version: 4.0.0 + resolution: "generic-names@npm:4.0.0" + dependencies: + loader-utils: "npm:^3.2.0" + checksum: 10c0/4e2be864535fadceed4e803fefc1df7f85447d9479d51e611a8a43a2c96533422b62c8fae84d9eb10cc21ee3de569a8c29d5ba68978ae930cccc9cb43b9a36d1 + languageName: node + linkType: hard + +"get-tsconfig@npm:^4.7.5": + version: 4.10.1 + resolution: "get-tsconfig@npm:4.10.1" + dependencies: + resolve-pkg-maps: "npm:^1.0.0" + checksum: 10c0/7f8e3dabc6a49b747920a800fb88e1952fef871cdf51b79e98db48275a5de6cdaf499c55ee67df5fa6fe7ce65f0063e26de0f2e53049b408c585aa74d39ffa21 + languageName: node + linkType: hard + +"glob-parent@npm:^5.1.2": + version: 5.1.2 + resolution: "glob-parent@npm:5.1.2" + dependencies: + is-glob: "npm:^4.0.1" + checksum: 10c0/cab87638e2112bee3f839ef5f6e0765057163d39c66be8ec1602f3823da4692297ad4e972de876ea17c44d652978638d2fd583c6713d0eb6591706825020c9ee + languageName: node + linkType: hard + +"glob-parent@npm:^6.0.2": + version: 6.0.2 + resolution: "glob-parent@npm:6.0.2" + dependencies: + is-glob: "npm:^4.0.3" + checksum: 10c0/317034d88654730230b3f43bb7ad4f7c90257a426e872ea0bf157473ac61c99bf5d205fad8f0185f989be8d2fa6d3c7dce1645d99d545b6ea9089c39f838e7f8 + languageName: node + linkType: hard + +"glob@npm:^10.2.2": + version: 10.4.5 + resolution: "glob@npm:10.4.5" + dependencies: + foreground-child: "npm:^3.1.0" + jackspeak: "npm:^3.1.2" + minimatch: "npm:^9.0.4" + minipass: "npm:^7.1.2" + package-json-from-dist: "npm:^1.0.0" + path-scurry: "npm:^1.11.1" + bin: + glob: dist/esm/bin.mjs + checksum: 10c0/19a9759ea77b8e3ca0a43c2f07ecddc2ad46216b786bb8f993c445aee80d345925a21e5280c7b7c6c59e860a0154b84e4b2b60321fea92cd3c56b4a7489f160e + languageName: node + linkType: hard + +"globals@npm:^14.0.0": + version: 14.0.0 + resolution: "globals@npm:14.0.0" + checksum: 10c0/b96ff42620c9231ad468d4c58ff42afee7777ee1c963013ff8aabe095a451d0ceeb8dcd8ef4cbd64d2538cef45f787a78ba3a9574f4a634438963e334471302d + languageName: node + linkType: hard + +"globals@npm:^16.3.0": + version: 16.3.0 + resolution: "globals@npm:16.3.0" + checksum: 10c0/c62dc20357d1c0bf2be4545d6c4141265d1a229bf1c3294955efb5b5ef611145391895e3f2729f8603809e81b30b516c33e6c2597573844449978606aad6eb38 + languageName: node + linkType: hard + +"graceful-fs@npm:^4.2.4, graceful-fs@npm:^4.2.6": + version: 4.2.11 + resolution: "graceful-fs@npm:4.2.11" + checksum: 10c0/386d011a553e02bc594ac2ca0bd6d9e4c22d7fa8cfbfc448a6d148c59ea881b092db9dbe3547ae4b88e55f1b01f7c4a2ecc53b310c042793e63aa44cf6c257f2 + languageName: node + linkType: hard + +"graphemer@npm:^1.4.0": + version: 1.4.0 + resolution: "graphemer@npm:1.4.0" + checksum: 10c0/e951259d8cd2e0d196c72ec711add7115d42eb9a8146c8eeda5b8d3ac91e5dd816b9cd68920726d9fd4490368e7ed86e9c423f40db87e2d8dfafa00fa17c3a31 + languageName: node + linkType: hard + +"has-flag@npm:^4.0.0": + version: 4.0.0 + resolution: "has-flag@npm:4.0.0" + checksum: 10c0/2e789c61b7888d66993e14e8331449e525ef42aac53c627cc53d1c3334e768bcb6abdc4f5f0de1478a25beec6f0bd62c7549058b7ac53e924040d4f301f02fd1 + languageName: node + linkType: hard + +"hono@npm:^4.5.4": + version: 4.8.9 + resolution: "hono@npm:4.8.9" + checksum: 10c0/385539d1787fdc747bc869ef0e5ccc9f39cbe40289b94f23eecfc82c6ca440f059704647cd6381a5066d2cf7baa43ab25184c78d44af4c5c98a5c5b07670059e + languageName: node + linkType: hard + +"http-cache-semantics@npm:^4.1.1": + version: 4.2.0 + resolution: "http-cache-semantics@npm:4.2.0" + checksum: 10c0/45b66a945cf13ec2d1f29432277201313babf4a01d9e52f44b31ca923434083afeca03f18417f599c9ab3d0e7b618ceb21257542338b57c54b710463b4a53e37 + languageName: node + linkType: hard + +"http-proxy-agent@npm:^7.0.0": + version: 7.0.2 + resolution: "http-proxy-agent@npm:7.0.2" + dependencies: + agent-base: "npm:^7.1.0" + debug: "npm:^4.3.4" + checksum: 10c0/4207b06a4580fb85dd6dff521f0abf6db517489e70863dca1a0291daa7f2d3d2d6015a57bd702af068ea5cf9f1f6ff72314f5f5b4228d299c0904135d2aef921 + languageName: node + linkType: hard + +"https-proxy-agent@npm:^7.0.1": + version: 7.0.6 + resolution: "https-proxy-agent@npm:7.0.6" + dependencies: + agent-base: "npm:^7.1.2" + debug: "npm:4" + checksum: 10c0/f729219bc735edb621fa30e6e84e60ee5d00802b8247aac0d7b79b0bd6d4b3294737a337b93b86a0bd9e68099d031858a39260c976dc14cdbba238ba1f8779ac + languageName: node + linkType: hard + +"iconv-lite@npm:^0.6.2": + version: 0.6.3 + resolution: "iconv-lite@npm:0.6.3" + dependencies: + safer-buffer: "npm:>= 2.1.2 < 3.0.0" + checksum: 10c0/98102bc66b33fcf5ac044099d1257ba0b7ad5e3ccd3221f34dd508ab4070edff183276221684e1e0555b145fce0850c9f7d2b60a9fcac50fbb4ea0d6e845a3b1 + languageName: node + linkType: hard + +"icss-utils@npm:^5.0.0, icss-utils@npm:^5.1.0": + version: 5.1.0 + resolution: "icss-utils@npm:5.1.0" + peerDependencies: + postcss: ^8.1.0 + checksum: 10c0/39c92936fabd23169c8611d2b5cc39e39d10b19b0d223352f20a7579f75b39d5f786114a6b8fc62bee8c5fed59ba9e0d38f7219a4db383e324fb3061664b043d + languageName: node + linkType: hard + +"ignore@npm:^5.2.0": + version: 5.3.2 + resolution: "ignore@npm:5.3.2" + checksum: 10c0/f9f652c957983634ded1e7f02da3b559a0d4cc210fca3792cb67f1b153623c9c42efdc1c4121af171e295444459fc4a9201101fb041b1104a3c000bccb188337 + languageName: node + linkType: hard + +"ignore@npm:^7.0.0": + version: 7.0.5 + resolution: "ignore@npm:7.0.5" + checksum: 10c0/ae00db89fe873064a093b8999fe4cc284b13ef2a178636211842cceb650b9c3e390d3339191acb145d81ed5379d2074840cf0c33a20bdbd6f32821f79eb4ad5d + languageName: node + linkType: hard + +"import-fresh@npm:^3.2.1": + version: 3.3.1 + resolution: "import-fresh@npm:3.3.1" + dependencies: + parent-module: "npm:^1.0.0" + resolve-from: "npm:^4.0.0" + checksum: 10c0/bf8cc494872fef783249709385ae883b447e3eb09db0ebd15dcead7d9afe7224dad7bd7591c6b73b0b19b3c0f9640eb8ee884f01cfaf2887ab995b0b36a0cbec + languageName: node + linkType: hard + +"imurmurhash@npm:^0.1.4": + version: 0.1.4 + resolution: "imurmurhash@npm:0.1.4" + checksum: 10c0/8b51313850dd33605c6c9d3fd9638b714f4c4c40250cff658209f30d40da60f78992fb2df5dabee4acf589a6a82bbc79ad5486550754bd9ec4e3fc0d4a57d6a6 + languageName: node + linkType: hard + +"inherits@npm:^2.0.3": + version: 2.0.4 + resolution: "inherits@npm:2.0.4" + checksum: 10c0/4e531f648b29039fb7426fb94075e6545faa1eb9fe83c29f0b6d9e7263aceb4289d2d4557db0d428188eeb449cc7c5e77b0a0b2c4e248ff2a65933a0dee49ef2 + languageName: node + linkType: hard + +"ip-address@npm:^9.0.5": + version: 9.0.5 + resolution: "ip-address@npm:9.0.5" + dependencies: + jsbn: "npm:1.1.0" + sprintf-js: "npm:^1.1.3" + checksum: 10c0/331cd07fafcb3b24100613e4b53e1a2b4feab11e671e655d46dc09ee233da5011284d09ca40c4ecbdfe1d0004f462958675c224a804259f2f78d2465a87824bc + languageName: node + linkType: hard + +"is-arrayish@npm:^0.3.1": + version: 0.3.2 + resolution: "is-arrayish@npm:0.3.2" + checksum: 10c0/f59b43dc1d129edb6f0e282595e56477f98c40278a2acdc8b0a5c57097c9eff8fe55470493df5775478cf32a4dc8eaf6d3a749f07ceee5bc263a78b2434f6a54 + languageName: node + linkType: hard + +"is-docker@npm:^3.0.0": + version: 3.0.0 + resolution: "is-docker@npm:3.0.0" + bin: + is-docker: cli.js + checksum: 10c0/d2c4f8e6d3e34df75a5defd44991b6068afad4835bb783b902fa12d13ebdb8f41b2a199dcb0b5ed2cb78bfee9e4c0bbdb69c2d9646f4106464674d3e697a5856 + languageName: node + linkType: hard + +"is-extglob@npm:^2.1.1": + version: 2.1.1 + resolution: "is-extglob@npm:2.1.1" + checksum: 10c0/5487da35691fbc339700bbb2730430b07777a3c21b9ebaecb3072512dfd7b4ba78ac2381a87e8d78d20ea08affb3f1971b4af629173a6bf435ff8a4c47747912 + languageName: node + linkType: hard + +"is-fullwidth-code-point@npm:^3.0.0": + version: 3.0.0 + resolution: "is-fullwidth-code-point@npm:3.0.0" + checksum: 10c0/bb11d825e049f38e04c06373a8d72782eee0205bda9d908cc550ccb3c59b99d750ff9537982e01733c1c94a58e35400661f57042158ff5e8f3e90cf936daf0fc + languageName: node + linkType: hard + +"is-glob@npm:^4.0.0, is-glob@npm:^4.0.1, is-glob@npm:^4.0.3": + version: 4.0.3 + resolution: "is-glob@npm:4.0.3" + dependencies: + is-extglob: "npm:^2.1.1" + checksum: 10c0/17fb4014e22be3bbecea9b2e3a76e9e34ff645466be702f1693e8f1ee1adac84710d0be0bd9f967d6354036fd51ab7c2741d954d6e91dae6bb69714de92c197a + languageName: node + linkType: hard + +"is-inside-container@npm:^1.0.0": + version: 1.0.0 + resolution: "is-inside-container@npm:1.0.0" + dependencies: + is-docker: "npm:^3.0.0" + bin: + is-inside-container: cli.js + checksum: 10c0/a8efb0e84f6197e6ff5c64c52890fa9acb49b7b74fed4da7c95383965da6f0fa592b4dbd5e38a79f87fc108196937acdbcd758fcefc9b140e479b39ce1fcd1cd + languageName: node + linkType: hard + +"is-number@npm:^7.0.0": + version: 7.0.0 + resolution: "is-number@npm:7.0.0" + checksum: 10c0/b4686d0d3053146095ccd45346461bc8e53b80aeb7671cc52a4de02dbbf7dc0d1d2a986e2fe4ae206984b4d34ef37e8b795ebc4f4295c978373e6575e295d811 + languageName: node + linkType: hard + +"is-stream@npm:^2.0.0": + version: 2.0.1 + resolution: "is-stream@npm:2.0.1" + checksum: 10c0/7c284241313fc6efc329b8d7f08e16c0efeb6baab1b4cd0ba579eb78e5af1aa5da11e68559896a2067cd6c526bd29241dda4eb1225e627d5aa1a89a76d4635a5 + languageName: node + linkType: hard + +"is-what@npm:^4.1.8": + version: 4.1.16 + resolution: "is-what@npm:4.1.16" + checksum: 10c0/611f1947776826dcf85b57cfb7bd3b3ea6f4b94a9c2f551d4a53f653cf0cb9d1e6518846648256d46ee6c91d114b6d09d2ac8a07306f7430c5900f87466aae5b + languageName: node + linkType: hard + +"is-wsl@npm:^3.1.0": + version: 3.1.0 + resolution: "is-wsl@npm:3.1.0" + dependencies: + is-inside-container: "npm:^1.0.0" + checksum: 10c0/d3317c11995690a32c362100225e22ba793678fe8732660c6de511ae71a0ff05b06980cf21f98a6bf40d7be0e9e9506f859abe00a1118287d63e53d0a3d06947 + languageName: node + linkType: hard + +"isexe@npm:^2.0.0": + version: 2.0.0 + resolution: "isexe@npm:2.0.0" + checksum: 10c0/228cfa503fadc2c31596ab06ed6aa82c9976eec2bfd83397e7eaf06d0ccf42cd1dfd6743bf9aeb01aebd4156d009994c5f76ea898d2832c1fe342da923ca457d + languageName: node + linkType: hard + +"isexe@npm:^3.1.1": + version: 3.1.1 + resolution: "isexe@npm:3.1.1" + checksum: 10c0/9ec257654093443eb0a528a9c8cbba9c0ca7616ccb40abd6dde7202734d96bb86e4ac0d764f0f8cd965856aacbff2f4ce23e730dc19dfb41e3b0d865ca6fdcc7 + languageName: node + linkType: hard + +"jackspeak@npm:^3.1.2": + version: 3.4.3 + resolution: "jackspeak@npm:3.4.3" + dependencies: + "@isaacs/cliui": "npm:^8.0.2" + "@pkgjs/parseargs": "npm:^0.11.0" + dependenciesMeta: + "@pkgjs/parseargs": + optional: true + checksum: 10c0/6acc10d139eaefdbe04d2f679e6191b3abf073f111edf10b1de5302c97ec93fffeb2fdd8681ed17f16268aa9dd4f8c588ed9d1d3bffbbfa6e8bf897cbb3149b9 + languageName: node + linkType: hard + +"jiti@npm:^2.4.2, jiti@npm:^2.5.1": + version: 2.5.1 + resolution: "jiti@npm:2.5.1" + bin: + jiti: lib/jiti-cli.mjs + checksum: 10c0/f0a38d7d8842cb35ffe883038166aa2d52ffd21f1a4fc839ae4076ea7301c22a1f11373f8fc52e2667de7acde8f3e092835620dd6f72a0fbe9296b268b0874bb + languageName: node + linkType: hard + +"js-tiktoken@npm:^1.0.12": + version: 1.0.20 + resolution: "js-tiktoken@npm:1.0.20" + dependencies: + base64-js: "npm:^1.5.1" + checksum: 10c0/846c9257b25efa153695bb2a4a85f35e2e747fa76f03d04dcaffee5b64ce22ccd61db83af099492558375eabf18c8070369d0f88de07a9fcf4cb494bb9759dbd + languageName: node + linkType: hard + +"js-tokens@npm:^4.0.0": + version: 4.0.0 + resolution: "js-tokens@npm:4.0.0" + checksum: 10c0/e248708d377aa058eacf2037b07ded847790e6de892bbad3dac0abba2e759cb9f121b00099a65195616badcb6eca8d14d975cb3e89eb1cfda644756402c8aeed + languageName: node + linkType: hard + +"js-yaml@npm:^4.1.0": + version: 4.1.0 + resolution: "js-yaml@npm:4.1.0" + dependencies: + argparse: "npm:^2.0.1" + bin: + js-yaml: bin/js-yaml.js + checksum: 10c0/184a24b4eaacfce40ad9074c64fd42ac83cf74d8c8cd137718d456ced75051229e5061b8633c3366b8aada17945a7a356b337828c19da92b51ae62126575018f + languageName: node + linkType: hard + +"jsbn@npm:1.1.0": + version: 1.1.0 + resolution: "jsbn@npm:1.1.0" + checksum: 10c0/4f907fb78d7b712e11dea8c165fe0921f81a657d3443dde75359ed52eb2b5d33ce6773d97985a089f09a65edd80b11cb75c767b57ba47391fee4c969f7215c96 + languageName: node + linkType: hard + +"json-buffer@npm:3.0.1": + version: 3.0.1 + resolution: "json-buffer@npm:3.0.1" + checksum: 10c0/0d1c91569d9588e7eef2b49b59851f297f3ab93c7b35c7c221e288099322be6b562767d11e4821da500f3219542b9afd2e54c5dc573107c1126ed1080f8e96d7 + languageName: node + linkType: hard + +"json-schema-traverse@npm:^0.4.1": + version: 0.4.1 + resolution: "json-schema-traverse@npm:0.4.1" + checksum: 10c0/108fa90d4cc6f08243aedc6da16c408daf81793bf903e9fd5ab21983cda433d5d2da49e40711da016289465ec2e62e0324dcdfbc06275a607fe3233fde4942ce + languageName: node + linkType: hard + +"json-stable-stringify-without-jsonify@npm:^1.0.1": + version: 1.0.1 + resolution: "json-stable-stringify-without-jsonify@npm:1.0.1" + checksum: 10c0/cb168b61fd4de83e58d09aaa6425ef71001bae30d260e2c57e7d09a5fd82223e2f22a042dedaab8db23b7d9ae46854b08bb1f91675a8be11c5cffebef5fb66a5 + languageName: node + linkType: hard + +"keyv@npm:^4.5.4": + version: 4.5.4 + resolution: "keyv@npm:4.5.4" + dependencies: + json-buffer: "npm:3.0.1" + checksum: 10c0/aa52f3c5e18e16bb6324876bb8b59dd02acf782a4b789c7b2ae21107fab95fab3890ed448d4f8dba80ce05391eeac4bfabb4f02a20221342982f806fa2cf271e + languageName: node + linkType: hard + +"kuler@npm:^2.0.0": + version: 2.0.0 + resolution: "kuler@npm:2.0.0" + checksum: 10c0/0a4e99d92ca373f8f74d1dc37931909c4d0d82aebc94cf2ba265771160fc12c8df34eaaac80805efbda367e2795cb1f1dd4c3d404b6b1cf38aec94035b503d2d + languageName: node + linkType: hard + +"langsmith@npm:^0.3.33, langsmith@npm:^0.3.46": + version: 0.3.49 + resolution: "langsmith@npm:0.3.49" + dependencies: + "@types/uuid": "npm:^10.0.0" + chalk: "npm:^4.1.2" + console-table-printer: "npm:^2.12.1" + p-queue: "npm:^6.6.2" + p-retry: "npm:4" + semver: "npm:^7.6.3" + uuid: "npm:^10.0.0" + peerDependencies: + "@opentelemetry/api": "*" + "@opentelemetry/exporter-trace-otlp-proto": "*" + "@opentelemetry/sdk-trace-base": "*" + openai: "*" + peerDependenciesMeta: + "@opentelemetry/api": + optional: true + "@opentelemetry/exporter-trace-otlp-proto": + optional: true + "@opentelemetry/sdk-trace-base": + optional: true + openai: + optional: true + checksum: 10c0/7152988844b04403c6e5b1e7b321eba12d62f273c9fcd554f04cfc5f905df85c126cb81238315210a282e53b6cafe11af53878ab4a82c1cef3ad196974419782 + languageName: node + linkType: hard + +"levn@npm:^0.4.1": + version: 0.4.1 + resolution: "levn@npm:0.4.1" + dependencies: + prelude-ls: "npm:^1.2.1" + type-check: "npm:~0.4.0" + checksum: 10c0/effb03cad7c89dfa5bd4f6989364bfc79994c2042ec5966cb9b95990e2edee5cd8969ddf42616a0373ac49fac1403437deaf6e9050fbbaa3546093a59b9ac94e + languageName: node + linkType: hard + +"lightningcss-darwin-arm64@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-darwin-arm64@npm:1.30.1" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"lightningcss-darwin-x64@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-darwin-x64@npm:1.30.1" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"lightningcss-freebsd-x64@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-freebsd-x64@npm:1.30.1" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"lightningcss-linux-arm-gnueabihf@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-linux-arm-gnueabihf@npm:1.30.1" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"lightningcss-linux-arm64-gnu@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-linux-arm64-gnu@npm:1.30.1" + conditions: os=linux & cpu=arm64 & libc=glibc + languageName: node + linkType: hard + +"lightningcss-linux-arm64-musl@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-linux-arm64-musl@npm:1.30.1" + conditions: os=linux & cpu=arm64 & libc=musl + languageName: node + linkType: hard + +"lightningcss-linux-x64-gnu@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-linux-x64-gnu@npm:1.30.1" + conditions: os=linux & cpu=x64 & libc=glibc + languageName: node + linkType: hard + +"lightningcss-linux-x64-musl@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-linux-x64-musl@npm:1.30.1" + conditions: os=linux & cpu=x64 & libc=musl + languageName: node + linkType: hard + +"lightningcss-win32-arm64-msvc@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-win32-arm64-msvc@npm:1.30.1" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"lightningcss-win32-x64-msvc@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss-win32-x64-msvc@npm:1.30.1" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + +"lightningcss@npm:1.30.1": + version: 1.30.1 + resolution: "lightningcss@npm:1.30.1" + dependencies: + detect-libc: "npm:^2.0.3" + lightningcss-darwin-arm64: "npm:1.30.1" + lightningcss-darwin-x64: "npm:1.30.1" + lightningcss-freebsd-x64: "npm:1.30.1" + lightningcss-linux-arm-gnueabihf: "npm:1.30.1" + lightningcss-linux-arm64-gnu: "npm:1.30.1" + lightningcss-linux-arm64-musl: "npm:1.30.1" + lightningcss-linux-x64-gnu: "npm:1.30.1" + lightningcss-linux-x64-musl: "npm:1.30.1" + lightningcss-win32-arm64-msvc: "npm:1.30.1" + lightningcss-win32-x64-msvc: "npm:1.30.1" + dependenciesMeta: + lightningcss-darwin-arm64: + optional: true + lightningcss-darwin-x64: + optional: true + lightningcss-freebsd-x64: + optional: true + lightningcss-linux-arm-gnueabihf: + optional: true + lightningcss-linux-arm64-gnu: + optional: true + lightningcss-linux-arm64-musl: + optional: true + lightningcss-linux-x64-gnu: + optional: true + lightningcss-linux-x64-musl: + optional: true + lightningcss-win32-arm64-msvc: + optional: true + lightningcss-win32-x64-msvc: + optional: true + checksum: 10c0/1e1ad908f3c68bf39d964a6735435a8dd5474fb2765076732d64a7b6aa2af1f084da65a9462443a9adfebf7dcfb02fb532fce1d78697f2a9de29c8f40f09aee3 + languageName: node + linkType: hard + +"loader-utils@npm:^3.2.0": + version: 3.3.1 + resolution: "loader-utils@npm:3.3.1" + checksum: 10c0/f2af4eb185ac5bf7e56e1337b666f90744e9f443861ac521b48f093fb9e8347f191c8960b4388a3365147d218913bc23421234e7788db69f385bacfefa0b4758 + languageName: node + linkType: hard + +"locate-path@npm:^6.0.0": + version: 6.0.0 + resolution: "locate-path@npm:6.0.0" + dependencies: + p-locate: "npm:^5.0.0" + checksum: 10c0/d3972ab70dfe58ce620e64265f90162d247e87159b6126b01314dd67be43d50e96a50b517bce2d9452a79409c7614054c277b5232377de50416564a77ac7aad3 + languageName: node + linkType: hard + +"lodash.camelcase@npm:^4.3.0": + version: 4.3.0 + resolution: "lodash.camelcase@npm:4.3.0" + checksum: 10c0/fcba15d21a458076dd309fce6b1b4bf611d84a0ec252cb92447c948c533ac250b95d2e00955801ebc367e5af5ed288b996d75d37d2035260a937008e14eaf432 + languageName: node + linkType: hard + +"lodash.merge@npm:^4.6.2": + version: 4.6.2 + resolution: "lodash.merge@npm:4.6.2" + checksum: 10c0/402fa16a1edd7538de5b5903a90228aa48eb5533986ba7fa26606a49db2572bf414ff73a2c9f5d5fd36b31c46a5d5c7e1527749c07cbcf965ccff5fbdf32c506 + languageName: node + linkType: hard + +"logform@npm:^2.2.0, logform@npm:^2.7.0": + version: 2.7.0 + resolution: "logform@npm:2.7.0" + dependencies: + "@colors/colors": "npm:1.6.0" + "@types/triple-beam": "npm:^1.3.2" + fecha: "npm:^4.2.0" + ms: "npm:^2.1.1" + safe-stable-stringify: "npm:^2.3.1" + triple-beam: "npm:^1.3.0" + checksum: 10c0/4789b4b37413c731d1835734cb799240d31b865afde6b7b3e06051d6a4127bfda9e88c99cfbf296d084a315ccbed2647796e6a56b66e725bcb268c586f57558f + languageName: node + linkType: hard + +"lru-cache@npm:^10.0.1, lru-cache@npm:^10.2.0": + version: 10.4.3 + resolution: "lru-cache@npm:10.4.3" + checksum: 10c0/ebd04fbca961e6c1d6c0af3799adcc966a1babe798f685bb84e6599266599cd95d94630b10262f5424539bc4640107e8a33aa28585374abf561d30d16f4b39fb + languageName: node + linkType: hard + +"magic-string@npm:^0.30.17": + version: 0.30.17 + resolution: "magic-string@npm:0.30.17" + dependencies: + "@jridgewell/sourcemap-codec": "npm:^1.5.0" + checksum: 10c0/16826e415d04b88378f200fe022b53e638e3838b9e496edda6c0e086d7753a44a6ed187adc72d19f3623810589bf139af1a315541cd6a26ae0771a0193eaf7b8 + languageName: node + linkType: hard + +"make-fetch-happen@npm:^14.0.3": + version: 14.0.3 + resolution: "make-fetch-happen@npm:14.0.3" + dependencies: + "@npmcli/agent": "npm:^3.0.0" + cacache: "npm:^19.0.1" + http-cache-semantics: "npm:^4.1.1" + minipass: "npm:^7.0.2" + minipass-fetch: "npm:^4.0.0" + minipass-flush: "npm:^1.0.5" + minipass-pipeline: "npm:^1.2.4" + negotiator: "npm:^1.0.0" + proc-log: "npm:^5.0.0" + promise-retry: "npm:^2.0.1" + ssri: "npm:^12.0.0" + checksum: 10c0/c40efb5e5296e7feb8e37155bde8eb70bc57d731b1f7d90e35a092fde403d7697c56fb49334d92d330d6f1ca29a98142036d6480a12681133a0a1453164cb2f0 + languageName: node + linkType: hard + +"merge2@npm:^1.3.0": + version: 1.4.1 + resolution: "merge2@npm:1.4.1" + checksum: 10c0/254a8a4605b58f450308fc474c82ac9a094848081bf4c06778200207820e5193726dc563a0d2c16468810516a5c97d9d3ea0ca6585d23c58ccfff2403e8dbbeb + languageName: node + linkType: hard + +"micromatch@npm:^4.0.8": + version: 4.0.8 + resolution: "micromatch@npm:4.0.8" + dependencies: + braces: "npm:^3.0.3" + picomatch: "npm:^2.3.1" + checksum: 10c0/166fa6eb926b9553f32ef81f5f531d27b4ce7da60e5baf8c021d043b27a388fb95e46a8038d5045877881e673f8134122b59624d5cecbd16eb50a42e7a6b5ca8 + languageName: node + linkType: hard + +"minimatch@npm:^3.1.2": + version: 3.1.2 + resolution: "minimatch@npm:3.1.2" + dependencies: + brace-expansion: "npm:^1.1.7" + checksum: 10c0/0262810a8fc2e72cca45d6fd86bd349eee435eb95ac6aa45c9ea2180e7ee875ef44c32b55b5973ceabe95ea12682f6e3725cbb63d7a2d1da3ae1163c8b210311 + languageName: node + linkType: hard + +"minimatch@npm:^9.0.4": + version: 9.0.5 + resolution: "minimatch@npm:9.0.5" + dependencies: + brace-expansion: "npm:^2.0.1" + checksum: 10c0/de96cf5e35bdf0eab3e2c853522f98ffbe9a36c37797778d2665231ec1f20a9447a7e567cb640901f89e4daaa95ae5d70c65a9e8aa2bb0019b6facbc3c0575ed + languageName: node + linkType: hard + +"minipass-collect@npm:^2.0.1": + version: 2.0.1 + resolution: "minipass-collect@npm:2.0.1" + dependencies: + minipass: "npm:^7.0.3" + checksum: 10c0/5167e73f62bb74cc5019594709c77e6a742051a647fe9499abf03c71dca75515b7959d67a764bdc4f8b361cf897fbf25e2d9869ee039203ed45240f48b9aa06e + languageName: node + linkType: hard + +"minipass-fetch@npm:^4.0.0": + version: 4.0.1 + resolution: "minipass-fetch@npm:4.0.1" + dependencies: + encoding: "npm:^0.1.13" + minipass: "npm:^7.0.3" + minipass-sized: "npm:^1.0.3" + minizlib: "npm:^3.0.1" + dependenciesMeta: + encoding: + optional: true + checksum: 10c0/a3147b2efe8e078c9bf9d024a0059339c5a09c5b1dded6900a219c218cc8b1b78510b62dae556b507304af226b18c3f1aeb1d48660283602d5b6586c399eed5c + languageName: node + linkType: hard + +"minipass-flush@npm:^1.0.5": + version: 1.0.5 + resolution: "minipass-flush@npm:1.0.5" + dependencies: + minipass: "npm:^3.0.0" + checksum: 10c0/2a51b63feb799d2bb34669205eee7c0eaf9dce01883261a5b77410c9408aa447e478efd191b4de6fc1101e796ff5892f8443ef20d9544385819093dbb32d36bd + languageName: node + linkType: hard + +"minipass-pipeline@npm:^1.2.4": + version: 1.2.4 + resolution: "minipass-pipeline@npm:1.2.4" + dependencies: + minipass: "npm:^3.0.0" + checksum: 10c0/cbda57cea20b140b797505dc2cac71581a70b3247b84480c1fed5ca5ba46c25ecc25f68bfc9e6dcb1a6e9017dab5c7ada5eab73ad4f0a49d84e35093e0c643f2 + languageName: node + linkType: hard + +"minipass-sized@npm:^1.0.3": + version: 1.0.3 + resolution: "minipass-sized@npm:1.0.3" + dependencies: + minipass: "npm:^3.0.0" + checksum: 10c0/298f124753efdc745cfe0f2bdfdd81ba25b9f4e753ca4a2066eb17c821f25d48acea607dfc997633ee5bf7b6dfffb4eee4f2051eb168663f0b99fad2fa4829cb + languageName: node + linkType: hard + +"minipass@npm:^3.0.0": + version: 3.3.6 + resolution: "minipass@npm:3.3.6" + dependencies: + yallist: "npm:^4.0.0" + checksum: 10c0/a114746943afa1dbbca8249e706d1d38b85ed1298b530f5808ce51f8e9e941962e2a5ad2e00eae7dd21d8a4aae6586a66d4216d1a259385e9d0358f0c1eba16c + languageName: node + linkType: hard + +"minipass@npm:^5.0.0 || ^6.0.2 || ^7.0.0, minipass@npm:^7.0.2, minipass@npm:^7.0.3, minipass@npm:^7.0.4, minipass@npm:^7.1.2": + version: 7.1.2 + resolution: "minipass@npm:7.1.2" + checksum: 10c0/b0fd20bb9fb56e5fa9a8bfac539e8915ae07430a619e4b86ff71f5fc757ef3924b23b2c4230393af1eda647ed3d75739e4e0acb250a6b1eb277cf7f8fe449557 + languageName: node + linkType: hard + +"minizlib@npm:^3.0.1": + version: 3.0.2 + resolution: "minizlib@npm:3.0.2" + dependencies: + minipass: "npm:^7.1.2" + checksum: 10c0/9f3bd35e41d40d02469cb30470c55ccc21cae0db40e08d1d0b1dff01cc8cc89a6f78e9c5d2b7c844e485ec0a8abc2238111213fdc5b2038e6d1012eacf316f78 + languageName: node + linkType: hard + +"mkdirp@npm:^3.0.1": + version: 3.0.1 + resolution: "mkdirp@npm:3.0.1" + bin: + mkdirp: dist/cjs/src/bin.js + checksum: 10c0/9f2b975e9246351f5e3a40dcfac99fcd0baa31fbfab615fe059fb11e51f10e4803c63de1f384c54d656e4db31d000e4767e9ef076a22e12a641357602e31d57d + languageName: node + linkType: hard + +"ms@npm:^2.1.1, ms@npm:^2.1.3": + version: 2.1.3 + resolution: "ms@npm:2.1.3" + checksum: 10c0/d924b57e7312b3b63ad21fc5b3dc0af5e78d61a1fc7cfb5457edaf26326bf62be5307cc87ffb6862ef1c2b33b0233cdb5d4f01c4c958cc0d660948b65a287a48 + languageName: node + linkType: hard + +"mustache@npm:^4.2.0": + version: 4.2.0 + resolution: "mustache@npm:4.2.0" + bin: + mustache: bin/mustache + checksum: 10c0/1f8197e8a19e63645a786581d58c41df7853da26702dbc005193e2437c98ca49b255345c173d50c08fe4b4dbb363e53cb655ecc570791f8deb09887248dd34a2 + languageName: node + linkType: hard + +"nanoid@npm:^3.3.11": + version: 3.3.11 + resolution: "nanoid@npm:3.3.11" + bin: + nanoid: bin/nanoid.cjs + checksum: 10c0/40e7f70b3d15f725ca072dfc4f74e81fcf1fbb02e491cf58ac0c79093adc9b0a73b152bcde57df4b79cd097e13023d7504acb38404a4da7bc1cd8e887b82fe0b + languageName: node + linkType: hard + +"natural-compare@npm:^1.4.0": + version: 1.4.0 + resolution: "natural-compare@npm:1.4.0" + checksum: 10c0/f5f9a7974bfb28a91afafa254b197f0f22c684d4a1731763dda960d2c8e375b36c7d690e0d9dc8fba774c537af14a7e979129bca23d88d052fbeb9466955e447 + languageName: node + linkType: hard + +"negotiator@npm:^1.0.0": + version: 1.0.0 + resolution: "negotiator@npm:1.0.0" + checksum: 10c0/4c559dd52669ea48e1914f9d634227c561221dd54734070791f999c52ed0ff36e437b2e07d5c1f6e32909fc625fe46491c16e4a8f0572567d4dd15c3a4fda04b + languageName: node + linkType: hard + +"node-gyp@npm:latest": + version: 11.2.0 + resolution: "node-gyp@npm:11.2.0" + dependencies: + env-paths: "npm:^2.2.0" + exponential-backoff: "npm:^3.1.1" + graceful-fs: "npm:^4.2.6" + make-fetch-happen: "npm:^14.0.3" + nopt: "npm:^8.0.0" + proc-log: "npm:^5.0.0" + semver: "npm:^7.3.5" + tar: "npm:^7.4.3" + tinyglobby: "npm:^0.2.12" + which: "npm:^5.0.0" + bin: + node-gyp: bin/node-gyp.js + checksum: 10c0/bd8d8c76b06be761239b0c8680f655f6a6e90b48e44d43415b11c16f7e8c15be346fba0cbf71588c7cdfb52c419d928a7d3db353afc1d952d19756237d8f10b9 + languageName: node + linkType: hard + +"node-releases@npm:^2.0.19": + version: 2.0.19 + resolution: "node-releases@npm:2.0.19" + checksum: 10c0/52a0dbd25ccf545892670d1551690fe0facb6a471e15f2cfa1b20142a5b255b3aa254af5f59d6ecb69c2bec7390bc643c43aa63b13bf5e64b6075952e716b1aa + languageName: node + linkType: hard + +"nopt@npm:^8.0.0": + version: 8.1.0 + resolution: "nopt@npm:8.1.0" + dependencies: + abbrev: "npm:^3.0.0" + bin: + nopt: bin/nopt.js + checksum: 10c0/62e9ea70c7a3eb91d162d2c706b6606c041e4e7b547cbbb48f8b3695af457dd6479904d7ace600856bf923dd8d1ed0696f06195c8c20f02ac87c1da0e1d315ef + languageName: node + linkType: hard + +"normalize-range@npm:^0.1.2": + version: 0.1.2 + resolution: "normalize-range@npm:0.1.2" + checksum: 10c0/bf39b73a63e0a42ad1a48c2bd1bda5a07ede64a7e2567307a407674e595bcff0fa0d57e8e5f1e7fa5e91000797c7615e13613227aaaa4d6d6e87f5bd5cc95de6 + languageName: node + linkType: hard + +"one-time@npm:^1.0.0": + version: 1.0.0 + resolution: "one-time@npm:1.0.0" + dependencies: + fn.name: "npm:1.x.x" + checksum: 10c0/6e4887b331edbb954f4e915831cbec0a7b9956c36f4feb5f6de98c448ac02ff881fd8d9b55a6b1b55030af184c6b648f340a76eb211812f4ad8c9b4b8692fdaa + languageName: node + linkType: hard + +"open@npm:^10.1.0": + version: 10.2.0 + resolution: "open@npm:10.2.0" + dependencies: + default-browser: "npm:^5.2.1" + define-lazy-prop: "npm:^3.0.0" + is-inside-container: "npm:^1.0.0" + wsl-utils: "npm:^0.1.0" + checksum: 10c0/5a36d0c1fd2f74ce553beb427ca8b8494b623fc22c6132d0c1688f246a375e24584ea0b44c67133d9ab774fa69be8e12fbe1ff12504b1142bd960fb09671948f + languageName: node + linkType: hard + +"openai@npm:^5.3.0": + version: 5.10.2 + resolution: "openai@npm:5.10.2" + peerDependencies: + ws: ^8.18.0 + zod: ^3.23.8 + peerDependenciesMeta: + ws: + optional: true + zod: + optional: true + bin: + openai: bin/cli + checksum: 10c0/c08896a5d20722f3bd1a72522768ffc7c053a4df94f7b7f064c9946110070187264e6e3bf01c3abeb861e5602c65329d6aec485989338bae8ce8d3d81160fca9 + languageName: node + linkType: hard + +"optionator@npm:^0.9.3": + version: 0.9.4 + resolution: "optionator@npm:0.9.4" + dependencies: + deep-is: "npm:^0.1.3" + fast-levenshtein: "npm:^2.0.6" + levn: "npm:^0.4.1" + prelude-ls: "npm:^1.2.1" + type-check: "npm:^0.4.0" + word-wrap: "npm:^1.2.5" + checksum: 10c0/4afb687a059ee65b61df74dfe87d8d6815cd6883cb8b3d5883a910df72d0f5d029821f37025e4bccf4048873dbdb09acc6d303d27b8f76b1a80dd5a7d5334675 + languageName: node + linkType: hard + +"p-finally@npm:^1.0.0": + version: 1.0.0 + resolution: "p-finally@npm:1.0.0" + checksum: 10c0/6b8552339a71fe7bd424d01d8451eea92d379a711fc62f6b2fe64cad8a472c7259a236c9a22b4733abca0b5666ad503cb497792a0478c5af31ded793d00937e7 + languageName: node + linkType: hard + +"p-limit@npm:^3.0.2": + version: 3.1.0 + resolution: "p-limit@npm:3.1.0" + dependencies: + yocto-queue: "npm:^0.1.0" + checksum: 10c0/9db675949dbdc9c3763c89e748d0ef8bdad0afbb24d49ceaf4c46c02c77d30db4e0652ed36d0a0a7a95154335fab810d95c86153105bb73b3a90448e2bb14e1a + languageName: node + linkType: hard + +"p-locate@npm:^5.0.0": + version: 5.0.0 + resolution: "p-locate@npm:5.0.0" + dependencies: + p-limit: "npm:^3.0.2" + checksum: 10c0/2290d627ab7903b8b70d11d384fee714b797f6040d9278932754a6860845c4d3190603a0772a663c8cb5a7b21d1b16acb3a6487ebcafa9773094edc3dfe6009a + languageName: node + linkType: hard + +"p-map@npm:^7.0.2": + version: 7.0.3 + resolution: "p-map@npm:7.0.3" + checksum: 10c0/46091610da2b38ce47bcd1d8b4835a6fa4e832848a6682cf1652bc93915770f4617afc844c10a77d1b3e56d2472bb2d5622353fa3ead01a7f42b04fc8e744a5c + languageName: node + linkType: hard + +"p-queue@npm:^6.6.2": + version: 6.6.2 + resolution: "p-queue@npm:6.6.2" + dependencies: + eventemitter3: "npm:^4.0.4" + p-timeout: "npm:^3.2.0" + checksum: 10c0/5739ecf5806bbeadf8e463793d5e3004d08bb3f6177bd1a44a005da8fd81bb90f80e4633e1fb6f1dfd35ee663a5c0229abe26aebb36f547ad5a858347c7b0d3e + languageName: node + linkType: hard + +"p-retry@npm:4": + version: 4.6.2 + resolution: "p-retry@npm:4.6.2" + dependencies: + "@types/retry": "npm:0.12.0" + retry: "npm:^0.13.1" + checksum: 10c0/d58512f120f1590cfedb4c2e0c42cb3fa66f3cea8a4646632fcb834c56055bb7a6f138aa57b20cc236fb207c9d694e362e0b5c2b14d9b062f67e8925580c73b0 + languageName: node + linkType: hard + +"p-timeout@npm:^3.2.0": + version: 3.2.0 + resolution: "p-timeout@npm:3.2.0" + dependencies: + p-finally: "npm:^1.0.0" + checksum: 10c0/524b393711a6ba8e1d48137c5924749f29c93d70b671e6db761afa784726572ca06149c715632da8f70c090073afb2af1c05730303f915604fd38ee207b70a61 + languageName: node + linkType: hard + +"package-json-from-dist@npm:^1.0.0": + version: 1.0.1 + resolution: "package-json-from-dist@npm:1.0.1" + checksum: 10c0/62ba2785eb655fec084a257af34dbe24292ab74516d6aecef97ef72d4897310bc6898f6c85b5cd22770eaa1ce60d55a0230e150fb6a966e3ecd6c511e23d164b + languageName: node + linkType: hard + +"parent-module@npm:^1.0.0": + version: 1.0.1 + resolution: "parent-module@npm:1.0.1" + dependencies: + callsites: "npm:^3.0.0" + checksum: 10c0/c63d6e80000d4babd11978e0d3fee386ca7752a02b035fd2435960ffaa7219dc42146f07069fb65e6e8bf1caef89daf9af7535a39bddf354d78bf50d8294f556 + languageName: node + linkType: hard + +"path-exists@npm:^4.0.0": + version: 4.0.0 + resolution: "path-exists@npm:4.0.0" + checksum: 10c0/8c0bd3f5238188197dc78dced15207a4716c51cc4e3624c44fc97acf69558f5ebb9a2afff486fe1b4ee148e0c133e96c5e11a9aa5c48a3006e3467da070e5e1b + languageName: node + linkType: hard + +"path-key@npm:^3.1.0": + version: 3.1.1 + resolution: "path-key@npm:3.1.1" + checksum: 10c0/748c43efd5a569c039d7a00a03b58eecd1d75f3999f5a28303d75f521288df4823bc057d8784eb72358b2895a05f29a070bc9f1f17d28226cc4e62494cc58c4c + languageName: node + linkType: hard + +"path-scurry@npm:^1.11.1": + version: 1.11.1 + resolution: "path-scurry@npm:1.11.1" + dependencies: + lru-cache: "npm:^10.2.0" + minipass: "npm:^5.0.0 || ^6.0.2 || ^7.0.0" + checksum: 10c0/32a13711a2a505616ae1cc1b5076801e453e7aae6ac40ab55b388bb91b9d0547a52f5aaceff710ea400205f18691120d4431e520afbe4266b836fadede15872d + languageName: node + linkType: hard + +"picocolors@npm:^1.1.1": + version: 1.1.1 + resolution: "picocolors@npm:1.1.1" + checksum: 10c0/e2e3e8170ab9d7c7421969adaa7e1b31434f789afb9b3f115f6b96d91945041ac3ceb02e9ec6fe6510ff036bcc0bf91e69a1772edc0b707e12b19c0f2d6bcf58 + languageName: node + linkType: hard + +"picomatch@npm:^2.3.1": + version: 2.3.1 + resolution: "picomatch@npm:2.3.1" + checksum: 10c0/26c02b8d06f03206fc2ab8d16f19960f2ff9e81a658f831ecb656d8f17d9edc799e8364b1f4a7873e89d9702dff96204be0fa26fe4181f6843f040f819dac4be + languageName: node + linkType: hard + +"picomatch@npm:^4.0.2": + version: 4.0.3 + resolution: "picomatch@npm:4.0.3" + checksum: 10c0/9582c951e95eebee5434f59e426cddd228a7b97a0161a375aed4be244bd3fe8e3a31b846808ea14ef2c8a2527a6eeab7b3946a67d5979e81694654f939473ae2 + languageName: node + linkType: hard + +"postcss-modules-extract-imports@npm:^3.1.0": + version: 3.1.0 + resolution: "postcss-modules-extract-imports@npm:3.1.0" + peerDependencies: + postcss: ^8.1.0 + checksum: 10c0/402084bcab376083c4b1b5111b48ec92974ef86066f366f0b2d5b2ac2b647d561066705ade4db89875a13cb175b33dd6af40d16d32b2ea5eaf8bac63bd2bf219 + languageName: node + linkType: hard + +"postcss-modules-local-by-default@npm:^4.0.5": + version: 4.2.0 + resolution: "postcss-modules-local-by-default@npm:4.2.0" + dependencies: + icss-utils: "npm:^5.0.0" + postcss-selector-parser: "npm:^7.0.0" + postcss-value-parser: "npm:^4.1.0" + peerDependencies: + postcss: ^8.1.0 + checksum: 10c0/b0b83feb2a4b61f5383979d37f23116c99bc146eba1741ca3cf1acca0e4d0dbf293ac1810a6ab4eccbe1ee76440dd0a9eb2db5b3bba4f99fc1b3ded16baa6358 + languageName: node + linkType: hard + +"postcss-modules-scope@npm:^3.2.0": + version: 3.2.1 + resolution: "postcss-modules-scope@npm:3.2.1" + dependencies: + postcss-selector-parser: "npm:^7.0.0" + peerDependencies: + postcss: ^8.1.0 + checksum: 10c0/bd2d81f79e3da0ef6365b8e2c78cc91469d05b58046b4601592cdeef6c4050ed8fe1478ae000a1608042fc7e692cb51fecbd2d9bce3f4eace4d32e883ffca10b + languageName: node + linkType: hard + +"postcss-modules-values@npm:^4.0.0": + version: 4.0.0 + resolution: "postcss-modules-values@npm:4.0.0" + dependencies: + icss-utils: "npm:^5.0.0" + peerDependencies: + postcss: ^8.1.0 + checksum: 10c0/dd18d7631b5619fb9921b198c86847a2a075f32e0c162e0428d2647685e318c487a2566cc8cc669fc2077ef38115cde7a068e321f46fb38be3ad49646b639dbc + languageName: node + linkType: hard + +"postcss-modules@npm:^6.0.1": + version: 6.0.1 + resolution: "postcss-modules@npm:6.0.1" + dependencies: + generic-names: "npm:^4.0.0" + icss-utils: "npm:^5.1.0" + lodash.camelcase: "npm:^4.3.0" + postcss-modules-extract-imports: "npm:^3.1.0" + postcss-modules-local-by-default: "npm:^4.0.5" + postcss-modules-scope: "npm:^3.2.0" + postcss-modules-values: "npm:^4.0.0" + string-hash: "npm:^1.1.3" + peerDependencies: + postcss: ^8.0.0 + checksum: 10c0/b82230693cb257b69db486df8835626d96632481ec6a8777b51ae7a530a56fa0ed399cbc8c2c777525f31fefab5a2d12ea7331a748fdfddde9f16cf3fff3bc58 + languageName: node + linkType: hard + +"postcss-selector-parser@npm:^7.0.0": + version: 7.1.0 + resolution: "postcss-selector-parser@npm:7.1.0" + dependencies: + cssesc: "npm:^3.0.0" + util-deprecate: "npm:^1.0.2" + checksum: 10c0/0fef257cfd1c0fe93c18a3f8a6e739b4438b527054fd77e9a62730a89b2d0ded1b59314a7e4aaa55bc256204f40830fecd2eb50f20f8cb7ab3a10b52aa06c8aa + languageName: node + linkType: hard + +"postcss-value-parser@npm:^4.1.0, postcss-value-parser@npm:^4.2.0": + version: 4.2.0 + resolution: "postcss-value-parser@npm:4.2.0" + checksum: 10c0/f4142a4f56565f77c1831168e04e3effd9ffcc5aebaf0f538eee4b2d465adfd4b85a44257bb48418202a63806a7da7fe9f56c330aebb3cac898e46b4cbf49161 + languageName: node + linkType: hard + +"postcss@npm:^8.4.41, postcss@npm:^8.5.1": + version: 8.5.6 + resolution: "postcss@npm:8.5.6" + dependencies: + nanoid: "npm:^3.3.11" + picocolors: "npm:^1.1.1" + source-map-js: "npm:^1.2.1" + checksum: 10c0/5127cc7c91ed7a133a1b7318012d8bfa112da9ef092dddf369ae699a1f10ebbd89b1b9f25f3228795b84585c72aabd5ced5fc11f2ba467eedf7b081a66fad024 + languageName: node + linkType: hard + +"prelude-ls@npm:^1.2.1": + version: 1.2.1 + resolution: "prelude-ls@npm:1.2.1" + checksum: 10c0/b00d617431e7886c520a6f498a2e14c75ec58f6d93ba48c3b639cf241b54232d90daa05d83a9e9b9fef6baa63cb7e1e4602c2372fea5bc169668401eb127d0cd + languageName: node + linkType: hard + +"proc-log@npm:^5.0.0": + version: 5.0.0 + resolution: "proc-log@npm:5.0.0" + checksum: 10c0/bbe5edb944b0ad63387a1d5b1911ae93e05ce8d0f60de1035b218cdcceedfe39dbd2c697853355b70f1a090f8f58fe90da487c85216bf9671f9499d1a897e9e3 + languageName: node + linkType: hard + +"promise-retry@npm:^2.0.1": + version: 2.0.1 + resolution: "promise-retry@npm:2.0.1" + dependencies: + err-code: "npm:^2.0.2" + retry: "npm:^0.12.0" + checksum: 10c0/9c7045a1a2928094b5b9b15336dcd2a7b1c052f674550df63cc3f36cd44028e5080448175b6f6ca32b642de81150f5e7b1a98b728f15cb069f2dd60ac2616b96 + languageName: node + linkType: hard + +"punycode@npm:^2.1.0": + version: 2.3.1 + resolution: "punycode@npm:2.3.1" + checksum: 10c0/14f76a8206bc3464f794fb2e3d3cc665ae416c01893ad7a02b23766eb07159144ee612ad67af5e84fa4479ccfe67678c4feb126b0485651b302babf66f04f9e9 + languageName: node + linkType: hard + +"queue-microtask@npm:^1.2.2": + version: 1.2.3 + resolution: "queue-microtask@npm:1.2.3" + checksum: 10c0/900a93d3cdae3acd7d16f642c29a642aea32c2026446151f0778c62ac089d4b8e6c986811076e1ae180a694cedf077d453a11b58ff0a865629a4f82ab558e102 + languageName: node + linkType: hard + +"readable-stream@npm:^3.4.0, readable-stream@npm:^3.6.2": + version: 3.6.2 + resolution: "readable-stream@npm:3.6.2" + dependencies: + inherits: "npm:^2.0.3" + string_decoder: "npm:^1.1.1" + util-deprecate: "npm:^1.0.1" + checksum: 10c0/e37be5c79c376fdd088a45fa31ea2e423e5d48854be7a22a58869b4e84d25047b193f6acb54f1012331e1bcd667ffb569c01b99d36b0bd59658fb33f513511b7 + languageName: node + linkType: hard + +"resolve-from@npm:^4.0.0": + version: 4.0.0 + resolution: "resolve-from@npm:4.0.0" + checksum: 10c0/8408eec31a3112ef96e3746c37be7d64020cda07c03a920f5024e77290a218ea758b26ca9529fd7b1ad283947f34b2291c1c0f6aa0ed34acfdda9c6014c8d190 + languageName: node + linkType: hard + +"resolve-pkg-maps@npm:^1.0.0": + version: 1.0.0 + resolution: "resolve-pkg-maps@npm:1.0.0" + checksum: 10c0/fb8f7bbe2ca281a73b7ef423a1cbc786fb244bd7a95cbe5c3fba25b27d327150beca8ba02f622baea65919a57e061eb5005204daa5f93ed590d9b77463a567ab + languageName: node + linkType: hard + +"retry@npm:^0.12.0": + version: 0.12.0 + resolution: "retry@npm:0.12.0" + checksum: 10c0/59933e8501727ba13ad73ef4a04d5280b3717fd650408460c987392efe9d7be2040778ed8ebe933c5cbd63da3dcc37919c141ef8af0a54a6e4fca5a2af177bfe + languageName: node + linkType: hard + +"retry@npm:^0.13.1": + version: 0.13.1 + resolution: "retry@npm:0.13.1" + checksum: 10c0/9ae822ee19db2163497e074ea919780b1efa00431d197c7afdb950e42bf109196774b92a49fc9821f0b8b328a98eea6017410bfc5e8a0fc19c85c6d11adb3772 + languageName: node + linkType: hard + +"reusify@npm:^1.0.4": + version: 1.1.0 + resolution: "reusify@npm:1.1.0" + checksum: 10c0/4eff0d4a5f9383566c7d7ec437b671cc51b25963bd61bf127c3f3d3f68e44a026d99b8d2f1ad344afff8d278a8fe70a8ea092650a716d22287e8bef7126bb2fa + languageName: node + linkType: hard + +"run-applescript@npm:^7.0.0": + version: 7.0.0 + resolution: "run-applescript@npm:7.0.0" + checksum: 10c0/bd821bbf154b8e6c8ecffeaf0c33cebbb78eb2987476c3f6b420d67ab4c5301faa905dec99ded76ebb3a7042b4e440189ae6d85bbbd3fc6e8d493347ecda8bfe + languageName: node + linkType: hard + +"run-parallel@npm:^1.1.9": + version: 1.2.0 + resolution: "run-parallel@npm:1.2.0" + dependencies: + queue-microtask: "npm:^1.2.2" + checksum: 10c0/200b5ab25b5b8b7113f9901bfe3afc347e19bb7475b267d55ad0eb86a62a46d77510cb0f232507c9e5d497ebda569a08a9867d0d14f57a82ad5564d991588b39 + languageName: node + linkType: hard + +"safe-buffer@npm:~5.2.0": + version: 5.2.1 + resolution: "safe-buffer@npm:5.2.1" + checksum: 10c0/6501914237c0a86e9675d4e51d89ca3c21ffd6a31642efeba25ad65720bce6921c9e7e974e5be91a786b25aa058b5303285d3c15dbabf983a919f5f630d349f3 + languageName: node + linkType: hard + +"safe-stable-stringify@npm:^2.3.1": + version: 2.5.0 + resolution: "safe-stable-stringify@npm:2.5.0" + checksum: 10c0/baea14971858cadd65df23894a40588ed791769db21bafb7fd7608397dbdce9c5aac60748abae9995e0fc37e15f2061980501e012cd48859740796bea2987f49 + languageName: node + linkType: hard + +"safer-buffer@npm:>= 2.1.2 < 3.0.0": + version: 2.1.2 + resolution: "safer-buffer@npm:2.1.2" + checksum: 10c0/7e3c8b2e88a1841c9671094bbaeebd94448111dd90a81a1f606f3f67708a6ec57763b3b47f06da09fc6054193e0e6709e77325415dc8422b04497a8070fa02d4 + languageName: node + linkType: hard + +"semver@npm:^7.3.5, semver@npm:^7.6.0, semver@npm:^7.6.3, semver@npm:^7.7.1": + version: 7.7.2 + resolution: "semver@npm:7.7.2" + bin: + semver: bin/semver.js + checksum: 10c0/aca305edfbf2383c22571cb7714f48cadc7ac95371b4b52362fb8eeffdfbc0de0669368b82b2b15978f8848f01d7114da65697e56cd8c37b0dab8c58e543f9ea + languageName: node + linkType: hard + +"shebang-command@npm:^2.0.0": + version: 2.0.0 + resolution: "shebang-command@npm:2.0.0" + dependencies: + shebang-regex: "npm:^3.0.0" + checksum: 10c0/a41692e7d89a553ef21d324a5cceb5f686d1f3c040759c50aab69688634688c5c327f26f3ecf7001ebfd78c01f3c7c0a11a7c8bfd0a8bc9f6240d4f40b224e4e + languageName: node + linkType: hard + +"shebang-regex@npm:^3.0.0": + version: 3.0.0 + resolution: "shebang-regex@npm:3.0.0" + checksum: 10c0/1dbed0726dd0e1152a92696c76c7f06084eb32a90f0528d11acd764043aacf76994b2fb30aa1291a21bd019d6699164d048286309a278855ee7bec06cf6fb690 + languageName: node + linkType: hard + +"signal-exit@npm:^4.0.1": + version: 4.1.0 + resolution: "signal-exit@npm:4.1.0" + checksum: 10c0/41602dce540e46d599edba9d9860193398d135f7ff72cab629db5171516cfae628d21e7bfccde1bbfdf11c48726bc2a6d1a8fb8701125852fbfda7cf19c6aa83 + languageName: node + linkType: hard + +"simple-swizzle@npm:^0.2.2": + version: 0.2.2 + resolution: "simple-swizzle@npm:0.2.2" + dependencies: + is-arrayish: "npm:^0.3.1" + checksum: 10c0/df5e4662a8c750bdba69af4e8263c5d96fe4cd0f9fe4bdfa3cbdeb45d2e869dff640beaaeb1ef0e99db4d8d2ec92f85508c269f50c972174851bc1ae5bd64308 + languageName: node + linkType: hard + +"simple-wcswidth@npm:^1.0.1": + version: 1.1.2 + resolution: "simple-wcswidth@npm:1.1.2" + checksum: 10c0/0db23ffef39d81a018a2354d64db1d08a44123c54263e48173992c61d808aaa8b58e5651d424e8c275589671f35e9094ac6fa2bbf2c98771b1bae9e007e611dd + languageName: node + linkType: hard + +"smart-buffer@npm:^4.2.0": + version: 4.2.0 + resolution: "smart-buffer@npm:4.2.0" + checksum: 10c0/a16775323e1404dd43fabafe7460be13a471e021637bc7889468eb45ce6a6b207261f454e4e530a19500cc962c4cc5348583520843b363f4193cee5c00e1e539 + languageName: node + linkType: hard + +"socks-proxy-agent@npm:^8.0.3": + version: 8.0.5 + resolution: "socks-proxy-agent@npm:8.0.5" + dependencies: + agent-base: "npm:^7.1.2" + debug: "npm:^4.3.4" + socks: "npm:^2.8.3" + checksum: 10c0/5d2c6cecba6821389aabf18728325730504bf9bb1d9e342e7987a5d13badd7a98838cc9a55b8ed3cb866ad37cc23e1086f09c4d72d93105ce9dfe76330e9d2a6 + languageName: node + linkType: hard + +"socks@npm:^2.8.3": + version: 2.8.6 + resolution: "socks@npm:2.8.6" + dependencies: + ip-address: "npm:^9.0.5" + smart-buffer: "npm:^4.2.0" + checksum: 10c0/15b95db4caa359c80bfa880ff3e58f3191b9ffa4313570e501a60ee7575f51e4be664a296f4ee5c2c40544da179db6140be53433ce41ec745f9d51f342557514 + languageName: node + linkType: hard + +"source-map-js@npm:^1.2.1": + version: 1.2.1 + resolution: "source-map-js@npm:1.2.1" + checksum: 10c0/7bda1fc4c197e3c6ff17de1b8b2c20e60af81b63a52cb32ec5a5d67a20a7d42651e2cb34ebe93833c5a2a084377e17455854fee3e21e7925c64a51b6a52b0faf + languageName: node + linkType: hard + +"sprintf-js@npm:^1.1.3": + version: 1.1.3 + resolution: "sprintf-js@npm:1.1.3" + checksum: 10c0/09270dc4f30d479e666aee820eacd9e464215cdff53848b443964202bf4051490538e5dd1b42e1a65cf7296916ca17640aebf63dae9812749c7542ee5f288dec + languageName: node + linkType: hard + +"ssri@npm:^12.0.0": + version: 12.0.0 + resolution: "ssri@npm:12.0.0" + dependencies: + minipass: "npm:^7.0.3" + checksum: 10c0/caddd5f544b2006e88fa6b0124d8d7b28208b83c72d7672d5ade44d794525d23b540f3396108c4eb9280dcb7c01f0bef50682f5b4b2c34291f7c5e211fd1417d + languageName: node + linkType: hard + +"stack-trace@npm:0.0.x": + version: 0.0.10 + resolution: "stack-trace@npm:0.0.10" + checksum: 10c0/9ff3dabfad4049b635a85456f927a075c9d0c210e3ea336412d18220b2a86cbb9b13ec46d6c37b70a302a4ea4d49e30e5d4944dd60ae784073f1cde778ac8f4b + languageName: node + linkType: hard + +"stacktrace-parser@npm:^0.1.10": + version: 0.1.11 + resolution: "stacktrace-parser@npm:0.1.11" + dependencies: + type-fest: "npm:^0.7.1" + checksum: 10c0/4633d9afe8cd2f6c7fb2cebdee3cc8de7fd5f6f9736645fd08c0f66872a303061ce9cc0ccf46f4216dc94a7941b56e331012398dc0024dc25e46b5eb5d4ff018 + languageName: node + linkType: hard + +"string-hash@npm:^1.1.3": + version: 1.1.3 + resolution: "string-hash@npm:1.1.3" + checksum: 10c0/179725d7706b49fbbc0a4901703a2d8abec244140879afd5a17908497e586a6b07d738f6775450aefd9f8dd729e4a0abd073fbc6fa3bd020b7a1d2369614af88 + languageName: node + linkType: hard + +"string-width-cjs@npm:string-width@^4.2.0, string-width@npm:^4.1.0": + version: 4.2.3 + resolution: "string-width@npm:4.2.3" + dependencies: + emoji-regex: "npm:^8.0.0" + is-fullwidth-code-point: "npm:^3.0.0" + strip-ansi: "npm:^6.0.1" + checksum: 10c0/1e525e92e5eae0afd7454086eed9c818ee84374bb80328fc41217ae72ff5f065ef1c9d7f72da41de40c75fa8bb3dee63d92373fd492c84260a552c636392a47b + languageName: node + linkType: hard + +"string-width@npm:^5.0.1, string-width@npm:^5.1.2": + version: 5.1.2 + resolution: "string-width@npm:5.1.2" + dependencies: + eastasianwidth: "npm:^0.2.0" + emoji-regex: "npm:^9.2.2" + strip-ansi: "npm:^7.0.1" + checksum: 10c0/ab9c4264443d35b8b923cbdd513a089a60de339216d3b0ed3be3ba57d6880e1a192b70ae17225f764d7adbf5994e9bb8df253a944736c15a0240eff553c678ca + languageName: node + linkType: hard + +"string_decoder@npm:^1.1.1": + version: 1.3.0 + resolution: "string_decoder@npm:1.3.0" + dependencies: + safe-buffer: "npm:~5.2.0" + checksum: 10c0/810614ddb030e271cd591935dcd5956b2410dd079d64ff92a1844d6b7588bf992b3e1b69b0f4d34a3e06e0bd73046ac646b5264c1987b20d0601f81ef35d731d + languageName: node + linkType: hard + +"strip-ansi-cjs@npm:strip-ansi@^6.0.1, strip-ansi@npm:^6.0.0, strip-ansi@npm:^6.0.1": + version: 6.0.1 + resolution: "strip-ansi@npm:6.0.1" + dependencies: + ansi-regex: "npm:^5.0.1" + checksum: 10c0/1ae5f212a126fe5b167707f716942490e3933085a5ff6c008ab97ab2f272c8025d3aa218b7bd6ab25729ca20cc81cddb252102f8751e13482a5199e873680952 + languageName: node + linkType: hard + +"strip-ansi@npm:^7.0.1": + version: 7.1.0 + resolution: "strip-ansi@npm:7.1.0" + dependencies: + ansi-regex: "npm:^6.0.1" + checksum: 10c0/a198c3762e8832505328cbf9e8c8381de14a4fa50a4f9b2160138158ea88c0f5549fb50cb13c651c3088f47e63a108b34622ec18c0499b6c8c3a5ddf6b305ac4 + languageName: node + linkType: hard + +"strip-json-comments@npm:^3.1.1": + version: 3.1.1 + resolution: "strip-json-comments@npm:3.1.1" + checksum: 10c0/9681a6257b925a7fa0f285851c0e613cc934a50661fa7bb41ca9cbbff89686bb4a0ee366e6ecedc4daafd01e83eee0720111ab294366fe7c185e935475ebcecd + languageName: node + linkType: hard + +"strnum@npm:^1.1.1": + version: 1.1.2 + resolution: "strnum@npm:1.1.2" + checksum: 10c0/a0fce2498fa3c64ce64a40dada41beb91cabe3caefa910e467dc0518ef2ebd7e4d10f8c2202a6104f1410254cae245066c0e94e2521fb4061a5cb41831952392 + languageName: node + linkType: hard + +"superjson@npm:^2.2.2": + version: 2.2.2 + resolution: "superjson@npm:2.2.2" + dependencies: + copy-anything: "npm:^3.0.2" + checksum: 10c0/aa49ebe6653e963020bc6a1ed416d267dfda84cfcc3cbd3beffd75b72e44eb9df7327215f3e3e77528f6e19ad8895b16a4964fdcd56d1799d14350db8c92afbc + languageName: node + linkType: hard + +"supports-color@npm:^7.1.0": + version: 7.2.0 + resolution: "supports-color@npm:7.2.0" + dependencies: + has-flag: "npm:^4.0.0" + checksum: 10c0/afb4c88521b8b136b5f5f95160c98dee7243dc79d5432db7efc27efb219385bbc7d9427398e43dd6cc730a0f87d5085ce1652af7efbe391327bc0a7d0f7fc124 + languageName: node + linkType: hard + +"tailwindcss@npm:4.1.11": + version: 4.1.11 + resolution: "tailwindcss@npm:4.1.11" + checksum: 10c0/e23eed0a0d6557b3aff8ba320b82758988ca67c351ee9b33dfc646e83a64f6eaeca6183dfc97e931f7b2fab46e925090066edd697d2ede3f396c9fdeb4af24c1 + languageName: node + linkType: hard + +"tapable@npm:^2.2.0": + version: 2.2.2 + resolution: "tapable@npm:2.2.2" + checksum: 10c0/8ad130aa705cab6486ad89e42233569a1fb1ff21af115f59cebe9f2b45e9e7995efceaa9cc5062510cdb4ec673b527924b2ab812e3579c55ad659ae92117011e + languageName: node + linkType: hard + +"tar@npm:^7.4.3": + version: 7.4.3 + resolution: "tar@npm:7.4.3" + dependencies: + "@isaacs/fs-minipass": "npm:^4.0.0" + chownr: "npm:^3.0.0" + minipass: "npm:^7.1.2" + minizlib: "npm:^3.0.1" + mkdirp: "npm:^3.0.1" + yallist: "npm:^5.0.0" + checksum: 10c0/d4679609bb2a9b48eeaf84632b6d844128d2412b95b6de07d53d8ee8baf4ca0857c9331dfa510390a0727b550fd543d4d1a10995ad86cdf078423fbb8d99831d + languageName: node + linkType: hard + +"text-hex@npm:1.0.x": + version: 1.0.0 + resolution: "text-hex@npm:1.0.0" + checksum: 10c0/57d8d320d92c79d7c03ffb8339b825bb9637c2cbccf14304309f51d8950015c44464b6fd1b6820a3d4821241c68825634f09f5a2d9d501e84f7c6fd14376860d + languageName: node + linkType: hard + +"tinyglobby@npm:^0.2.12": + version: 0.2.14 + resolution: "tinyglobby@npm:0.2.14" + dependencies: + fdir: "npm:^6.4.4" + picomatch: "npm:^4.0.2" + checksum: 10c0/f789ed6c924287a9b7d3612056ed0cda67306cd2c80c249fd280cf1504742b12583a2089b61f4abbd24605f390809017240e250241f09938054c9b363e51c0a6 + languageName: node + linkType: hard + +"to-regex-range@npm:^5.0.1": + version: 5.0.1 + resolution: "to-regex-range@npm:5.0.1" + dependencies: + is-number: "npm:^7.0.0" + checksum: 10c0/487988b0a19c654ff3e1961b87f471702e708fa8a8dd02a298ef16da7206692e8552a0250e8b3e8759270f62e9d8314616f6da274734d3b558b1fc7b7724e892 + languageName: node + linkType: hard + +"triple-beam@npm:^1.3.0": + version: 1.4.1 + resolution: "triple-beam@npm:1.4.1" + checksum: 10c0/4bf1db71e14fe3ff1c3adbe3c302f1fdb553b74d7591a37323a7badb32dc8e9c290738996cbb64f8b10dc5a3833645b5d8c26221aaaaa12e50d1251c9aba2fea + languageName: node + linkType: hard + +"ts-api-utils@npm:^2.1.0": + version: 2.1.0 + resolution: "ts-api-utils@npm:2.1.0" + peerDependencies: + typescript: ">=4.8.4" + checksum: 10c0/9806a38adea2db0f6aa217ccc6bc9c391ddba338a9fe3080676d0d50ed806d305bb90e8cef0276e793d28c8a929f400abb184ddd7ff83a416959c0f4d2ce754f + languageName: node + linkType: hard + +"tslib@npm:^2.4.0, tslib@npm:^2.8.0": + version: 2.8.1 + resolution: "tslib@npm:2.8.1" + checksum: 10c0/9c4759110a19c53f992d9aae23aac5ced636e99887b51b9e61def52611732872ff7668757d4e4c61f19691e36f4da981cd9485e869b4a7408d689f6bf1f14e62 + languageName: node + linkType: hard + +"tsx@npm:^4.19.3": + version: 4.20.3 + resolution: "tsx@npm:4.20.3" + dependencies: + esbuild: "npm:~0.25.0" + fsevents: "npm:~2.3.3" + get-tsconfig: "npm:^4.7.5" + dependenciesMeta: + fsevents: + optional: true + bin: + tsx: dist/cli.mjs + checksum: 10c0/6ff0d91ed046ec743fac7ed60a07f3c025e5b71a5aaf58f3d2a6b45e4db114c83e59ebbb078c8e079e48d3730b944a02bc0de87695088aef4ec8bbc705dc791b + languageName: node + linkType: hard + +"type-check@npm:^0.4.0, type-check@npm:~0.4.0": + version: 0.4.0 + resolution: "type-check@npm:0.4.0" + dependencies: + prelude-ls: "npm:^1.2.1" + checksum: 10c0/7b3fd0ed43891e2080bf0c5c504b418fbb3e5c7b9708d3d015037ba2e6323a28152ec163bcb65212741fa5d2022e3075ac3c76440dbd344c9035f818e8ecee58 + languageName: node + linkType: hard + +"type-fest@npm:^0.7.1": + version: 0.7.1 + resolution: "type-fest@npm:0.7.1" + checksum: 10c0/ce6b5ef806a76bf08d0daa78d65e61f24d9a0380bd1f1df36ffb61f84d14a0985c3a921923cf4b97831278cb6fa9bf1b89c751df09407e0510b14e8c081e4e0f + languageName: node + linkType: hard + +"typescript-eslint@npm:^8.38.0": + version: 8.38.0 + resolution: "typescript-eslint@npm:8.38.0" + dependencies: + "@typescript-eslint/eslint-plugin": "npm:8.38.0" + "@typescript-eslint/parser": "npm:8.38.0" + "@typescript-eslint/typescript-estree": "npm:8.38.0" + "@typescript-eslint/utils": "npm:8.38.0" + peerDependencies: + eslint: ^8.57.0 || ^9.0.0 + typescript: ">=4.8.4 <5.9.0" + checksum: 10c0/486b9862ee08f7827d808a2264ce03b58087b11c4c646c0da3533c192a67ae3fcb4e68d7a1e69d0f35a1edc274371a903a50ecfe74012d5eaa896cb9d5a81e0b + languageName: node + linkType: hard + +"typescript@npm:^5.8.3": + version: 5.8.3 + resolution: "typescript@npm:5.8.3" + bin: + tsc: bin/tsc + tsserver: bin/tsserver + checksum: 10c0/5f8bb01196e542e64d44db3d16ee0e4063ce4f3e3966df6005f2588e86d91c03e1fb131c2581baf0fb65ee79669eea6e161cd448178986587e9f6844446dbb48 + languageName: node + linkType: hard + +"typescript@patch:typescript@npm%3A^5.8.3#optional!builtin<compat/typescript>": + version: 5.8.3 + resolution: "typescript@patch:typescript@npm%3A5.8.3#optional!builtin<compat/typescript>::version=5.8.3&hash=5786d5" + bin: + tsc: bin/tsc + tsserver: bin/tsserver + checksum: 10c0/39117e346ff8ebd87ae1510b3a77d5d92dae5a89bde588c747d25da5c146603a99c8ee588c7ef80faaf123d89ed46f6dbd918d534d641083177d5fac38b8a1cb + languageName: node + linkType: hard + +"unique-filename@npm:^4.0.0": + version: 4.0.0 + resolution: "unique-filename@npm:4.0.0" + dependencies: + unique-slug: "npm:^5.0.0" + checksum: 10c0/38ae681cceb1408ea0587b6b01e29b00eee3c84baee1e41fd5c16b9ed443b80fba90c40e0ba69627e30855570a34ba8b06702d4a35035d4b5e198bf5a64c9ddc + languageName: node + linkType: hard + +"unique-slug@npm:^5.0.0": + version: 5.0.0 + resolution: "unique-slug@npm:5.0.0" + dependencies: + imurmurhash: "npm:^0.1.4" + checksum: 10c0/d324c5a44887bd7e105ce800fcf7533d43f29c48757ac410afd42975de82cc38ea2035c0483f4de82d186691bf3208ef35c644f73aa2b1b20b8e651be5afd293 + languageName: node + linkType: hard + +"update-browserslist-db@npm:^1.1.3": + version: 1.1.3 + resolution: "update-browserslist-db@npm:1.1.3" + dependencies: + escalade: "npm:^3.2.0" + picocolors: "npm:^1.1.1" + peerDependencies: + browserslist: ">= 4.21.0" + bin: + update-browserslist-db: cli.js + checksum: 10c0/682e8ecbf9de474a626f6462aa85927936cdd256fe584c6df2508b0df9f7362c44c957e9970df55dfe44d3623807d26316ea2c7d26b80bb76a16c56c37233c32 + languageName: node + linkType: hard + +"uri-js@npm:^4.2.2": + version: 4.4.1 + resolution: "uri-js@npm:4.4.1" + dependencies: + punycode: "npm:^2.1.0" + checksum: 10c0/4ef57b45aa820d7ac6496e9208559986c665e49447cb072744c13b66925a362d96dd5a46c4530a6b8e203e5db5fe849369444440cb22ecfc26c679359e5dfa3c + languageName: node + linkType: hard + +"util-deprecate@npm:^1.0.1, util-deprecate@npm:^1.0.2": + version: 1.0.2 + resolution: "util-deprecate@npm:1.0.2" + checksum: 10c0/41a5bdd214df2f6c3ecf8622745e4a366c4adced864bc3c833739791aeeeb1838119af7daed4ba36428114b5c67dcda034a79c882e97e43c03e66a4dd7389942 + languageName: node + linkType: hard + +"uuid@npm:^10.0.0": + version: 10.0.0 + resolution: "uuid@npm:10.0.0" + bin: + uuid: dist/bin/uuid + checksum: 10c0/eab18c27fe4ab9fb9709a5d5f40119b45f2ec8314f8d4cf12ce27e4c6f4ffa4a6321dc7db6c515068fa373c075b49691ba969f0010bf37f44c37ca40cd6bf7fe + languageName: node + linkType: hard + +"uuid@npm:^9.0.0": + version: 9.0.1 + resolution: "uuid@npm:9.0.1" + bin: + uuid: dist/bin/uuid + checksum: 10c0/1607dd32ac7fc22f2d8f77051e6a64845c9bce5cd3dd8aa0070c074ec73e666a1f63c7b4e0f4bf2bc8b9d59dc85a15e17807446d9d2b17c8485fbc2147b27f9b + languageName: node + linkType: hard + +"which@npm:^2.0.1": + version: 2.0.2 + resolution: "which@npm:2.0.2" + dependencies: + isexe: "npm:^2.0.0" + bin: + node-which: ./bin/node-which + checksum: 10c0/66522872a768b60c2a65a57e8ad184e5372f5b6a9ca6d5f033d4b0dc98aff63995655a7503b9c0a2598936f532120e81dd8cc155e2e92ed662a2b9377cc4374f + languageName: node + linkType: hard + +"which@npm:^5.0.0": + version: 5.0.0 + resolution: "which@npm:5.0.0" + dependencies: + isexe: "npm:^3.1.1" + bin: + node-which: bin/which.js + checksum: 10c0/e556e4cd8b7dbf5df52408c9a9dd5ac6518c8c5267c8953f5b0564073c66ed5bf9503b14d876d0e9c7844d4db9725fb0dcf45d6e911e17e26ab363dc3965ae7b + languageName: node + linkType: hard + +"winston-console-format@npm:^1.0.8": + version: 1.0.8 + resolution: "winston-console-format@npm:1.0.8" + dependencies: + colors: "npm:^1.4.0" + logform: "npm:^2.2.0" + triple-beam: "npm:^1.3.0" + checksum: 10c0/67839ac8f533617747ea3c22a14f2b3cd5bb07dcf91bb25c87f1ad3aa9850d30e5960c44e92726a9cd4c239611dc5171f6b0f4d3d9fcf58ca1ad5323a1fb81c5 + languageName: node + linkType: hard + +"winston-transport@npm:^4.9.0": + version: 4.9.0 + resolution: "winston-transport@npm:4.9.0" + dependencies: + logform: "npm:^2.7.0" + readable-stream: "npm:^3.6.2" + triple-beam: "npm:^1.3.0" + checksum: 10c0/e2990a172e754dbf27e7823772214a22dc8312f7ec9cfba831e5ef30a5d5528792e5ea8f083c7387ccfc5b2af20e3691f64738546c8869086110a26f98671095 + languageName: node + linkType: hard + +"winston@npm:^3.17.0": + version: 3.17.0 + resolution: "winston@npm:3.17.0" + dependencies: + "@colors/colors": "npm:^1.6.0" + "@dabh/diagnostics": "npm:^2.0.2" + async: "npm:^3.2.3" + is-stream: "npm:^2.0.0" + logform: "npm:^2.7.0" + one-time: "npm:^1.0.0" + readable-stream: "npm:^3.4.0" + safe-stable-stringify: "npm:^2.3.1" + stack-trace: "npm:0.0.x" + triple-beam: "npm:^1.3.0" + winston-transport: "npm:^4.9.0" + checksum: 10c0/ec8eaeac9a72b2598aedbff50b7dac82ce374a400ed92e7e705d7274426b48edcb25507d78cff318187c4fb27d642a0e2a39c57b6badc9af8e09d4a40636a5f7 + languageName: node + linkType: hard + +"word-wrap@npm:^1.2.5": + version: 1.2.5 + resolution: "word-wrap@npm:1.2.5" + checksum: 10c0/e0e4a1ca27599c92a6ca4c32260e8a92e8a44f4ef6ef93f803f8ed823f486e0889fc0b93be4db59c8d51b3064951d25e43d434e95dc8c960cc3a63d65d00ba20 + languageName: node + linkType: hard + +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": + version: 7.0.0 + resolution: "wrap-ansi@npm:7.0.0" + dependencies: + ansi-styles: "npm:^4.0.0" + string-width: "npm:^4.1.0" + strip-ansi: "npm:^6.0.0" + checksum: 10c0/d15fc12c11e4cbc4044a552129ebc75ee3f57aa9c1958373a4db0292d72282f54373b536103987a4a7594db1ef6a4f10acf92978f79b98c49306a4b58c77d4da + languageName: node + linkType: hard + +"wrap-ansi@npm:^8.1.0": + version: 8.1.0 + resolution: "wrap-ansi@npm:8.1.0" + dependencies: + ansi-styles: "npm:^6.1.0" + string-width: "npm:^5.0.1" + strip-ansi: "npm:^7.0.1" + checksum: 10c0/138ff58a41d2f877eae87e3282c0630fc2789012fc1af4d6bd626eeb9a2f9a65ca92005e6e69a75c7b85a68479fe7443c7dbe1eb8fbaa681a4491364b7c55c60 + languageName: node + linkType: hard + +"wsl-utils@npm:^0.1.0": + version: 0.1.0 + resolution: "wsl-utils@npm:0.1.0" + dependencies: + is-wsl: "npm:^3.1.0" + checksum: 10c0/44318f3585eb97be994fc21a20ddab2649feaf1fbe893f1f866d936eea3d5f8c743bec6dc02e49fbdd3c0e69e9b36f449d90a0b165a4f47dd089747af4cf2377 + languageName: node + linkType: hard + +"yallist@npm:^4.0.0": + version: 4.0.0 + resolution: "yallist@npm:4.0.0" + checksum: 10c0/2286b5e8dbfe22204ab66e2ef5cc9bbb1e55dfc873bbe0d568aa943eb255d131890dfd5bf243637273d31119b870f49c18fcde2c6ffbb7a7a092b870dc90625a + languageName: node + linkType: hard + +"yallist@npm:^5.0.0": + version: 5.0.0 + resolution: "yallist@npm:5.0.0" + checksum: 10c0/a499c81ce6d4a1d260d4ea0f6d49ab4da09681e32c3f0472dee16667ed69d01dae63a3b81745a24bd78476ec4fcf856114cb4896ace738e01da34b2c42235416 + languageName: node + linkType: hard + +"yocto-queue@npm:^0.1.0": + version: 0.1.0 + resolution: "yocto-queue@npm:0.1.0" + checksum: 10c0/dceb44c28578b31641e13695d200d34ec4ab3966a5729814d5445b194933c096b7ced71494ce53a0e8820685d1d010df8b2422e5bf2cdea7e469d97ffbea306f + languageName: node + linkType: hard + +"zod-to-json-schema@npm:^3.22.3": + version: 3.24.6 + resolution: "zod-to-json-schema@npm:3.24.6" + peerDependencies: + zod: ^3.24.1 + checksum: 10c0/b907ab6d057100bd25a37e5545bf5f0efa5902cd84d3c3ec05c2e51541431a47bd9bf1e5e151a244273409b45f5986d55b26e5d207f98abc5200702f733eb368 + languageName: node + linkType: hard + +"zod@npm:^3.23.8, zod@npm:^3.25.32": + version: 3.25.76 + resolution: "zod@npm:3.25.76" + checksum: 10c0/5718ec35e3c40b600316c5b4c5e4976f7fee68151bc8f8d90ec18a469be9571f072e1bbaace10f1e85cf8892ea12d90821b200e980ab46916a6166a4260a983c + languageName: node + linkType: hard + +"zod@npm:^4.0.10": + version: 4.0.10 + resolution: "zod@npm:4.0.10" + checksum: 10c0/8d1145e767c22b571a7967c198632f69ef15ce571b5021cdba84cf31d9af2ca40b033ea2fcbe5797cfd2da9c67b3a6ebe435938eabfbb1d1f3ab2f17f00f443b + languageName: node + linkType: hard diff --git a/docs/_scripts/js_translation/extract_codeblocks.py b/docs/_scripts/js_translation/extract_codeblocks.py new file mode 100755 index 000000000..b73f5fe30 --- /dev/null +++ b/docs/_scripts/js_translation/extract_codeblocks.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python +"""Extracts typescript code blocks from a markdown file.""" + +import argparse +import json +import re +import os +from typing import List, TypedDict, Literal + + +class CodeBlock(TypedDict): + """A code block extracted from a markdown file.""" + starting_line: int + """The line number where the code block starts in the source file""" + ending_line: int + """The line number where the code block ends in the source file""" + indentation: int + """Number of spaces/tabs used for indentation of the code block""" + source_file: str + """Path to the markdown file containing this code block""" + frontmatter: str + """Any metadata or frontmatter specified after the opening code fence""" + code: str + """The actual code content within the code block""" + language: str + """The language of the code block (e.g. typescript, javascript)""" + + +def extract_code_blocks(markdown_content: str, source_file: str) -> List[CodeBlock]: + """Extracts code blocks from a markdown file. + + Args: + markdown_content: The content of the markdown file. + source_file: The path to the markdown file. + + Returns: + A list of TypedDicts, where each dict represents a code block. + """ + # Regex to find code blocks with specified languages, capturing indentation + # and frontmatter. + pattern = re.compile( + r"^(?P<indentation>\s*)```(?P<language>typescript|javascript|ts|js)(?P<frontmatter>[^\n]*)\n(?P<code>.*?)\n^(?P=indentation)```\s*$", + re.DOTALL | re.MULTILINE, + ) + + code_blocks: List[CodeBlock] = [] + for match in pattern.finditer(markdown_content): + start_pos = match.start() + + # Calculate line numbers + starting_line = markdown_content.count("\n", 0, start_pos) + 1 + ending_line = starting_line + match.group(0).count("\n") + + indentation_str = match.group("indentation") + + code_block: CodeBlock = { + "starting_line": starting_line, + "ending_line": ending_line, + "indentation": len(indentation_str), + "source_file": source_file, + "frontmatter": match.group("frontmatter").strip(), + "code": match.group("code"), + "language": match.group("language"), + } + code_blocks.append(code_block) + + return code_blocks + +def dump_code_blocks(input_file: str, output_file: str, format: Literal["json", "inline"]) -> None: + """Function to extract and save code blocks from a markdown file. + + Args: + input_file: Path to the input markdown file. + output_file: Path to the output JSON file for the extracted code blocks. + format: Output format - either "json" or "inline" + """ + with open(input_file, "r", encoding="utf-8") as f: + markdown_content = f.read() + + extracted_code = extract_code_blocks(markdown_content, input_file) + + if len(extracted_code) == 0: + print(f"No code blocks found in {input_file}") + return + + if format == "json": + with open(output_file, "w", encoding="utf-8") as f: + json.dump(extracted_code, f, indent=2) + elif format == "inline": + with open(output_file, "w", encoding="utf-8") as f: + for code_block in extracted_code: + f.write(f"// {json.dumps({k:v for k,v in code_block.items() if k != 'code'})}\n") + f.write("\n") + f.write(code_block["code"]) + f.write("\n") + print(f"Extracted {len(extracted_code)} code blocks from {input_file} to {output_file}") + +def main(input_path: str, output_path: str, format: Literal["json", "inline"]) -> None: + """Main function to extract code blocks from a markdown file. + + Args: + input_file: Path to the input markdown file. + output_file: Path to the output JSON file for the extracted code blocks. + format: Output format - either "json" or "inline" + """ + # Check if input path is a directory + if os.path.isdir(input_path): + if os.path.isfile(output_path): + raise ValueError("If input_path is a directory, output_path must also be a directory") + if not os.path.isdir(output_path): + os.makedirs(output_path, exist_ok=True) + + # Process each markdown file in the directory recursively + for root, _, files in os.walk(input_path): + for filename in files: + if filename.endswith(".md"): + # Get relative path to maintain directory structure + rel_path = os.path.relpath(root, input_path) + input_file = os.path.join(root, filename) + # Create output directory if it doesn't exist + output_dir = os.path.join(output_path, rel_path) + os.makedirs(output_dir, exist_ok=True) + output_file = os.path.join(output_dir, filename.replace(".md", ".ts")) + dump_code_blocks(input_file, output_file, format) + else: + # Process single file + dump_code_blocks(input_path, output_path, format) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Extract typescript code blocks from a markdown file." + ) + parser.add_argument( + "input_file", + help="Path to the input markdown file.", + ) + parser.add_argument( + "output_file", + help="Path to the output JSON file for the extracted code blocks.", + ) + parser.add_argument( + "--format", + choices=["json", "inline"], + default="json", + help="Output format - either 'json' or 'inline'", + ) + args = parser.parse_args() + + main(args.input_file, args.output_file, args.format) diff --git a/docs/_scripts/notebook_hooks.py b/docs/_scripts/notebook_hooks.py index 6400a0da3..702255069 100644 --- a/docs/_scripts/notebook_hooks.py +++ b/docs/_scripts/notebook_hooks.py @@ -186,7 +186,7 @@ def _apply_conditional_rendering(md_text: str, target_language: str) -> str: pattern = re.compile( r"(?P<indent>[ \t]*):::(?P<language>\w+)\s*\n" r"(?P<content>((?:.*\n)*?))" # Capture the content inside the block - r"(?P=indent):::" # Match closing with the same indentation + r"(?P=indent)[ \t]*:::" # Match closing with the same indentation + any additional whitespace ) def replace_conditional_blocks(match: re.Match) -> str: @@ -301,8 +301,13 @@ def _on_page_markdown_with_config( # logger.info("Processing Jupyter notebook: %s", page.file.src_path) markdown = convert_notebook(page.file.abs_src_path) + target_language = kwargs.get( + "target_language", + os.environ.get("TARGET_LANGUAGE", "python") + ) + # Apply cross-reference preprocessing to all markdown content - markdown = _replace_autolinks(markdown, page.file.src_path) + markdown = _replace_autolinks(markdown, page.file.src_path, default_scope=target_language) # Append API reference links to code blocks if add_api_references: @@ -311,7 +316,6 @@ def _on_page_markdown_with_config( markdown = _highlight_code_blocks(markdown) # Apply conditional rendering for code blocks - target_language = kwargs.get("target_language", "python") markdown = _apply_conditional_rendering(markdown, target_language) # Add file path as an attribute to code blocks that are executable. diff --git a/docs/_scripts/third_party_page/create_third_party_page.py b/docs/_scripts/third_party_page/create_third_party_page.py index f0c8fea8f..2d9e485c1 100755 --- a/docs/_scripts/third_party_page/create_third_party_page.py +++ b/docs/_scripts/third_party_page/create_third_party_page.py @@ -15,9 +15,10 @@ If you’re looking for other prebuilt libraries, explore the community-built op below. These libraries can extend LangGraph's functionality in various ways. ## 📚 Available Libraries - [//]: # (This file is automatically generated using a script in docs/_scripts. Do not edit this file directly!) -{library_list} + +:::python +{python_library_list} ## ✨ Contributing Your Library @@ -28,16 +29,39 @@ To share your project, simply open a Pull Request adding an entry for your packa **Guidelines** -- Your repo must be distributed as an installable package (e.g., PyPI for Python, npm - for JavaScript/TypeScript, etc.) 📦 +- Your repo must be distributed as an installable package on PyPI 📦 - The repo should either use the Graph API (exposing a `StateGraph` instance) or the Functional API (exposing an `entrypoint`). - The package must include documentation (e.g., a `README.md` or docs site) explaining how to use it. - + We'll review your contribution and merge it in! Thanks for contributing! 🚀 +::: + +:::js +{js_library_list} + +## ✨ Contributing Your Library + +Have you built an awesome open-source library using LangGraph? We'd love to feature +your project on the official LangGraph documentation pages! 🏆 + +To share your project, simply open a Pull Request adding an entry for your package in our [packages.yml]({langgraph_url}) file. + +**Guidelines** + +- Your repo must be distributed as an installable package on npm 📦 +- The repo should either use the Graph API (exposing a `StateGraph` instance) or + the Functional API (exposing an `entrypoint`). +- The package must include documentation (e.g., a `README.md` or docs site) + explaining how to use it. + +We'll review your contribution and merge it in! + +Thanks for contributing! 🚀 +::: """ @@ -46,36 +70,18 @@ class ResolvedPackage(TypedDict): """The name of the package.""" repo: str """Repository ID within github. Format is: [orgname]/[repo_name].""" + monorepo_path: str | None + """Optional: The path to the package in the monorepo. Must be relative to the root of the monorepo.""" + language: str + """The language of the package. (either 'python' or 'js')""" weekly_downloads: int | None """The weekly download count of the package.""" description: str """A brief description of what the package does.""" - -def generate_markdown(resolved_packages: List[ResolvedPackage], language: str) -> str: - """Generate the markdown content for the third party page. - - Args: - resolved_packages: A list of resolved package information. - language: str - - Returns: - The markdown content as a string. +def generate_package_table(resolved_packages: List[ResolvedPackage]) -> str: + """Generate the package table for the third party page. """ - # Update the URL to the actual file once the initial version is merged - if language == "python": - langgraph_url = ( - "https://github.com/langchain-ai/langgraph/blob/main/docs" - "/_scripts/third_party_page/packages.yml" - ) - elif language == "js": - langgraph_url = ( - "https://github.com/langchain-ai/langgraphjs/blob/main/docs" - "/_scripts/third_party/packages.yml" - ) - else: - raise ValueError(f"Invalid language '{language}'. Expected 'python' or 'js'.") - sorted_packages = sorted( resolved_packages, key=lambda p: p["weekly_downloads"] or 0, reverse=True ) @@ -85,7 +91,15 @@ def generate_markdown(resolved_packages: List[ResolvedPackage], language: str) - ] for package in sorted_packages: name = f"**{package['name']}**" - repo_url = f"[{package['repo']}](https://github.com/{package['repo']})" + + monorepo_path = package.get("monorepo_path", "") + if monorepo_path: + monorepo_path = monorepo_path[1:] if monorepo_path.startswith('/') else monorepo_path + repo_url_suffix = f"/tree/main/{monorepo_path}" + else: + repo_url_suffix = "" + repo_url = f"https://github.com/{package['repo']}{repo_url_suffix}" + stars_badge = ( f"https://img.shields.io/github/stars/{package['repo']}?style=social" ) @@ -93,13 +107,39 @@ def generate_markdown(resolved_packages: List[ResolvedPackage], language: str) - downloads = package["weekly_downloads"] or "-" row = f"| {name} | {repo_url} | {package['description']} | {downloads} | {stars}" rows.append(row) + return "\n".join(rows) + +def generate_markdown(resolved_packages: List[ResolvedPackage]) -> str: + """Generate the markdown content for the third party page. + + Args: + resolved_packages: A list of resolved package information. + + Returns: + The markdown content as a string. + """ + # Update the URL to the actual file once the initial version is merged + langgraph_url = ( + "https://github.com/langchain-ai/langgraph/blob/main/docs" + "/_scripts/third_party_page/packages.yml" + ) + + python_library_list = generate_package_table( + [p for p in resolved_packages if p["language"] == "python"] + ) + js_library_list = generate_package_table( + [p for p in resolved_packages if p["language"] == "js"] + ) + markdown_content = MARKDOWN.format( - library_list="\n".join(rows), langgraph_url=langgraph_url + python_library_list=python_library_list, + js_library_list=js_library_list, + langgraph_url=langgraph_url, ) return markdown_content -def main(input_file: str, output_file: str, language: str) -> None: +def main(input_file: str, output_file: str) -> None: """Main function to create the third party page. Args: @@ -111,7 +151,7 @@ def main(input_file: str, output_file: str, language: str) -> None: with open(input_file, "r") as f: resolved_packages: List[ResolvedPackage] = yaml.safe_load(f) - markdown_content = generate_markdown(resolved_packages, language) + markdown_content = generate_markdown(resolved_packages) # Write the markdown content to the output file with open(output_file, "w", encoding="utf-8") as f: @@ -127,12 +167,6 @@ if __name__ == "__main__": parser.add_argument( "output_file", help="Path to the output file for the third party page." ) - parser.add_argument( - "--language", - choices=["python", "js"], - default="python", - help="The language for which to generate the third party page. Defaults to 'python'.", - ) args = parser.parse_args() - main(args.input_file, args.output_file, args.language) + main(args.input_file, args.output_file) diff --git a/docs/_scripts/third_party_page/get_download_stats.py b/docs/_scripts/third_party_page/get_download_stats.py index 582aa269f..bd43ecda8 100755 --- a/docs/_scripts/third_party_page/get_download_stats.py +++ b/docs/_scripts/third_party_page/get_download_stats.py @@ -11,101 +11,146 @@ import yaml class Package(TypedDict): - """A TypedDict representing a package""" - name: str """The name of the package.""" repo: str """Repository ID within github. Format is: [orgname]/[repo_name].""" + monorepo_path: str | None + """The path to the package in the monorepo. Only used for JS packages.""" description: str """A brief description of what the package does.""" - class ResolvedPackage(Package): weekly_downloads: int | None - + """The weekly download count of the package.""" + language: str + """The language of the package. (either 'python' or 'js')""" HERE = pathlib.Path(__file__).parent PACKAGES_FILE = HERE / "packages.yml" -PACKAGES = yaml.safe_load(PACKAGES_FILE.read_text())['packages'] +PACKAGES = yaml.safe_load(PACKAGES_FILE.read_text())["packages"] +def _get_pypi_downloads(package: Package) -> int: + """Retrieve the weekly download count for a package from PyPIStats.""" -def _get_weekly_downloads(packages: list[Package], fake: bool) -> list[ResolvedPackage]: - """Retrieve the monthly download count for a list of packages from PyPIStats.""" + # First check if package exists on PyPI + pypi_url = f"https://pypi.org/pypi/{package['name']}/json" + try: + pypi_response = requests.get(pypi_url) + pypi_response.raise_for_status() + except requests.exceptions.HTTPError: + raise AssertionError(f"Package {package['name']} does not exist on PyPI") + + # Get first release date + pypi_data = pypi_response.json() + releases = pypi_data["releases"] + first_release_date = None + for version_releases in releases.values(): + if version_releases: # Some versions may be empty lists + upload_time = datetime.fromisoformat(version_releases[0]["upload_time"]) + if first_release_date is None or upload_time < first_release_date: + first_release_date = upload_time + + if first_release_date is None: + raise AssertionError(f"Package {package['name']} has no releases yet") + + # If package was published in last 48 hours, skip download stats + if (datetime.now() - first_release_date).total_seconds() >= 48 * 3600: + url = f"https://pypistats.org/api/packages/{package['name']}/overall" + + response = requests.get(url) + response.raise_for_status() + data = response.json() + + sorted_data = sorted( + data["data"], + key=lambda x: datetime.strptime(x["date"], "%Y-%m-%d"), + reverse=True, + ) + + # Sum the last 7 days of downloads + return sum(entry["downloads"] for entry in sorted_data[:7]) + else: + return None + +def _get_npm_downloads(package: Package) -> int: + """Retrieve the weekly download count for a package on the npm registry.""" + + # Check if package exists on the npm registry + npm_url = f"https://registry.npmjs.org/{package['name']}" + try: + npm_response = requests.get(npm_url) + npm_response.raise_for_status() + except requests.exceptions.HTTPError: + raise AssertionError(f"Package {package['name']} does not exist on npm registry") + + npm_data = npm_response.json() + + # Retrieve the first publish date using the 'created' timestamp from the 'time' field. + created_str = npm_data.get("time", {}).get("created") + if created_str is None: + raise AssertionError(f"Package {package['name']} has no creation time in registry data") + # Remove the trailing 'Z' if present and parse the ISO format timestamp + first_publish_date = datetime.fromisoformat(created_str.rstrip("Z")) + + # If package was published more than 48 hours ago, fetch download stats. + if (datetime.now() - first_publish_date).total_seconds() >= 48 * 3600: + stats_url = f"https://api.npmjs.org/downloads/point/last-week/{package['name']}" + stats_response = requests.get(stats_url) + stats_response.raise_for_status() + stats_data = stats_response.json() + return stats_data.get("downloads", None) + else: + return None + +def _get_weekly_downloads(packages: dict[str, list[Package]], fake: bool) -> list[ResolvedPackage]: + """Retrieve the weekly download count for a dictionary of python or js packages.""" resolved_packages: list[ResolvedPackage] = [] if fake: # To avoid making network requests during testing, return fake download counts - for package in packages: + for language, package_list in packages.items(): + for package in package_list: + resolved_packages.append( + { + "name": package["name"], + "repo": package["repo"], + "monorepo_path": package.get("monorepo_path", None), + "language": language, + "description": package["description"], + "weekly_downloads": -12345, + } + ) + return resolved_packages + + for language, package_list in packages.items(): + for package in package_list: + if language == "python": + num_downloads = _get_pypi_downloads(package) + elif language == "js": + num_downloads = _get_npm_downloads(package) + else: + num_downloads = None + resolved_packages.append( { "name": package["name"], "repo": package["repo"], - "weekly_downloads": -12345, + "monorepo_path": package.get("monorepo_path", None), + "language": language, "description": package["description"], + "weekly_downloads": num_downloads, } ) - return resolved_packages - - for package in packages: - # First check if package exists on PyPI - pypi_url = f"https://pypi.org/pypi/{package['name']}/json" - try: - pypi_response = requests.get(pypi_url) - pypi_response.raise_for_status() - except requests.exceptions.HTTPError: - raise AssertionError(f"Package {package['name']} does not exist on PyPI") - - # Get first release date - pypi_data = pypi_response.json() - releases = pypi_data["releases"] - first_release_date = None - for version_releases in releases.values(): - if version_releases: # Some versions may be empty lists - upload_time = datetime.fromisoformat(version_releases[0]["upload_time"]) - if first_release_date is None or upload_time < first_release_date: - first_release_date = upload_time - - if first_release_date is None: - raise AssertionError(f"Package {package['name']} has no releases yet") - - # If package was published in last 48 hours, skip download stats - if (datetime.now() - first_release_date).total_seconds() >= 48 * 3600: - url = f"https://pypistats.org/api/packages/{package['name']}/overall" - - response = requests.get(url) - response.raise_for_status() - data = response.json() - - sorted_data = sorted( - data["data"], - key=lambda x: datetime.strptime(x["date"], "%Y-%m-%d"), - reverse=True, - ) - - # Sum the last 7 days of downloads - num_downloads = sum(entry["downloads"] for entry in sorted_data[:7]) - else: - num_downloads = None - - resolved_packages.append( - { - "name": package["name"], - "repo": package["repo"], - "weekly_downloads": num_downloads, - "description": package["description"], - } - ) return resolved_packages - - def main(output_file: str, fake: bool) -> None: """Main function to generate package download information. Args: output_file: Path to the output YAML file. + fake: If True, use fake download counts for testing purposes. """ resolved_packages: list[ResolvedPackage] = _get_weekly_downloads(PACKAGES, fake) diff --git a/docs/_scripts/third_party_page/packages.yml b/docs/_scripts/third_party_page/packages.yml index 5028fd8e8..ed8b4d26f 100644 --- a/docs/_scripts/third_party_page/packages.yml +++ b/docs/_scripts/third_party_page/packages.yml @@ -1,41 +1,58 @@ #A list of third-party packages to surface on the third-party page. packages: - - name: "trustcall" - repo: "hinthornw/trustcall" - description: "Tenacious tool calling built on LangGraph." - - name: "breeze-agent" - repo: "andrestorres123/breeze-agent" - description: "A streamlined research system built inspired on STORM and built on LangGraph." - - name: "langgraph-supervisor" - repo: "langchain-ai/langgraph-supervisor-py" - description: "Build supervisor multi-agent systems with LangGraph." - - name: "langmem" - repo: "langchain-ai/langmem" - description: "Build agents that learn and adapt from interactions over time." - - name: "langchain-mcp-adapters" - repo: "langchain-ai/langchain-mcp-adapters" - description: "Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph agents." - - name: "open-deep-research" - repo: "langchain-ai/open_deep_research" - description: "Open source assistant for iterative web research and report writing." - - name: "langgraph-swarm" - repo: "langchain-ai/langgraph-swarm-py" - description: "Build swarm-style multi-agent systems using LangGraph." - - name: "delve-taxonomy-generator" - repo: "andrestorres123/delve" - description: "A taxonomy generator for unstructured data" - - name: "nodeology" - repo: "xyin-anl/Nodeology" - description: "Enable researcher to build scientific workflows easily with simplified interface." - - name: "langgraph-bigtool" - repo: "langchain-ai/langgraph-bigtool" - description: "Build LangGraph agents with large numbers of tools." - - name: "ai-data-science-team" - repo: "business-science/ai-data-science-team" - description: "An AI-powered data science team of agents to help you perform common data science tasks 10X faster." - - name: "langgraph-reflection" - repo: "langchain-ai/langgraph-reflection" - description: "LangGraph agent that runs a reflection step." - - name: "langgraph-codeact" - repo: "langchain-ai/langgraph-codeact" - description: "LangGraph implementation of CodeAct agent that generates and executes code instead of tool calling." + python: + - name: "trustcall" + repo: "hinthornw/trustcall" + description: "Tenacious tool calling built on LangGraph." + - name: "breeze-agent" + repo: "andrestorres123/breeze-agent" + description: "A streamlined research system built inspired on STORM and built on LangGraph." + - name: "langgraph-supervisor" + repo: "langchain-ai/langgraph-supervisor-py" + description: "Build supervisor multi-agent systems with LangGraph." + - name: "langmem" + repo: "langchain-ai/langmem" + description: "Build agents that learn and adapt from interactions over time." + - name: "langchain-mcp-adapters" + repo: "langchain-ai/langchain-mcp-adapters" + description: "Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph agents." + - name: "open-deep-research" + repo: "langchain-ai/open_deep_research" + description: "Open source assistant for iterative web research and report writing." + - name: "langgraph-swarm" + repo: "langchain-ai/langgraph-swarm-py" + description: "Build swarm-style multi-agent systems using LangGraph." + - name: "delve-taxonomy-generator" + repo: "andrestorres123/delve" + description: "A taxonomy generator for unstructured data" + - name: "nodeology" + repo: "xyin-anl/Nodeology" + description: "Enable researcher to build scientific workflows easily with simplified interface." + - name: "langgraph-bigtool" + repo: "langchain-ai/langgraph-bigtool" + description: "Build LangGraph agents with large numbers of tools." + - name: "ai-data-science-team" + repo: "business-science/ai-data-science-team" + description: "An AI-powered data science team of agents to help you perform common data science tasks 10X faster." + - name: "langgraph-reflection" + repo: "langchain-ai/langgraph-reflection" + description: "LangGraph agent that runs a reflection step." + - name: "langgraph-codeact" + repo: "langchain-ai/langgraph-codeact" + description: "LangGraph implementation of CodeAct agent that generates and executes code instead of tool calling." + js: + - name: "@langchain/mcp-adapters" + repo: "langchain-ai/langchainjs" + description: "Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph agents." + - name: "@langchain/langgraph-supervisor" + repo: "langchain-ai/langgraphjs" + monorepo_path: "libs/langgraph-supervisor" + description: "Build supervisor multi-agent systems with LangGraph" + - name: "@langchain/langgraph-swarm" + repo: "langchain-ai/langgraphjs" + monorepo_path: "libs/langgraph-swarm" + description: "Build multi-agent swarms with LangGraph" + - name: "@langchain/langgraph-cua" + repo: "langchain-ai/langgraphjs" + monorepo_path: "libs/langgraph-cua" + description: "Build computer use agents with LangGraph" diff --git a/docs/docs/agents/agents.md b/docs/docs/agents/agents.md index 04ee419ce..74514bd41 100644 --- a/docs/docs/agents/agents.md +++ b/docs/docs/agents/agents.md @@ -15,23 +15,40 @@ This guide shows you how to set up and use LangGraph's **prebuilt**, **reusable* Before you start this tutorial, ensure you have the following: -- An [Anthropic](https://console.anthropic.com/settings/keys) API key +- An [Anthropic](https://console.anthropic.com/settings/keys) API key ## 1. Install dependencies If you haven't already, install LangGraph and LangChain: +:::python + ``` pip install -U langgraph "langchain[anthropic]" ``` -!!! info +!!! info LangChain is installed so the agent can call the [model](https://python.langchain.com/docs/integrations/chat/). +::: + +:::js + +```bash +npm install @langchain/langgraph @langchain/core @langchain/anthropic +``` + +!!! info + + LangChain is installed so the agent can call the [model](https://js.langchain.com/docs/integrations/chat/). + +::: + ## 2. Create an agent -To create an agent, use [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent]: +:::python +To create an agent, use @[`create_react_agent`][create_react_agent]: ```python from langgraph.prebuilt import create_react_agent @@ -56,9 +73,52 @@ agent.invoke( 2. Provide a language model for the agent to use. To learn more about configuring language models for the agents, check the [models](./models.md) page. 3. Provide a list of tools for the model to use. 4. Provide a system prompt (instructions) to the language model used by the agent. + ::: + +:::js +To create an agent, use [`createReactAgent`](https://langchain-ai.github.io/langgraphjs/reference/functions/langgraph_prebuilt.createReactAgent.html): + +```typescript +import { ChatAnthropic } from "@langchain/anthropic"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const getWeather = tool( + // (1)! + async ({ city }) => { + return `It's always sunny in ${city}!`; + }, + { + name: "get_weather", + description: "Get weather for a given city.", + schema: z.object({ + city: z.string().describe("The city to get weather for"), + }), + } +); + +const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "anthropic:claude-3-5-sonnet-latest" }), // (2)! + tools: [getWeather], // (3)! + stateModifier: "You are a helpful assistant", // (4)! +}); + +// Run the agent +await agent.invoke({ + messages: [{ role: "user", content: "what is the weather in sf" }], +}); +``` + +1. Define a tool for the agent to use. Tools can be defined using the `tool` function. For more advanced tool usage and customization, check the [tools](./tools.md) page. +2. Provide a language model for the agent to use. To learn more about configuring language models for the agents, check the [models](./models.md) page. +3. Provide a list of tools for the model to use. +4. Provide a system prompt (instructions) to the language model used by the agent. + ::: ## 3. Configure an LLM +:::python To configure an LLM with specific parameters, such as temperature, use [init_chat_model](https://python.langchain.com/api_reference/langchain/chat_models/langchain.chat_models.base.init_chat_model.html): ```python @@ -79,19 +139,45 @@ agent = create_react_agent( ) ``` +::: + +:::js +To configure an LLM with specific parameters, such as temperature, use a model instance: + +```typescript +import { ChatAnthropic } from "@langchain/anthropic"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +// highlight-next-line +const model = new ChatAnthropic({ + model: "claude-3-5-sonnet-latest", + // highlight-next-line + temperature: 0, +}); + +const agent = createReactAgent({ + // highlight-next-line + llm: model, + tools: [getWeather], +}); +``` + +::: + For more information on how to configure LLMs, see [Models](./models.md). ## 4. Add a custom prompt Prompts instruct the LLM how to behave. Add one of the following types of prompts: -* **Static**: A string is interpreted as a **system message**. -* **Dynamic**: A list of messages generated at **runtime**, based on input or configuration. +- **Static**: A string is interpreted as a **system message**. +- **Dynamic**: A list of messages generated at **runtime**, based on input or configuration. === "Static prompt" Define a fixed prompt string or list of messages: + :::python ```python from langgraph.prebuilt import create_react_agent @@ -107,9 +193,30 @@ Prompts instruct the LLM how to behave. Add one of the following types of prompt {"messages": [{"role": "user", "content": "what is the weather in sf"}]} ) ``` + ::: + + :::js + ```typescript + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import { ChatAnthropic } from "@langchain/anthropic"; + + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "anthropic:claude-3-5-sonnet-latest" }), + tools: [getWeather], + // A static prompt that never changes + // highlight-next-line + stateModifier: "Never answer questions about the weather." + }); + + await agent.invoke({ + messages: [{ role: "user", content: "what is the weather in sf" }] + }); + ``` + ::: === "Dynamic prompt" + :::python Define a function that returns a message list based on the agent's state and configuration: ```python @@ -144,12 +251,52 @@ Prompts instruct the LLM how to behave. Add one of the following types of prompt - Internal agent state updated during a multi-step reasoning process (using `state`). Dynamic prompts can be defined as functions that take `state` and `config` and return a list of messages to send to the LLM. + ::: + + :::js + Define a function that returns messages based on the agent's state and configuration: + + ```typescript + import { type BaseMessageLike } from "@langchain/core/messages"; + import { type RunnableConfig } from "@langchain/core/runnables"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + + // highlight-next-line + const dynamicPrompt = (state: { messages: BaseMessageLike[] }, config: RunnableConfig): BaseMessageLike[] => { // (1)! + const userName = config.configurable?.user_name; + const systemMsg = `You are a helpful assistant. Address the user as ${userName}.`; + return [{ role: "system", content: systemMsg }, ...state.messages]; + }; + + const agent = createReactAgent({ + llm: "anthropic:claude-3-5-sonnet-latest", + tools: [getWeather], + // highlight-next-line + stateModifier: dynamicPrompt + }); + + await agent.invoke( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + // highlight-next-line + { configurable: { user_name: "John Smith" } } + ); + ``` + + 1. Dynamic prompts allow including non-message [context](./context.md) when constructing an input to the LLM, such as: + + - Information passed at runtime, like a `user_id` or API credentials (using `config`). + - Internal agent state updated during a multi-step reasoning process (using `state`). + + Dynamic prompts can be defined as functions that take `state` and `config` and return a list of messages to send to the LLM. + ::: For more information, see [Context](./context.md). ## 5. Add memory -To allow multi-turn conversations with an agent, you need to enable [persistence](../concepts/persistence.md) by providing a `checkpointer` when creating an agent. At runtime, you need to provide a config containing `thread_id` — a unique identifier for the conversation (session): +To allow multi-turn conversations with an agent, you need to enable [persistence](../concepts/persistence.md) by providing a checkpointer when creating an agent. At runtime, you need to provide a config containing `thread_id` — a unique identifier for the conversation (session): + +:::python ```python from langgraph.prebuilt import create_react_agent @@ -182,8 +329,50 @@ ny_response = agent.invoke( 1. `checkpointer` allows the agent to store its state at every step in the tool calling loop. This enables [short-term memory](../how-tos/memory/add-memory.md#add-short-term-memory) and [human-in-the-loop](../concepts/human_in_the_loop.md) capabilities. 2. Pass configuration with `thread_id` to be able to resume the same conversation on future agent invocations. + ::: +:::js + +```typescript +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { MemorySaver } from "@langchain/langgraph"; + +// highlight-next-line +const checkpointer = new MemorySaver(); + +const agent = createReactAgent({ + llm: "anthropic:claude-3-5-sonnet-latest", + tools: [getWeather], + // highlight-next-line + checkpointSaver: checkpointer, // (1)! +}); + +// Run the agent +// highlight-next-line +const config = { configurable: { thread_id: "1" } }; +const sfResponse = await agent.invoke( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + // highlight-next-line + config // (2)! +); +const nyResponse = await agent.invoke( + { messages: [{ role: "user", content: "what about new york?" }] }, + // highlight-next-line + config +); +``` + +1. `checkpointSaver` allows the agent to store its state at every step in the tool calling loop. This enables [short-term memory](../how-tos/memory/add-memory.md#add-short-term-memory) and [human-in-the-loop](../concepts/human_in_the_loop.md) capabilities. +2. Pass configuration with `thread_id` to be able to resume the same conversation on future agent invocations. + ::: + +:::python When you enable the checkpointer, it stores agent state at every step in the provided checkpointer database (or in memory, if using `InMemorySaver`). +::: + +:::js +When you enable the checkpointer, it stores agent state at every step in the provided checkpointer database (or in memory, if using `MemorySaver`). +::: Note that in the above example, when the agent is invoked the second time with the same `thread_id`, the original message history from the first conversation is automatically included, together with the new user input. @@ -191,6 +380,7 @@ For more information, see [Memory](../how-tos/memory/add-memory.md). ## 6. Configure structured output +:::python To produce structured responses conforming to a schema, use the `response_format` parameter. The schema can be defined with a `Pydantic` model or `TypedDict`. The result will be accessible via the `structured_response` field. ```python @@ -215,9 +405,43 @@ response = agent.invoke( response["structured_response"] ``` -1. When `response_format` is provided, a separate step is added at the end of the agent loop: agent message history is passed to an LLM with structured output to generate a structured response. +1. When `response_format` is provided, a separate step is added at the end of the agent loop: agent message history is passed to an LLM with structured output to generate a structured response. - To provide a system prompt to this LLM, use a tuple `(prompt, schema)`, e.g., `response_format=(prompt, WeatherResponse)`. + To provide a system prompt to this LLM, use a tuple `(prompt, schema)`, e.g., `response_format=(prompt, WeatherResponse)`. + + ::: + +:::js +To produce structured responses conforming to a schema, use the `responseFormat` parameter. The schema can be defined with a `Zod` schema. The result will be accessible via the `structuredResponse` field. + +```typescript +import { z } from "zod"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const WeatherResponse = z.object({ + conditions: z.string(), +}); + +const agent = createReactAgent({ + llm: "anthropic:claude-3-5-sonnet-latest", + tools: [getWeather], + // highlight-next-line + responseFormat: WeatherResponse, // (1)! +}); + +const response = await agent.invoke({ + messages: [{ role: "user", content: "what is the weather in sf" }], +}); + +// highlight-next-line +response.structuredResponse; +``` + +1. When `responseFormat` is provided, a separate step is added at the end of the agent loop: agent message history is passed to an LLM with structured output to generate a structured response. + + To provide a system prompt to this LLM, use an object `{ prompt, schema }`, e.g., `responseFormat: { prompt, schema: WeatherResponse }`. + + ::: !!! Note "LLM post-processing" diff --git a/docs/docs/agents/context.md b/docs/docs/agents/context.md index d8db38c49..6d0a60d96 100644 --- a/docs/docs/agents/context.md +++ b/docs/docs/agents/context.md @@ -2,15 +2,13 @@ **Context engineering** is the practice of building dynamic systems that provide the right information and tools, in the right format, so that an AI application can accomplish a task. Context can be characterized along two key dimensions: + 1. By **mutability**: - - - **Static context**: Immutable data that doesn't change during execution (e.g., user metadata, database connections, tools) - - **Dynamic context**: Mutable data that evolves as the application runs (e.g., conversation history, intermediate results, tool call observations) - + - **Static context**: Immutable data that doesn't change during execution (e.g., user metadata, database connections, tools) + - **Dynamic context**: Mutable data that evolves as the application runs (e.g., conversation history, intermediate results, tool call observations) 2. By **lifetime**: - - - **Runtime context**: Data scoped to a single run or invocation - - **Cross-conversation context**: Data that persists across multiple conversations or sessions + - **Runtime context**: Data scoped to a single run or invocation + - **Cross-conversation context**: Data that persists across multiple conversations or sessions !!! tip "Runtime context vs LLM context" @@ -24,11 +22,13 @@ LangGraph provides three ways to manage context, which combines the mutability and lifetime dimensions: -| Context type | Description | Mutability | Lifetime | Access method | -|------------------------------------------------------------------------------|--------------------------------------------------------|------------|-------------------------|-----------------------------------| -| [**Static runtime context**](#static-runtime-context) | User metadata, tools, db connections passed at startup | Static | Single run | `context` argument to `invoke`/`stream` | -| [**Dynamic runtime context (state)**](#dynamic-runtime-context-state) | Mutable data that evolves during a single run | Dynamic | Single run | LangGraph state object | -| [**Dynamic cross-conversation context (store)**](#dynamic-cross-conversation-context-store) | Persistent data shared across conversations | Dynamic | Cross-conversation | LangGraph store | +:::python + +| Context type | Description | Mutability | Lifetime | Access method | +| ------------------------------------------------------------------------------------------- | ------------------------------------------------------ | ---------- | ------------------ | --------------------------------------- | +| [**Static runtime context**](#static-runtime-context) | User metadata, tools, db connections passed at startup | Static | Single run | `context` argument to `invoke`/`stream` | +| [**Dynamic runtime context (state)**](#dynamic-runtime-context-state) | Mutable data that evolves during a single run | Dynamic | Single run | LangGraph state object | +| [**Dynamic cross-conversation context (store)**](#dynamic-cross-conversation-context-store) | Persistent data shared across conversations | Dynamic | Cross-conversation | LangGraph store | ## Static runtime context @@ -51,12 +51,38 @@ graph.invoke( # (1)! ) ``` +::: + +:::js + +| Type | Description | Mutable? | Lifetime | +| ---------------------------------------------------------------------------- | --------------------------------------------- | -------- | ----------------------- | +| [**Config**](#config-static-context) | data passed at the start of a run | ❌ | per run | +| [**Short-term memory (State)**](#short-term-memory-mutable-context) | dynamic data that can change during execution | ✅ | per run or conversation | +| [**Long-term memory (Store)**](#long-term-memory-cross-conversation-context) | data that can be shared between conversations | ✅ | across conversations | + +Config is for immutable data like user metadata or API keys. Use this when you have values that don't change mid-run. + +Specify configuration using a key called **"configurable"** which is reserved for this purpose. + +```typescript +await graph.invoke( + // (1)! + { messages: [{ role: "user", content: "hi!" }] }, // (2)! + // highlight-next-line + { configurable: { user_id: "user_123" } } // (3)! +); +``` + +::: + 1. This is the invocation of the agent or graph. The `invoke` method runs the underlying graph with the provided input. 2. This example uses messages as an input, which is common, but your application may use different input structures. 3. This is where you pass the runtime data. The `context` parameter allows you to provide additional dependencies that the agent can use during its execution. === "Agent prompt" + :::python ```python from langchain_core.messages import AnyMessage from langgraph.runtime import get_runtime @@ -82,11 +108,42 @@ graph.invoke( # (1)! context={"user_name": "John Smith"} ) ``` + ::: + + :::js + ```typescript + import type { BaseMessage } from "@langchain/core/messages"; + import type { RunnableConfig } from "@langchain/core/runnables"; + import type { AgentState } from "@langchain/langgraph/prebuilt"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + + // highlight-next-line + const prompt = (state: AgentState, config: RunnableConfig): BaseMessage[] => { + const userName = config.configurable?.user_name; + const systemMsg = `You are a helpful assistant. Address the user as ${userName}.`; + return [{ role: "system", content: systemMsg }, ...state.messages]; + }; + + const agent = createReactAgent({ + llm: model, + tools: [getWeather], + prompt, + }); + + await agent.invoke( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + // highlight-next-line + { configurable: { user_name: "John Smith" } } + ); + ``` + ::: + * See [Agents](../agents/agents.md) for details. === "Workflow node" + :::python ```python from langgraph.runtime import Runtime @@ -95,11 +152,25 @@ graph.invoke( # (1)! user_name = runtime.context.user_name ... ``` + ::: + + :::js + ```typescript + import type { RunnableConfig } from "@langchain/core/runnables"; + + // highlight-next-line + const node = (state: State, config?: RunnableConfig) => { + const userName = config?.configurable?.user_name; + // ... + }; + ``` + ::: * See [the Graph API](https://langchain-ai.github.io/langgraph/how-tos/graph-api/#add-runtime-configuration) for details. === "In a tool" + :::python ```python from langgraph.runtime import get_runtime @@ -112,6 +183,27 @@ graph.invoke( # (1)! email = get_user_email_from_db(runtime.context.user_name) return email ``` + ::: + + :::js + ```typescript + import type { RunnableConfig } from "@langchain/core/runnables"; + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + + // highlight-next-line + const getUserInfo = tool( + async (_, config: RunnableConfig): Promise<string> => { + const userId = config.configurable?.user_id; + return userId === "user_123" ? "User is John Smith" : "Unknown user"; + }, + { + name: "get_user_info", + description: "Retrieve user information based on user ID." + } + ); + ``` + ::: See the [tool calling guide](../how-tos/tool-calling.md#configuration) for details. @@ -130,6 +222,7 @@ graph.invoke( # (1)! State can also be accessed by the agent's **tools**, which can read or update the state as needed. See [tool calling guide](../how-tos/tool-calling.md#short-term-memory) for details. + :::python ```python from langchain_core.messages import AnyMessage from langchain_core.runnables import RunnableConfig @@ -164,10 +257,51 @@ graph.invoke( # (1)! 1. Define a custom state schema that extends `AgentState` or `MessagesState`. 2. Pass the custom state schema to the agent. This allows the agent to access and modify the state during execution. + ::: + :::js + ```typescript + import type { BaseMessage } from "@langchain/core/messages"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import { MessagesZodState } from "@langchain/langgraph"; + import { z } from "zod"; + + // highlight-next-line + const CustomState = z.object({ // (1)! + messages: MessagesZodState.shape.messages, + userName: z.string(), + }); + + const prompt = ( + // highlight-next-line + state: z.infer<typeof CustomState> + ): BaseMessage[] => { + const userName = state.userName; + const systemMsg = `You are a helpful assistant. User's name is ${userName}`; + return [{ role: "system", content: systemMsg }, ...state.messages]; + }; + + const agent = createReactAgent({ + llm: model, + tools: [...], + // highlight-next-line + stateSchema: CustomState, // (2)! + stateModifier: prompt, + }); + + await agent.invoke({ + messages: [{ role: "user", content: "hi!" }], + userName: "John Smith", + }); + ``` + + 1. Define a custom state schema that extends `MessagesZodState` or creates a new schema. + 2. Pass the custom state schema to the agent. This allows the agent to access and modify the state during execution. + ::: === "In a workflow" + :::python ```python from typing_extensions import TypedDict from langchain_core.messages import AnyMessage @@ -192,11 +326,42 @@ graph.invoke( # (1)! builder.set_entry_point("node") graph = builder.compile() ``` - + 1. Define a custom state 2. Access the state in any node or tool 3. The Graph API is designed to work as easily as possible with state. The return value of a node represents a requested update to the state. + ::: + :::js + ```typescript + import type { BaseMessage } from "@langchain/core/messages"; + import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; + import { z } from "zod"; + + // highlight-next-line + const CustomState = z.object({ // (1)! + messages: MessagesZodState.shape.messages, + extraField: z.number(), + }); + + const builder = new StateGraph(CustomState) + .addNode("node", async (state) => { // (2)! + const messages = state.messages; + // ... + return { // (3)! + // highlight-next-line + extraField: state.extraField + 1, + }; + }) + .addEdge(START, "node"); + + const graph = builder.compile(); + ``` + + 1. Define a custom state + 2. Access the state in any node or tool + 3. The Graph API is designed to work as easily as possible with state. The return value of a node represents a requested update to the state. + ::: !!! tip "Turning on memory" @@ -204,6 +369,6 @@ graph.invoke( # (1)! ## Dynamic cross-conversation context (store) -**Dynamic cross-conversation context** represents persistent, mutable data that spans across multiple conversations or sessions and is managed through the LangGraph store. This includes user profiles, preferences, and historical interactions. The LangGraph store acts as [long-term memory](../concepts/memory.md#long-term-memory) across multiple runs. This can be used to read or update persistent facts (e.g., user profiles, preferences, prior interactions). +**Dynamic cross-conversation context** represents persistent, mutable data that spans across multiple conversations or sessions and is managed through the LangGraph store. This includes user profiles, preferences, and historical interactions. The LangGraph store acts as [long-term memory](../concepts/memory.md#long-term-memory) across multiple runs. This can be used to read or update persistent facts (e.g., user profiles, preferences, prior interactions). -For more information, see the [Memory guide](../how-tos/memory/add-memory.md). \ No newline at end of file +For more information, see the [Memory guide](../how-tos/memory/add-memory.md). diff --git a/docs/docs/agents/evals.md b/docs/docs/agents/evals.md index ead956dd5..ff843e761 100644 --- a/docs/docs/agents/evals.md +++ b/docs/docs/agents/evals.md @@ -11,6 +11,8 @@ hide: To evaluate your agent's performance you can use `LangSmith` [evaluations](https://docs.smith.langchain.com/evaluation). You would need to first define an evaluator function to judge the results from an agent, such as final outputs or trajectory. Depending on your evaluation technique, this may or may not involve a reference output: +:::python + ```python def evaluator(*, outputs: dict, reference_outputs: dict): # compare agent outputs against reference outputs @@ -20,16 +22,51 @@ def evaluator(*, outputs: dict, reference_outputs: dict): return {"key": "evaluator_score", "score": score} ``` +::: + +:::js + +```typescript +type EvaluatorParams = { + outputs: Record<string, any>; + referenceOutputs: Record<string, any>; +}; + +function evaluator({ outputs, referenceOutputs }: EvaluatorParams) { + // compare agent outputs against reference outputs + const outputMessages = outputs.messages; + const referenceMessages = referenceOutputs.messages; + const score = compareMessages(outputMessages, referenceMessages); + return { key: "evaluator_score", score: score }; +} +``` + +::: + To get started, you can use prebuilt evaluators from `AgentEvals` package: +:::python + ```bash pip install -U agentevals ``` +::: + +:::js + +```bash +npm install agentevals +``` + +::: + ## Create evaluator A common way to evaluate agent performance is by comparing its trajectory (the order in which it calls its tools) against a reference trajectory: +:::python + ```python import json # highlight-next-line @@ -80,8 +117,63 @@ result = evaluator( ) ``` -1. Specify how the trajectories will be compared. `superset` will accept output trajectory as valid if it's a superset of the reference one. Other options include: [strict](https://github.com/langchain-ai/agentevals?tab=readme-ov-file#strict-match), [unordered](https://github.com/langchain-ai/agentevals?tab=readme-ov-file#unordered-match) and [subset](https://github.com/langchain-ai/agentevals?tab=readme-ov-file#subset-and-superset-match) +::: +:::js + +```typescript +import { createTrajectoryMatchEvaluator } from "agentevals/trajectory/match"; + +const outputs = [ + { + role: "assistant", + tool_calls: [ + { + function: { + name: "get_weather", + arguments: JSON.stringify({ city: "san francisco" }), + }, + }, + { + function: { + name: "get_directions", + arguments: JSON.stringify({ destination: "presidio" }), + }, + }, + ], + }, +]; + +const referenceOutputs = [ + { + role: "assistant", + tool_calls: [ + { + function: { + name: "get_weather", + arguments: JSON.stringify({ city: "san francisco" }), + }, + }, + ], + }, +]; + +// Create the evaluator +const evaluator = createTrajectoryMatchEvaluator({ + // Specify how the trajectories will be compared. `superset` will accept output trajectory as valid if it's a superset of the reference one. Other options include: strict, unordered and subset + trajectoryMatchMode: "superset", // (1)! +}); + +// Run the evaluator +const result = evaluator({ + outputs: outputs, + referenceOutputs: referenceOutputs, +}); +``` + +::: + +1. Specify how the trajectories will be compared. `superset` will accept output trajectory as valid if it's a superset of the reference one. Other options include: [strict](https://github.com/langchain-ai/agentevals?tab=readme-ov-file#strict-match), [unordered](https://github.com/langchain-ai/agentevals?tab=readme-ov-file#unordered-match) and [subset](https://github.com/langchain-ai/agentevals?tab=readme-ov-file#subset-and-superset-match) As a next step, learn more about how to [customize trajectory match evaluator](https://github.com/langchain-ai/agentevals?tab=readme-ov-file#agent-trajectory-match). @@ -89,6 +181,8 @@ As a next step, learn more about how to [customize trajectory match evaluator](h You can use LLM-as-a-judge evaluator that uses an LLM to compare the trajectory against the reference outputs and output a score: +:::python + ```python import json from agentevals.trajectory.llm import ( @@ -103,6 +197,24 @@ evaluator = create_trajectory_llm_as_judge( ) ``` +::: + +:::js + +```typescript +import { + createTrajectoryLlmAsJudge, + TRAJECTORY_ACCURACY_PROMPT_WITH_REFERENCE, +} from "agentevals/trajectory/llm"; + +const evaluator = createTrajectoryLlmAsJudge({ + prompt: TRAJECTORY_ACCURACY_PROMPT_WITH_REFERENCE, + model: "openai:o3-mini", +}); +``` + +::: + ## Run evaluator To run an evaluator, you will first need to create a [LangSmith dataset](https://docs.smith.langchain.com/evaluation/concepts#datasets). To use the prebuilt AgentEvals evaluators, you will need a dataset with the following schema: @@ -110,6 +222,8 @@ To run an evaluator, you will first need to create a [LangSmith dataset](https:/ - **input**: `{"messages": [...]}` input messages to call the agent with. - **output**: `{"messages": [...]}` expected message history in the agent output. For trajectory evaluation, you can choose to keep only assistant messages. +:::python + ```python from langsmith import Client from langgraph.prebuilt import create_react_agent @@ -125,4 +239,27 @@ experiment_results = client.evaluate( data="<Name of your dataset>", evaluators=[evaluator] ) -``` \ No newline at end of file +``` + +::: + +:::js + +```typescript +import { Client } from "langsmith"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { createTrajectoryMatchEvaluator } from "agentevals/trajectory/match"; + +const client = new Client(); +const agent = createReactAgent({...}); +const evaluator = createTrajectoryMatchEvaluator({...}); + +const experimentResults = await client.evaluate( + (inputs) => agent.invoke(inputs), + // replace with your dataset name + { data: "<Name of your dataset>" }, + { evaluators: [evaluator] } +); +``` + +::: diff --git a/docs/docs/agents/mcp.md b/docs/docs/agents/mcp.md index e204ac3bc..c4c029deb 100644 --- a/docs/docs/agents/mcp.md +++ b/docs/docs/agents/mcp.md @@ -9,10 +9,31 @@ hide: # Use MCP -The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide tools and context to language models. LangGraph agents can use tools defined on MCP servers through the `langchain-mcp-adapters` library. +[Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open protocol that standardizes how applications provide tools and context to language models. LangGraph agents can use tools defined on MCP servers through the `langchain-mcp-adapters` library. + +![MCP](./assets/mcp.png) + +:::python +Install the `langchain-mcp-adapters` library to use MCP tools in LangGraph: + +```bash +pip install langchain-mcp-adapters +``` + +::: + +:::js +Install the `@langchain/mcp-adapters` library to use MCP tools in LangGraph: + +```bash +npm install langchain-mcp-adapters +``` + +::: ## Use MCP tools +:::python The `langchain-mcp-adapters` package enables agents to use tools defined across one or more MCP servers. === "In an agent" @@ -125,10 +146,111 @@ The `langchain-mcp-adapters` package enables agents to use tools defined across ) ``` +::: +:::js +The `@langchain/mcp-adapters` package enables agents to use tools defined across one or more MCP servers. + +=== "In an agent" + + ```typescript title="Agent using tools defined on MCP servers" + // highlight-next-line + import { MultiServerMCPClient } from "langchain-mcp-adapters/client"; + import { ChatAnthropic } from "@langchain/langgraph/prebuilt"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + + // highlight-next-line + const client = new MultiServerMCPClient({ + math: { + command: "node", + // Replace with absolute path to your math_server.js file + args: ["/path/to/math_server.js"], + transport: "stdio", + }, + weather: { + // Ensure you start your weather server on port 8000 + url: "http://localhost:8000/mcp", + transport: "streamable_http", + }, + }); + + // highlight-next-line + const tools = await client.getTools(); + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-7-sonnet-latest" }), + // highlight-next-line + tools, + }); + + const mathResponse = await agent.invoke({ + messages: [{ role: "user", content: "what's (3 + 5) x 12?" }], + }); + + const weatherResponse = await agent.invoke({ + messages: [{ role: "user", content: "what is the weather in nyc?" }], + }); + ``` + +=== "In a workflow" + + ```typescript + import { MultiServerMCPClient } from "langchain-mcp-adapters/client"; + import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; + import { ToolNode } from "@langchain/langgraph/prebuilt"; + import { ChatOpenAI } from "@langchain/openai"; + import { AIMessage } from "@langchain/core/messages"; + import { z } from "zod"; + + const model = new ChatOpenAI({ model: "gpt-4" }); + + const client = new MultiServerMCPClient({ + math: { + command: "node", + // Make sure to update to the full absolute path to your math_server.js file + args: ["./examples/math_server.js"], + transport: "stdio", + }, + weather: { + // make sure you start your weather server on port 8000 + url: "http://localhost:8000/mcp/", + transport: "streamable_http", + }, + }); + + const tools = await client.getTools(); + + const builder = new StateGraph(MessagesZodState) + .addNode("callModel", async (state) => { + const response = await model.bindTools(tools).invoke(state.messages); + return { messages: [response] }; + }) + .addNode("tools", new ToolNode(tools)) + .addEdge(START, "callModel") + .addConditionalEdges("callModel", (state) => { + const lastMessage = state.messages.at(-1) as AIMessage | undefined; + if (!lastMessage?.tool_calls?.length) { + return "__end__"; + } + return "tools"; + }) + .addEdge("tools", "callModel"); + + const graph = builder.compile(); + + const mathResponse = await graph.invoke({ + messages: [{ role: "user", content: "what's (3 + 5) x 12?" }], + }); + + const weatherResponse = await graph.invoke({ + messages: [{ role: "user", content: "what is the weather in nyc?" }], + }); + ``` + +::: ## Custom MCP servers +:::python To create your own MCP servers, you can use the `mcp` library. This library provides a simple way to define tools and run them as servers. Install the MCP library: @@ -136,8 +258,24 @@ Install the MCP library: ```bash pip install mcp ``` + +::: + +:::js +To create your own MCP servers, you can use the `@modelcontextprotocol/sdk` library. This library provides a simple way to define tools and run them as servers. + +Install the MCP SDK: + +```bash +npm install @modelcontextprotocol/sdk +``` + +::: + Use the following reference implementations to test your agent with MCP tool servers. +:::python + ```python title="Example Math Server (stdio transport)" from mcp.server.fastmcp import FastMCP @@ -157,6 +295,115 @@ if __name__ == "__main__": mcp.run(transport="stdio") ``` +::: + +:::js + +```typescript title="Example Math Server (stdio transport)" +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; +import { + CallToolRequestSchema, + ListToolsRequestSchema, +} from "@modelcontextprotocol/sdk/types.js"; + +const server = new Server( + { + name: "math-server", + version: "0.1.0", + }, + { + capabilities: { + tools: {}, + }, + } +); + +server.setRequestHandler(ListToolsRequestSchema, async () => { + return { + tools: [ + { + name: "add", + description: "Add two numbers", + inputSchema: { + type: "object", + properties: { + a: { + type: "number", + description: "First number", + }, + b: { + type: "number", + description: "Second number", + }, + }, + required: ["a", "b"], + }, + }, + { + name: "multiply", + description: "Multiply two numbers", + inputSchema: { + type: "object", + properties: { + a: { + type: "number", + description: "First number", + }, + b: { + type: "number", + description: "Second number", + }, + }, + required: ["a", "b"], + }, + }, + ], + }; +}); + +server.setRequestHandler(CallToolRequestSchema, async (request) => { + switch (request.params.name) { + case "add": { + const { a, b } = request.params.arguments as { a: number; b: number }; + return { + content: [ + { + type: "text", + text: String(a + b), + }, + ], + }; + } + case "multiply": { + const { a, b } = request.params.arguments as { a: number; b: number }; + return { + content: [ + { + type: "text", + text: String(a * b), + }, + ], + }; + } + default: + throw new Error(`Unknown tool: ${request.params.name}`); + } +}); + +async function main() { + const transport = new StdioServerTransport(); + await server.connect(transport); + console.error("Math MCP server running on stdio"); +} + +main(); +``` + +::: + +:::python + ```python title="Example Weather Server (Streamable HTTP transport)" from mcp.server.fastmcp import FastMCP @@ -171,8 +418,100 @@ if __name__ == "__main__": mcp.run(transport="streamable-http") ``` +::: + +:::js + +```typescript title="Example Weather Server (HTTP transport)" +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; +import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"; +import { + CallToolRequestSchema, + ListToolsRequestSchema, +} from "@modelcontextprotocol/sdk/types.js"; +import express from "express"; + +const app = express(); +app.use(express.json()); + +const server = new Server( + { + name: "weather-server", + version: "0.1.0", + }, + { + capabilities: { + tools: {}, + }, + } +); + +server.setRequestHandler(ListToolsRequestSchema, async () => { + return { + tools: [ + { + name: "get_weather", + description: "Get weather for location", + inputSchema: { + type: "object", + properties: { + location: { + type: "string", + description: "Location to get weather for", + }, + }, + required: ["location"], + }, + }, + ], + }; +}); + +server.setRequestHandler(CallToolRequestSchema, async (request) => { + switch (request.params.name) { + case "get_weather": { + const { location } = request.params.arguments as { location: string }; + return { + content: [ + { + type: "text", + text: `It's always sunny in ${location}`, + }, + ], + }; + } + default: + throw new Error(`Unknown tool: ${request.params.name}`); + } +}); + +app.post("/mcp", async (req, res) => { + const transport = new SSEServerTransport("/mcp", res); + await server.connect(transport); +}); + +const PORT = process.env.PORT || 8000; +app.listen(PORT, () => { + console.log(`Weather MCP server running on port ${PORT}`); +}); +``` + +::: + +:::python + ## Additional resources - [MCP documentation](https://modelcontextprotocol.io/introduction) - [MCP Transport documentation](https://modelcontextprotocol.io/docs/concepts/transports) - [langchain_mcp_adapters](https://github.com/langchain-ai/langchain-mcp-adapters) + ::: + +:::js + +## Additional resources + +- [MCP documentation](https://modelcontextprotocol.io/introduction) +- [MCP Transport documentation](https://modelcontextprotocol.io/docs/concepts/transports) +- [`@langchain/mcp-adapters`](https://npmjs.com/package/@langchain/mcp-adapters) + ::: diff --git a/docs/docs/agents/models.md b/docs/docs/agents/models.md index 46db9c41d..5657d24d0 100644 --- a/docs/docs/agents/models.md +++ b/docs/docs/agents/models.md @@ -2,18 +2,70 @@ LangGraph provides built-in support for [LLMs (language models)](https://python.langchain.com/docs/concepts/chat_models/) via the LangChain library. This makes it easy to integrate various LLMs into your agents and workflows. - ## Initialize a model +:::python Use [`init_chat_model`](https://python.langchain.com/docs/how_to/chat_models_universal_init/) to initialize models: {% include-markdown "../../snippets/chat_model_tabs.md" %} +::: + +:::js +Use model provider classes to initialize models: + +=== "OpenAI" + + ```typescript + import { ChatOpenAI } from "@langchain/openai"; + + const model = new ChatOpenAI({ + model: "gpt-4o", + temperature: 0, + }); + ``` + +=== "Anthropic" + + ```typescript + import { ChatAnthropic } from "@langchain/anthropic"; + + const model = new ChatAnthropic({ + model: "claude-3-5-sonnet-20240620", + temperature: 0, + maxTokens: 2048, + }); + ``` + +=== "Google" + + ```typescript + import { ChatGoogleGenerativeAI } from "@langchain/google-genai"; + + const model = new ChatGoogleGenerativeAI({ + model: "gemini-1.5-pro", + temperature: 0, + }); + ``` + +=== "Groq" + + ```typescript + import { ChatGroq } from "@langchain/groq"; + + const model = new ChatGroq({ + model: "llama-3.1-70b-versatile", + temperature: 0, + }); + ``` + +::: + +:::python ### Instantiate a model directly If a model provider is not available via `init_chat_model`, you can instantiate the provider's model class directly. The model must implement the [BaseChatModel interface](https://python.langchain.com/api_reference/core/language_models/langchain_core.language_models.chat_models.BaseChatModel.html) and support tool calling: - ```python # Anthropic is already supported by `init_chat_model`, # but you can also instantiate it directly. @@ -26,19 +78,20 @@ model = ChatAnthropic( ) ``` +::: + !!! important "Tool calling support" If you are building an agent or workflow that requires the model to call external tools, ensure that the underlying language model supports [tool calling](../concepts/tools.md). Compatible models can be found in the [LangChain integrations directory](https://python.langchain.com/docs/integrations/chat/). - ## Use in an agent +:::python When using `create_react_agent` you can specify the model by its name string, which is a shorthand for initializing the model using `init_chat_model`. This allows you to use the model without needing to import or instantiate it directly. === "model name" - ```python from langgraph.prebuilt import create_react_agent @@ -70,10 +123,33 @@ When using `create_react_agent` you can specify the model by its name string, wh ) ``` +::: + +:::js +When using `createReactAgent` you can pass the model instance directly: + +```typescript +import { ChatOpenAI } from "@langchain/openai"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const model = new ChatOpenAI({ + model: "gpt-4o", + temperature: 0, +}); + +const agent = createReactAgent({ + llm: model, + tools: tools, +}); +``` + +::: + ## Advanced model configuration ### Disable streaming +:::python To disable streaming of the individual LLM tokens, set `disable_streaming=True` when initializing the model: === "`init_chat_model`" @@ -101,9 +177,25 @@ To disable streaming of the individual LLM tokens, set `disable_streaming=True` ``` Refer to the [API reference](https://python.langchain.com/api_reference/core/language_models/langchain_core.language_models.chat_models.BaseChatModel.html#langchain_core.language_models.chat_models.BaseChatModel.disable_streaming) for more information on `disable_streaming` +::: + +:::js +To disable streaming of the individual LLM tokens, set `streaming: false` when initializing the model: + +```typescript +import { ChatOpenAI } from "@langchain/openai"; + +const model = new ChatOpenAI({ + model: "gpt-4o", + streaming: false, +}); +``` + +::: ### Add model fallbacks +:::python You can add a fallback to a different model or a different LLM provider using `model.with_fallbacks([...])`: === "`init_chat_model`" @@ -136,6 +228,28 @@ You can add a fallback to a different model or a different LLM provider using `m ``` See this [guide](https://python.langchain.com/docs/how_to/fallbacks/#fallback-to-better-model) for more information on model fallbacks. +::: + +:::js +You can add a fallback to a different model or a different LLM provider using `model.withFallbacks([...])`: + +```typescript +import { ChatOpenAI } from "@langchain/openai"; +import { ChatAnthropic } from "@langchain/anthropic"; + +const modelWithFallbacks = new ChatOpenAI({ + model: "gpt-4o", +}).withFallbacks([ + new ChatAnthropic({ + model: "claude-3-5-sonnet-20240620", + }), +]); +``` + +See this [guide](https://js.langchain.com/docs/how_to/fallbacks/#fallback-to-better-model) for more information on model fallbacks. +::: + +:::python ### Use the built-in rate limiter @@ -152,28 +266,49 @@ rate_limiter = InMemoryRateLimiter( ) model = ChatAnthropic( - model_name="claude-3-opus-20240229", + model_name="claude-3-opus-20240229", rate_limiter=rate_limiter ) ``` See the LangChain docs for more information on how to [handle rate limiting](https://python.langchain.com/docs/how_to/chat_model_rate_limiting/). +::: ## Bring your own model If your desired LLM isn't officially supported by LangChain, consider these options: +:::python + 1. **Implement a custom LangChain chat model**: Create a model conforming to the [LangChain chat model interface](https://python.langchain.com/docs/how_to/custom_chat_model/). This enables full compatibility with LangGraph's agents and workflows but requires understanding of the LangChain framework. + ::: + +:::js + +1. **Implement a custom LangChain chat model**: Create a model conforming to the [LangChain chat model interface](https://js.langchain.com/docs/how_to/custom_chat/). This enables full compatibility with LangGraph's agents and workflows but requires understanding of the LangChain framework. + ::: 2. **Direct invocation with custom streaming**: Use your model directly by [adding custom streaming logic](../how-tos/streaming.md#use-with-any-llm) with `StreamWriter`. Refer to the [custom streaming documentation](../how-tos/streaming.md#use-with-any-llm) for guidance. This approach suits custom workflows where prebuilt agent integration is not necessary. - ## Additional resources +:::python + - [Multimodal inputs](https://python.langchain.com/docs/how_to/multimodal_inputs/) - [Structured outputs](https://python.langchain.com/docs/how_to/structured_output/) - [Model integration directory](https://python.langchain.com/docs/integrations/chat/) - [Force model to call a specific tool](https://python.langchain.com/docs/how_to/tool_choice/) - [All chat model how-to guides](https://python.langchain.com/docs/how_to/#chat-models) - [Chat model integrations](https://python.langchain.com/docs/integrations/chat/) + ::: + +:::js + +- [Multimodal inputs](https://js.langchain.com/docs/how_to/multimodal_inputs/) +- [Structured outputs](https://js.langchain.com/docs/how_to/structured_output/) +- [Model integration directory](https://js.langchain.com/docs/integrations/chat/) +- [Force model to call a specific tool](https://js.langchain.com/docs/how_to/tool_choice/) +- [All chat model how-to guides](https://js.langchain.com/docs/how_to/#chat-models) +- [Chat model integrations](https://js.langchain.com/docs/integrations/chat/) + ::: diff --git a/docs/docs/agents/multi-agent.md b/docs/docs/agents/multi-agent.md index 54ba13c9c..b378141cc 100644 --- a/docs/docs/agents/multi-agent.md +++ b/docs/docs/agents/multi-agent.md @@ -22,6 +22,7 @@ Two of the most popular multi-agent architectures are: ![Supervisor](./assets/supervisor.png) +:::python Use [`langgraph-supervisor`](https://github.com/langchain-ai/langgraph-supervisor-py) library to create a supervisor multi-agent system: ```bash @@ -82,10 +83,76 @@ for chunk in supervisor.stream( print("\n") ``` +::: + +:::js +Use [`@langchain/langgraph-supervisor`](https://github.com/langchain-ai/langgraphjs/tree/main/libs/langgraph-supervisor) library to create a supervisor multi-agent system: + +```bash +npm install @langchain/langgraph-supervisor +``` + +```typescript +import { ChatOpenAI } from "@langchain/openai"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +// highlight-next-line +import { createSupervisor } from "langgraph-supervisor"; + +function bookHotel(hotelName: string) { + /**Book a hotel*/ + return `Successfully booked a stay at ${hotelName}.`; +} + +function bookFlight(fromAirport: string, toAirport: string) { + /**Book a flight*/ + return `Successfully booked a flight from ${fromAirport} to ${toAirport}.`; +} + +const flightAssistant = createReactAgent({ + llm: "openai:gpt-4o", + tools: [bookFlight], + stateModifier: "You are a flight booking assistant", + // highlight-next-line + name: "flight_assistant", +}); + +const hotelAssistant = createReactAgent({ + llm: "openai:gpt-4o", + tools: [bookHotel], + stateModifier: "You are a hotel booking assistant", + // highlight-next-line + name: "hotel_assistant", +}); + +// highlight-next-line +const supervisor = createSupervisor({ + agents: [flightAssistant, hotelAssistant], + llm: new ChatOpenAI({ model: "gpt-4o" }), + systemPrompt: + "You manage a hotel booking assistant and a " + + "flight booking assistant. Assign work to them.", +}); + +for await (const chunk of supervisor.stream({ + messages: [ + { + role: "user", + content: "book a flight from BOS to JFK and a stay at McKittrick Hotel", + }, + ], +})) { + console.log(chunk); + console.log("\n"); +} +``` + +::: + ## Swarm ![Swarm](./assets/swarm.png) +:::python Use [`langgraph-swarm`](https://github.com/langchain-ai/langgraph-swarm-py) library to create a swarm multi-agent system: ```bash @@ -143,18 +210,82 @@ for chunk in swarm.stream( print("\n") ``` +::: + +:::js +Use [`@langchain/langgraph-swarm`](https://github.com/langchain-ai/langgraphjs/tree/main/libs/langgraph-swarm) library to create a swarm multi-agent system: + +```bash +npm install @langchain/langgraph-swarm +``` + +```typescript +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +// highlight-next-line +import { createSwarm, createHandoffTool } from "@langchain/langgraph-swarm"; + +const transferToHotelAssistant = createHandoffTool({ + agentName: "hotel_assistant", + description: "Transfer user to the hotel-booking assistant.", +}); + +const transferToFlightAssistant = createHandoffTool({ + agentName: "flight_assistant", + description: "Transfer user to the flight-booking assistant.", +}); + +const flightAssistant = createReactAgent({ + llm: "anthropic:claude-3-5-sonnet-latest", + // highlight-next-line + tools: [bookFlight, transferToHotelAssistant], + stateModifier: "You are a flight booking assistant", + // highlight-next-line + name: "flight_assistant", +}); + +const hotelAssistant = createReactAgent({ + llm: "anthropic:claude-3-5-sonnet-latest", + // highlight-next-line + tools: [bookHotel, transferToFlightAssistant], + stateModifier: "You are a hotel booking assistant", + // highlight-next-line + name: "hotel_assistant", +}); + +// highlight-next-line +const swarm = createSwarm({ + agents: [flightAssistant, hotelAssistant], + defaultActiveAgent: "flight_assistant", +}); + +for await (const chunk of swarm.stream({ + messages: [ + { + role: "user", + content: "book a flight from BOS to JFK and a stay at McKittrick Hotel", + }, + ], +})) { + console.log(chunk); + console.log("\n"); +} +``` + +::: + ## Handoffs -A common pattern in multi-agent interactions is **handoffs**, where one agent *hands off* control to another. Handoffs allow you to specify: +A common pattern in multi-agent interactions is **handoffs**, where one agent _hands off_ control to another. Handoffs allow you to specify: - **destination**: target agent to navigate to - **payload**: information to pass to that agent +:::python This is used both by `langgraph-supervisor` (supervisor hands off to individual agents) and `langgraph-swarm` (an individual agent can hand off to other agents). To implement handoffs with `create_react_agent`, you need to: -1. Create a special tool that can transfer control to a different agent +1. Create a special tool that can transfer control to a different agent ```python def transfer_to_bob(): @@ -173,7 +304,7 @@ To implement handoffs with `create_react_agent`, you need to: ) ``` -1. Create individual agents that have access to handoff tools: +2. Create individual agents that have access to handoff tools: ```python flight_assistant = create_react_agent( @@ -184,7 +315,7 @@ To implement handoffs with `create_react_agent`, you need to: ) ``` -1. Define a parent graph that contains individual agents as nodes: +3. Define a parent graph that contains individual agents as nodes: ```python from langgraph.graph import StateGraph, MessagesState @@ -196,8 +327,60 @@ To implement handoffs with `create_react_agent`, you need to: ) ``` +::: + +:::js +This is used both by `@langchain/langgraph-supervisor` (supervisor hands off to individual agents) and `@langchain/langgraph-swarm` (an individual agent can hand off to other agents). + +To implement handoffs with `createReactAgent`, you need to: + +1. Create a special tool that can transfer control to a different agent + + ```typescript + function transferToBob() { + /**Transfer to bob.*/ + return new Command({ + // name of the agent (node) to go to + // highlight-next-line + goto: "bob", + // data to send to the agent + // highlight-next-line + update: { messages: [...] }, + // indicate to LangGraph that we need to navigate to + // agent node in a parent graph + // highlight-next-line + graph: Command.PARENT, + }); + } + ``` + +2. Create individual agents that have access to handoff tools: + + ```typescript + const flightAssistant = createReactAgent({ + ..., tools: [bookFlight, transferToHotelAssistant] + }); + const hotelAssistant = createReactAgent({ + ..., tools: [bookHotel, transferToFlightAssistant] + }); + ``` + +3. Define a parent graph that contains individual agents as nodes: + + ```typescript + import { StateGraph, MessagesZodState } from "@langchain/langgraph"; + const multiAgentGraph = new StateGraph(MessagesZodState) + .addNode("flight_assistant", flightAssistant) + .addNode("hotel_assistant", hotelAssistant) + // ... + ``` + + ::: + Putting this together, here is how you can implement a simple multi-agent system with two agents — a flight booking assistant and a hotel booking assistant: +:::python + ```python from typing import Annotated from langchain_core.tools import tool, InjectedToolCallId @@ -298,11 +481,157 @@ for chunk in multi_agent_graph.stream( 3. Name of the agent or node to hand off to. 4. Take the agent's messages and **add** them to the parent's **state** as part of the handoff. The next agent will see the parent state. 5. Indicate to LangGraph that we need to navigate to agent node in a **parent** multi-agent graph. + ::: + +:::js + +```typescript +import { tool } from "@langchain/core/tools"; +import { ChatAnthropic } from "@langchain/anthropic"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { + StateGraph, + START, + MessagesZodState, + Command, +} from "@langchain/langgraph"; +import { z } from "zod"; + +function createHandoffTool({ + agentName, + description, +}: { + agentName: string; + description?: string; +}) { + const name = `transfer_to_${agentName}`; + const toolDescription = description || `Transfer to ${agentName}`; + + return tool( + async (_, config) => { + const toolMessage = { + role: "tool" as const, + content: `Successfully transferred to ${agentName}`, + name: name, + tool_call_id: config.toolCall?.id!, + }; + return new Command({ + // (2)! + // highlight-next-line + goto: agentName, // (3)! + // highlight-next-line + update: { messages: [toolMessage] }, // (4)! + // highlight-next-line + graph: Command.PARENT, // (5)! + }); + }, + { + name, + description: toolDescription, + schema: z.object({}), + } + ); +} + +// Handoffs +const transferToHotelAssistant = createHandoffTool({ + agentName: "hotel_assistant", + description: "Transfer user to the hotel-booking assistant.", +}); + +const transferToFlightAssistant = createHandoffTool({ + agentName: "flight_assistant", + description: "Transfer user to the flight-booking assistant.", +}); + +// Simple agent tools +const bookHotel = tool( + async ({ hotelName }) => { + /**Book a hotel*/ + return `Successfully booked a stay at ${hotelName}.`; + }, + { + name: "book_hotel", + description: "Book a hotel", + schema: z.object({ + hotelName: z.string().describe("Name of the hotel to book"), + }), + } +); + +const bookFlight = tool( + async ({ fromAirport, toAirport }) => { + /**Book a flight*/ + return `Successfully booked a flight from ${fromAirport} to ${toAirport}.`; + }, + { + name: "book_flight", + description: "Book a flight", + schema: z.object({ + fromAirport: z.string().describe("Departure airport code"), + toAirport: z.string().describe("Arrival airport code"), + }), + } +); + +// Define agents +const flightAssistant = createReactAgent({ + llm: new ChatAnthropic({ model: "anthropic:claude-3-5-sonnet-latest" }), + // highlight-next-line + tools: [bookFlight, transferToHotelAssistant], + stateModifier: "You are a flight booking assistant", + // highlight-next-line + name: "flight_assistant", +}); + +const hotelAssistant = createReactAgent({ + llm: new ChatAnthropic({ model: "anthropic:claude-3-5-sonnet-latest" }), + // highlight-next-line + tools: [bookHotel, transferToFlightAssistant], + stateModifier: "You are a hotel booking assistant", + // highlight-next-line + name: "hotel_assistant", +}); + +// Define multi-agent graph +const multiAgentGraph = new StateGraph(MessagesZodState) + .addNode("flight_assistant", flightAssistant) + .addNode("hotel_assistant", hotelAssistant) + .addEdge(START, "flight_assistant") + .compile(); + +// Run the multi-agent graph +for await (const chunk of multiAgentGraph.stream({ + messages: [ + { + role: "user", + content: "book a flight from BOS to JFK and a stay at McKittrick Hotel", + }, + ], +})) { + console.log(chunk); + console.log("\n"); +} +``` + +1. Access agent's state +2. The `Command` primitive allows specifying a state update and a node transition as a single operation, making it useful for implementing handoffs. +3. Name of the agent or node to hand off to. +4. Take the agent's messages and **add** them to the parent's **state** as part of the handoff. The next agent will see the parent state. +5. Indicate to LangGraph that we need to navigate to agent node in a **parent** multi-agent graph. + ::: !!! Note + This handoff implementation assumes that: - each agent receives overall message history (across all agents) in the multi-agent system as its input - each agent outputs its internal messages history to the overall message history of the multi-agent system - Check out LangGraph [supervisor](https://github.com/langchain-ai/langgraph-supervisor-py#customizing-handoff-tools) and [swarm](https://github.com/langchain-ai/langgraph-swarm-py#customizing-handoff-tools) documentation to learn how to customize handoffs. \ No newline at end of file +:::python +Check out LangGraph [supervisor](https://github.com/langchain-ai/langgraph-supervisor-py#customizing-handoff-tools) and [swarm](https://github.com/langchain-ai/langgraph-swarm-py#customizing-handoff-tools) documentation to learn how to customize handoffs. +::: + +:::js +Check out LangGraph [supervisor](https://github.com/langchain-ai/langgraphjs/tree/main/libs/langgraph-supervisor#customizing-handoff-tools) and [swarm](https://github.com/langchain-ai/langgraphjs/tree/main/libs/langgraph-swarm#customizing-handoff-tools) documentation to learn how to customize handoffs. +::: diff --git a/docs/docs/agents/overview.md b/docs/docs/agents/overview.md index 96fef74b3..00601cdd4 100644 --- a/docs/docs/agents/overview.md +++ b/docs/docs/agents/overview.md @@ -14,7 +14,7 @@ LangGraph provides both low-level primitives and high-level prebuilt components ## What is an agent? -An *agent* consists of three components: a **large language model (LLM)**, a set of **tools** it can use, and a **prompt** that provides instructions. +An _agent_ consists of three components: a **large language model (LLM)**, a set of **tools** it can use, and a **prompt** that provides instructions. The LLM operates in a loop. In each iteration, it selects a tool to invoke, provides input, receives the result (an observation), and uses that observation to inform the next action. The loop continues until a stopping condition is met — typically when the agent has gathered enough information to respond to the user. @@ -27,12 +27,12 @@ The LLM operates in a loop. In each iteration, it selects a tool to invoke, prov LangGraph includes several capabilities essential for building robust, production-ready agentic systems: -- [**Memory integration**](../how-tos/memory/add-memory.md): Native support for *short-term* (session-based) and *long-term* (persistent across sessions) memory, enabling stateful behaviors in chatbots and assistants. -- [**Human-in-the-loop control**](../concepts/human_in_the_loop.md): Execution can pause *indefinitely* to await human feedback—unlike websocket-based solutions limited to real-time interaction. This enables asynchronous approval, correction, or intervention at any point in the workflow. +- [**Memory integration**](../how-tos/memory/add-memory.md): Native support for _short-term_ (session-based) and _long-term_ (persistent across sessions) memory, enabling stateful behaviors in chatbots and assistants. +- [**Human-in-the-loop control**](../concepts/human_in_the_loop.md): Execution can pause _indefinitely_ to await human feedback—unlike websocket-based solutions limited to real-time interaction. This enables asynchronous approval, correction, or intervention at any point in the workflow. - [**Streaming support**](../how-tos/streaming.md): Real-time streaming of agent state, model tokens, tool outputs, or combined streams. - [**Deployment tooling**](../tutorials/langgraph-platform/local-server.md): Includes infrastructure-free deployment tools. [**LangGraph Platform**](https://langchain-ai.github.io/langgraph/concepts/langgraph_platform/) supports testing, debugging, and deployment. - - **[Studio](https://langchain-ai.github.io/langgraph/concepts/langgraph_studio/)**: A visual IDE for inspecting and debugging workflows. - - Supports multiple [**deployment options**](https://langchain-ai.github.io/langgraph/concepts/deployment_options.md) for production. + - **[Studio](https://langchain-ai.github.io/langgraph/concepts/langgraph_studio/)**: A visual IDE for inspecting and debugging workflows. + - Supports multiple [**deployment options**](https://langchain-ai.github.io/langgraph/concepts/deployment_options.md) for production. ## High-level building blocks @@ -40,30 +40,32 @@ LangGraph comes with a set of prebuilt components that implement common agent be Using LangGraph for agent development allows you to focus on your application's logic and behavior, instead of building and maintaining the supporting infrastructure for state, memory, and human feedback. +:::python + ## Package ecosystem The high-level components are organized into several packages, each with a specific focus. -| Package | Description | Installation | -|--------------------------------------------|-----------------------------------------------------------------------------|-----------------------------------------| -| `langgraph-prebuilt` (part of `langgraph`) | Prebuilt components to [**create agents**](./agents.md) | `pip install -U langgraph langchain` | -| `langgraph-supervisor` | Tools for building [**supervisor**](./multi-agent.md#supervisor) agents | `pip install -U langgraph-supervisor` | -| `langgraph-swarm` | Tools for building a [**swarm**](./multi-agent.md#swarm) multi-agent system | `pip install -U langgraph-swarm` | -| `langchain-mcp-adapters` | Interfaces to [**MCP servers**](./mcp.md) for tool and resource integration | `pip install -U langchain-mcp-adapters` | -| `langmem` | Agent memory management: [**short-term and long-term**](../how-tos/memory/add-memory.md) | `pip install -U langmem` | -| `agentevals` | Utilities to [**evaluate agent performance**](./evals.md) | `pip install -U agentevals` | +| Package | Description | Installation | +| ------------------------------------------ | ---------------------------------------------------------------------------------------- | --------------------------------------- | +| `langgraph-prebuilt` (part of `langgraph`) | Prebuilt components to [**create agents**](./agents.md) | `pip install -U langgraph langchain` | +| `langgraph-supervisor` | Tools for building [**supervisor**](./multi-agent.md#supervisor) agents | `pip install -U langgraph-supervisor` | +| `langgraph-swarm` | Tools for building a [**swarm**](./multi-agent.md#swarm) multi-agent system | `pip install -U langgraph-swarm` | +| `langchain-mcp-adapters` | Interfaces to [**MCP servers**](./mcp.md) for tool and resource integration | `pip install -U langchain-mcp-adapters` | +| `langmem` | Agent memory management: [**short-term and long-term**](../how-tos/memory/add-memory.md) | `pip install -U langmem` | +| `agentevals` | Utilities to [**evaluate agent performance**](./evals.md) | `pip install -U agentevals` | ## Visualize an agent graph Use the following tool to visualize the graph generated by -[`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] +@[`create_react_agent`][create_react_agent] and to view an outline of the corresponding code. It allows you to explore the infrastructure of the agent as defined by the presence of: -* [`tools`](../how-tos/tool-calling.md): A list of tools (functions, APIs, or other callable objects) that the agent can use to perform tasks. -* [`pre_model_hook`](../how-tos/create-react-agent-manage-message-history.ipynb): A function that is called before the model is invoked. It can be used to condense messages or perform other preprocessing tasks. -* `post_model_hook`: A function that is called after the model is invoked. It can be used to implement guardrails, human-in-the-loop flows, or other postprocessing tasks. -* [`response_format`](../agents/agents.md#6-configure-structured-output): A data structure used to constrain the type of the final output, e.g., a `pydantic` `BaseModel`. +- [`tools`](../how-tos/tool-calling.md): A list of tools (functions, APIs, or other callable objects) that the agent can use to perform tasks. +- [`pre_model_hook`](../how-tos/create-react-agent-manage-message-history.ipynb): A function that is called before the model is invoked. It can be used to condense messages or perform other preprocessing tasks. +- `post_model_hook`: A function that is called after the model is invoked. It can be used to implement guardrails, human-in-the-loop flows, or other postprocessing tasks. +- [`response_format`](../agents/agents.md#6-configure-structured-output): A data structure used to constrain the type of the final output, e.g., a `pydantic` `BaseModel`. <div class="agent-layout"> <div class="agent-graph-features-container"> @@ -82,15 +84,13 @@ It allows you to explore the infrastructure of the agent as defined by the prese </div> </div> - The following code snippet shows how to create the above agent (and underlying graph) with -[`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent]: +@[`create_react_agent`][create_react_agent]: <div class="language-python"> <pre><code id="agent-code" class="language-python"></code></pre> </div> - <script> function getCheckedValue(id) { return document.getElementById(id).checked ? "1" : "0"; @@ -189,3 +189,159 @@ function initializeWidget() { window.addEventListener("DOMContentLoaded", initializeWidget); document$.subscribe(initializeWidget); </script> + +::: + +:::js + +## Package ecosystem + +The high-level components are organized into several packages, each with a specific focus. + +| Package | Description | Installation | +| ------------------------ | --------------------------------------------------------------------------- | -------------------------------------------------- | +| `langgraph` | Prebuilt components to [**create agents**](./agents.md) | `npm install @langchain/langgraph @langchain/core` | +| `langgraph-supervisor` | Tools for building [**supervisor**](./multi-agent.md#supervisor) agents | `npm install @langchain/langgraph-supervisor` | +| `langgraph-swarm` | Tools for building a [**swarm**](./multi-agent.md#swarm) multi-agent system | `npm install @langchain/langgraph-swarm` | +| `langchain-mcp-adapters` | Interfaces to [**MCP servers**](./mcp.md) for tool and resource integration | `npm install @langchain/mcp-adapters` | +| `agentevals` | Utilities to [**evaluate agent performance**](./evals.md) | `npm install agentevals` | + +## Visualize an agent graph + +Use the following tool to visualize the graph generated by [`createReactAgent`](/langgraphjs/reference/functions/langgraph_prebuilt.createReactAgent.html) and to view an outline of the corresponding code. It allows you to explore the infrastructure of the agent as defined by the presence of: + +- [`tools`](./tools.md): A list of tools (functions, APIs, or other callable objects) that the agent can use to perform tasks. +- `preModelHook`: A function that is called before the model is invoked. It can be used to condense messages or perform other preprocessing tasks. +- `postModelHook`: A function that is called after the model is invoked. It can be used to implement guardrails, human-in-the-loop flows, or other postprocessing tasks. +- [`responseFormat`](./agents.md#structured-output): A data structure used to constrain the type of the final output (via Zod schemas). + +<div class="agent-layout"> + <div class="agent-graph-features-container"> + <div class="agent-graph-features"> + <h3 class="agent-section-title">Features</h3> + <label><input type="checkbox" id="tools" checked> <code>tools</code></label> + <label><input type="checkbox" id="preModelHook"> <code>preModelHook</code></label> + <label><input type="checkbox" id="postModelHook"> <code>postModelHook</code></label> + <label><input type="checkbox" id="responseFormat"> <code>responseFormat</code></label> + </div> + </div> + + <div class="agent-graph-container"> + <h3 class="agent-section-title">Graph</h3> + <img id="agent-graph-img" src="../assets/react_agent_graphs/0001.svg" alt="graph image" style="max-width: 100%;"/> + </div> +</div> + +The following code snippet shows how to create the above agent (and underlying graph) with [`createReactAgent`](/langgraphjs/reference/functions/langgraph_prebuilt.createReactAgent.html): + +<div class="language-typescript"> + <pre><code id="agent-code" class="language-typescript"></code></pre> +</div> + +<script> +function getCheckedValue(id) { + return document.getElementById(id).checked ? "1" : "0"; +} + +function getKey() { + return [ + getCheckedValue("responseFormat"), + getCheckedValue("postModelHook"), + getCheckedValue("preModelHook"), + getCheckedValue("tools") + ].join(""); +} + +function dedent(strings, ...values) { + const str = String.raw({ raw: strings }, ...values) + const [space] = str.split("\n").filter(Boolean).at(0).match(/^(\s*)/) + const spaceLen = space.length + return str.split("\n").map(line => line.slice(spaceLen)).join("\n").trim() +} + +Object.assign(dedent, { + offset: (size) => (strings, ...values) => { + return dedent(strings, ...values).split("\n").map(line => " ".repeat(size) + line).join("\n") + } +}) + + + + +function generateCodeSnippet({ tools, pre, post, response }) { + const lines = [] + + lines.push(dedent` + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import { ChatOpenAI } from "@langchain/openai"; + `) + + if (tools) lines.push(`import { tool } from "@langchain/core/tools";`); + if (response || tools) lines.push(`import { z } from "zod";`); + + lines.push("", dedent` + const agent = createReactAgent({ + llm: new ChatOpenAI({ model: "o4-mini" }), + `) + + if (tools) { + lines.push(dedent.offset(2)` + tools: [ + tool(() => "Sample tool output", { + name: "sampleTool", + schema: z.object({}), + }), + ], + `) + } + + if (pre) { + lines.push(dedent.offset(2)` + preModelHook: (state) => ({ llmInputMessages: state.messages }), + `) + } + + if (post) { + lines.push(dedent.offset(2)` + postModelHook: (state) => state, + `) + } + + if (response) { + lines.push(dedent.offset(2)` + responseFormat: z.object({ result: z.string() }), + `) + } + + lines.push(`});`); + + return lines.join("\n"); +} + +function render() { + const key = getKey(); + document.getElementById("agent-graph-img").src = `../assets/react_agent_graphs/${key}.svg`; + + const state = { + tools: document.getElementById("tools").checked, + pre: document.getElementById("preModelHook").checked, + post: document.getElementById("postModelHook").checked, + response: document.getElementById("responseFormat").checked + }; + + document.getElementById("agent-code").textContent = generateCodeSnippet(state); +} + +function initializeWidget() { + render(); // no need for `await` here + document.querySelectorAll(".agent-graph-features input").forEach((input) => { + input.addEventListener("change", render); + }); +} + +// Init for both full reload and SPA nav (used by MkDocs Material) +window.addEventListener("DOMContentLoaded", initializeWidget); +document$.subscribe(initializeWidget); +</script> + +::: diff --git a/docs/docs/agents/prebuilt.md b/docs/docs/agents/prebuilt.md index 8ebd5c17b..38c609105 100644 --- a/docs/docs/agents/prebuilt.md +++ b/docs/docs/agents/prebuilt.md @@ -5,23 +5,24 @@ If you’re looking for other prebuilt libraries, explore the community-built op below. These libraries can extend LangGraph's functionality in various ways. ## 📚 Available Libraries - [//]: # (This file is automatically generated using a script in docs/_scripts. Do not edit this file directly!) + +:::python | Name | GitHub URL | Description | Weekly Downloads | Stars | | --- | --- | --- | --- | --- | -| **trustcall** | [hinthornw/trustcall](https://github.com/hinthornw/trustcall) | Tenacious tool calling built on LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/hinthornw/trustcall?style=social) -| **breeze-agent** | [andrestorres123/breeze-agent](https://github.com/andrestorres123/breeze-agent) | A streamlined research system built inspired on STORM and built on LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/andrestorres123/breeze-agent?style=social) -| **langgraph-supervisor** | [langchain-ai/langgraph-supervisor-py](https://github.com/langchain-ai/langgraph-supervisor-py) | Build supervisor multi-agent systems with LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-supervisor-py?style=social) -| **langmem** | [langchain-ai/langmem](https://github.com/langchain-ai/langmem) | Build agents that learn and adapt from interactions over time. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langmem?style=social) -| **langchain-mcp-adapters** | [langchain-ai/langchain-mcp-adapters](https://github.com/langchain-ai/langchain-mcp-adapters) | Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph agents. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langchain-mcp-adapters?style=social) -| **open-deep-research** | [langchain-ai/open_deep_research](https://github.com/langchain-ai/open_deep_research) | Open source assistant for iterative web research and report writing. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/open_deep_research?style=social) -| **langgraph-swarm** | [langchain-ai/langgraph-swarm-py](https://github.com/langchain-ai/langgraph-swarm-py) | Build swarm-style multi-agent systems using LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-swarm-py?style=social) -| **delve-taxonomy-generator** | [andrestorres123/delve](https://github.com/andrestorres123/delve) | A taxonomy generator for unstructured data | -12345 | ![GitHub stars](https://img.shields.io/github/stars/andrestorres123/delve?style=social) -| **nodeology** | [xyin-anl/Nodeology](https://github.com/xyin-anl/Nodeology) | Enable researcher to build scientific workflows easily with simplified interface. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/xyin-anl/Nodeology?style=social) -| **langgraph-bigtool** | [langchain-ai/langgraph-bigtool](https://github.com/langchain-ai/langgraph-bigtool) | Build LangGraph agents with large numbers of tools. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-bigtool?style=social) -| **ai-data-science-team** | [business-science/ai-data-science-team](https://github.com/business-science/ai-data-science-team) | An AI-powered data science team of agents to help you perform common data science tasks 10X faster. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/business-science/ai-data-science-team?style=social) -| **langgraph-reflection** | [langchain-ai/langgraph-reflection](https://github.com/langchain-ai/langgraph-reflection) | LangGraph agent that runs a reflection step. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-reflection?style=social) -| **langgraph-codeact** | [langchain-ai/langgraph-codeact](https://github.com/langchain-ai/langgraph-codeact) | LangGraph implementation of CodeAct agent that generates and executes code instead of tool calling. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-codeact?style=social) +| **trustcall** | https://github.com/hinthornw/trustcall | Tenacious tool calling built on LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/hinthornw/trustcall?style=social) +| **breeze-agent** | https://github.com/andrestorres123/breeze-agent | A streamlined research system built inspired on STORM and built on LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/andrestorres123/breeze-agent?style=social) +| **langgraph-supervisor** | https://github.com/langchain-ai/langgraph-supervisor-py | Build supervisor multi-agent systems with LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-supervisor-py?style=social) +| **langmem** | https://github.com/langchain-ai/langmem | Build agents that learn and adapt from interactions over time. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langmem?style=social) +| **langchain-mcp-adapters** | https://github.com/langchain-ai/langchain-mcp-adapters | Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph agents. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langchain-mcp-adapters?style=social) +| **open-deep-research** | https://github.com/langchain-ai/open_deep_research | Open source assistant for iterative web research and report writing. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/open_deep_research?style=social) +| **langgraph-swarm** | https://github.com/langchain-ai/langgraph-swarm-py | Build swarm-style multi-agent systems using LangGraph. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-swarm-py?style=social) +| **delve-taxonomy-generator** | https://github.com/andrestorres123/delve | A taxonomy generator for unstructured data | -12345 | ![GitHub stars](https://img.shields.io/github/stars/andrestorres123/delve?style=social) +| **nodeology** | https://github.com/xyin-anl/Nodeology | Enable researcher to build scientific workflows easily with simplified interface. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/xyin-anl/Nodeology?style=social) +| **langgraph-bigtool** | https://github.com/langchain-ai/langgraph-bigtool | Build LangGraph agents with large numbers of tools. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-bigtool?style=social) +| **ai-data-science-team** | https://github.com/business-science/ai-data-science-team | An AI-powered data science team of agents to help you perform common data science tasks 10X faster. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/business-science/ai-data-science-team?style=social) +| **langgraph-reflection** | https://github.com/langchain-ai/langgraph-reflection | LangGraph agent that runs a reflection step. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-reflection?style=social) +| **langgraph-codeact** | https://github.com/langchain-ai/langgraph-codeact | LangGraph implementation of CodeAct agent that generates and executes code instead of tool calling. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraph-codeact?style=social) ## ✨ Contributing Your Library @@ -32,13 +33,41 @@ To share your project, simply open a Pull Request adding an entry for your packa **Guidelines** -- Your repo must be distributed as an installable package (e.g., PyPI for Python, npm - for JavaScript/TypeScript, etc.) 📦 +- Your repo must be distributed as an installable package on PyPI 📦 - The repo should either use the Graph API (exposing a `StateGraph` instance) or the Functional API (exposing an `entrypoint`). - The package must include documentation (e.g., a `README.md` or docs site) explaining how to use it. - + We'll review your contribution and merge it in! Thanks for contributing! 🚀 +::: + +:::js +| Name | GitHub URL | Description | Weekly Downloads | Stars | +| --- | --- | --- | --- | --- | +| **@langchain/mcp-adapters** | https://github.com/langchain-ai/langchainjs | Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph agents. | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langchainjs?style=social) +| **@langchain/langgraph-supervisor** | https://github.com/langchain-ai/langgraphjs/tree/main/libs/langgraph-supervisor | Build supervisor multi-agent systems with LangGraph | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraphjs?style=social) +| **@langchain/langgraph-swarm** | https://github.com/langchain-ai/langgraphjs/tree/main/libs/langgraph-swarm | Build multi-agent swarms with LangGraph | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraphjs?style=social) +| **@langchain/langgraph-cua** | https://github.com/langchain-ai/langgraphjs/tree/main/libs/langgraph-cua | Build computer use agents with LangGraph | -12345 | ![GitHub stars](https://img.shields.io/github/stars/langchain-ai/langgraphjs?style=social) + +## ✨ Contributing Your Library + +Have you built an awesome open-source library using LangGraph? We'd love to feature +your project on the official LangGraph documentation pages! 🏆 + +To share your project, simply open a Pull Request adding an entry for your package in our [packages.yml](https://github.com/langchain-ai/langgraph/blob/main/docs/_scripts/third_party_page/packages.yml) file. + +**Guidelines** + +- Your repo must be distributed as an installable package on npm 📦 +- The repo should either use the Graph API (exposing a `StateGraph` instance) or + the Functional API (exposing an `entrypoint`). +- The package must include documentation (e.g., a `README.md` or docs site) + explaining how to use it. + +We'll review your contribution and merge it in! + +Thanks for contributing! 🚀 +::: diff --git a/docs/docs/agents/run_agents.md b/docs/docs/agents/run_agents.md index 4ea2d07e5..d87d23c9d 100644 --- a/docs/docs/agents/run_agents.md +++ b/docs/docs/agents/run_agents.md @@ -9,18 +9,27 @@ hide: # Running agents - Agents support both synchronous and asynchronous execution using either `.invoke()` / `await .ainvoke()` for full responses, or `.stream()` / `.astream()` for **incremental** [streaming](../how-tos/streaming.md) output. This section explains how to provide input, interpret output, enable streaming, and control execution limits. - ## Basic usage Agents can be executed in two primary modes: +:::python + - **Synchronous** using `.invoke()` or `.stream()` - **Asynchronous** using `await .ainvoke()` or `async for` with `.astream()` + ::: +:::js + +- **Synchronous** using `.invoke()` or `.stream()` +- **Asynchronous** using `await .invoke()` or `for await` with `.stream()` + ::: + +:::python === "Sync invocation" + ```python from langgraph.prebuilt import create_react_agent @@ -31,6 +40,7 @@ Agents can be executed in two primary modes: ``` === "Async invocation" + ```python from langgraph.prebuilt import create_react_agent @@ -39,6 +49,24 @@ Agents can be executed in two primary modes: response = await agent.ainvoke({"messages": [{"role": "user", "content": "what is the weather in sf"}]}) ``` +::: + +:::js + +```typescript +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const agent = createReactAgent(...); +// highlight-next-line +const response = await agent.invoke({ + "messages": [ + { "role": "user", "content": "what is the weather in sf" } + ] +}); +``` + +::: + ## Inputs and outputs Agents use a language model that expects a list of `messages` as an input. Therefore, agent inputs and outputs are stored as a list of `messages` under the `messages` key in the agent [state](../concepts/low_level.md#working-with-messages-in-graph-state). @@ -47,33 +75,73 @@ Agents use a language model that expects a list of `messages` as an input. There Agent input must be a dictionary with a `messages` key. Supported formats are: -| Format | Example | +:::python +| Format | Example | |--------------------|-------------------------------------------------------------------------------------------------------------------------------| -| String | `{"messages": "Hello"}` — Interpreted as a [HumanMessage](https://python.langchain.com/docs/concepts/messages/#humanmessage) | -| Message dictionary | `{"messages": {"role": "user", "content": "Hello"}}` | -| List of messages | `{"messages": [{"role": "user", "content": "Hello"}]}` | -| With custom state | `{"messages": [{"role": "user", "content": "Hello"}], "user_name": "Alice"}` — If using a custom `state_schema` | +| String | `{"messages": "Hello"}` — Interpreted as a [HumanMessage](https://python.langchain.com/docs/concepts/messages/#humanmessage) | +| Message dictionary | `{"messages": {"role": "user", "content": "Hello"}}` | +| List of messages | `{"messages": [{"role": "user", "content": "Hello"}]}` | +| With custom state | `{"messages": [{"role": "user", "content": "Hello"}], "user_name": "Alice"}` — If using a custom `state_schema` | +::: +:::js +| Format | Example | +|--------------------|-------------------------------------------------------------------------------------------------------------------------------| +| String | `{"messages": "Hello"}` — Interpreted as a [HumanMessage](https://js.langchain.com/docs/concepts/messages/#humanmessage) | +| Message dictionary | `{"messages": {"role": "user", "content": "Hello"}}` | +| List of messages | `{"messages": [{"role": "user", "content": "Hello"}]}` | +| With custom state | `{"messages": [{"role": "user", "content": "Hello"}], "user_name": "Alice"}` — If using a custom state definition | +::: + +:::python Messages are automatically converted into LangChain's internal message format. You can read more about [LangChain messages](https://python.langchain.com/docs/concepts/messages/#langchain-messages) in the LangChain documentation. +::: + +:::js +Messages are automatically converted into LangChain's internal message format. You can read +more about [LangChain messages](https://js.langchain.com/docs/concepts/messages/#langchain-messages) in the LangChain documentation. +::: !!! tip "Using custom agent state" - You can provide additional fields defined in your agent’s state schema directly in the input dictionary. This allows dynamic behavior based on runtime data or prior tool outputs. + :::python + You can provide additional fields defined in your agent's state schema directly in the input dictionary. This allows dynamic behavior based on runtime data or prior tool outputs. See the [context guide](./context.md) for full details. + ::: + + :::js + You can provide additional fields defined in your agent's state directly in the state definition. This allows dynamic behavior based on runtime data or prior tool outputs. + See the [context guide](./context.md) for full details. + ::: !!! note + :::python A string input for `messages` is converted to a [HumanMessage](https://python.langchain.com/docs/concepts/messages/#humanmessage). This behavior differs from the `prompt` parameter in `create_react_agent`, which is interpreted as a [SystemMessage](https://python.langchain.com/docs/concepts/messages/#systemmessage) when passed as a string. + ::: + :::js + A string input for `messages` is converted to a [HumanMessage](https://js.langchain.com/docs/concepts/messages/#humanmessage). This behavior differs from the `prompt` parameter in `createReactAgent`, which is interpreted as a [SystemMessage](https://js.langchain.com/docs/concepts/messages/#systemmessage) when passed as a string. + ::: ## Output format +:::python Agent output is a dictionary containing: - `messages`: A list of all messages exchanged during execution (user input, assistant replies, tool invocations). - Optionally, `structured_response` if [structured output](./agents.md#6-configure-structured-output) is configured. - If using a custom `state_schema`, additional keys corresponding to your defined fields may also be present in the output. These can hold updated state values from tool execution or prompt logic. +::: + +:::js +Agent output is a dictionary containing: + +- `messages`: A list of all messages exchanged during execution (user input, assistant replies, tool invocations). +- Optionally, `structuredResponse` if [structured output](./agents.md#6-configure-structured-output) is configured. +- If using a custom state definition, additional keys corresponding to your defined fields may also be present in the output. These can hold updated state values from tool execution or prompt logic. +::: See the [context guide](./context.md) for more details on working with custom state schemas and accessing context. @@ -87,6 +155,7 @@ Agents support streaming responses for more responsive applications. This includ Streaming is available in both sync and async modes: +:::python === "Sync streaming" ```python @@ -107,14 +176,36 @@ Streaming is available in both sync and async modes: print(chunk) ``` +::: + +:::js + +```typescript +for await (const chunk of agent.stream( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + { streamMode: "updates" } +)) { + console.log(chunk); +} +``` + +::: + !!! tip For full details, see the [streaming guide](../how-tos/streaming.md). ## Max iterations +:::python To control agent execution and avoid infinite loops, set a recursion limit. This defines the maximum number of steps the agent can take before raising a `GraphRecursionError`. You can configure `recursion_limit` at runtime or when defining agent via `.with_config()`: +::: +:::js +To control agent execution and avoid infinite loops, set a recursion limit. This defines the maximum number of steps the agent can take before raising a `GraphRecursionError`. You can configure `recursionLimit` at runtime or when defining agent via `.withConfig()`: +::: + +:::python === "Runtime" ```python @@ -163,6 +254,70 @@ To control agent execution and avoid infinite loops, set a recursion limit. This print("Agent stopped due to max iterations.") ``` +::: + +:::js +=== "Runtime" + + ```typescript + import { GraphRecursionError } from "@langchain/langgraph"; + import { ChatAnthropic } from "@langchain/langgraph/prebuilt"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + + const maxIterations = 3; + // highlight-next-line + const recursionLimit = 2 * maxIterations + 1; + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-haiku-latest" }), + tools: [getWeather] + }); + + try { + const response = await agent.invoke( + {"messages": [{"role": "user", "content": "what's the weather in sf"}]}, + // highlight-next-line + { recursionLimit } + ); + } catch (error) { + if (error instanceof GraphRecursionError) { + console.log("Agent stopped due to max iterations."); + } + } + ``` + +=== "`.withConfig()`" + + ```typescript + import { GraphRecursionError } from "@langchain/langgraph"; + import { ChatAnthropic } from "@langchain/langgraph/prebuilt"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + + const maxIterations = 3; + // highlight-next-line + const recursionLimit = 2 * maxIterations + 1; + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-haiku-latest" }), + tools: [getWeather] + }); + // highlight-next-line + const agentWithRecursionLimit = agent.withConfig({ recursionLimit }); + + try { + const response = await agentWithRecursionLimit.invoke( + {"messages": [{"role": "user", "content": "what's the weather in sf"}]}, + ); + } catch (error) { + if (error instanceof GraphRecursionError) { + console.log("Agent stopped due to max iterations."); + } + } + ``` + +::: + +:::python + ## Additional Resources -* [Async programming in LangChain](https://python.langchain.com/docs/concepts/async) +- [Async programming in LangChain](https://python.langchain.com/docs/concepts/async) + ::: diff --git a/docs/docs/agents/ui.md b/docs/docs/agents/ui.md index 41735d652..569bb5821 100644 --- a/docs/docs/agents/ui.md +++ b/docs/docs/agents/ui.md @@ -31,7 +31,7 @@ Agent Chat UI has full support for [human-in-the-loop](../concepts/human_in_the_ !!! Important - Agent Chat UI works best if your LangGraph agent interrupts using the [`HumanInterrupt` schema][langgraph.prebuilt.interrupt.HumanInterrupt]. If you do not use that schema, the Agent Chat UI will be able to render the input passed to the `interrupt` function, but it will not have full support for resuming your graph. + Agent Chat UI works best if your LangGraph agent interrupts using the @[`HumanInterrupt` schema][HumanInterrupt]. If you do not use that schema, the Agent Chat UI will be able to render the input passed to the `interrupt` function, but it will not have full support for resuming your graph. ## Generative UI diff --git a/docs/docs/cloud/deployment/setup.md b/docs/docs/cloud/deployment/setup.md index 91aecf989..986bec5ca 100644 --- a/docs/docs/cloud/deployment/setup.md +++ b/docs/docs/cloud/deployment/setup.md @@ -95,7 +95,7 @@ my-app/ ## Define Graphs -Implement your graphs! Graphs can be defined in a single file or multiple files. Make note of the variable names of each [CompiledStateGraph][langgraph.graph.state.CompiledStateGraph] to be included in the LangGraph application. The variable names will be used later when creating the [LangGraph configuration file](../reference/cli.md#configuration-file). +Implement your graphs! Graphs can be defined in a single file or multiple files. Make note of the variable names of each @[CompiledStateGraph][CompiledStateGraph] to be included in the LangGraph application. The variable names will be used later when creating the [LangGraph configuration file](../reference/cli.md#configuration-file). Example `agent.py` file, which shows how to import from other modules you define (code for the modules is not shown here, please see [this repository](https://github.com/langchain-ai/langgraph-example) to see their implementation): diff --git a/docs/docs/cloud/deployment/setup_pyproject.md b/docs/docs/cloud/deployment/setup_pyproject.md index 0b06149f6..087dd4de8 100644 --- a/docs/docs/cloud/deployment/setup_pyproject.md +++ b/docs/docs/cloud/deployment/setup_pyproject.md @@ -108,7 +108,7 @@ my-app/ ## Define Graphs -Implement your graphs! Graphs can be defined in a single file or multiple files. Make note of the variable names of each [CompiledStateGraph][langgraph.graph.state.CompiledStateGraph] to be included in the LangGraph application. The variable names will be used later when creating the [LangGraph configuration file](../reference/cli.md#configuration-file). +Implement your graphs! Graphs can be defined in a single file or multiple files. Make note of the variable names of each @[CompiledStateGraph][CompiledStateGraph] to be included in the LangGraph application. The variable names will be used later when creating the [LangGraph configuration file](../reference/cli.md#configuration-file). Example `agent.py` file, which shows how to import from other modules you define (code for the modules is not shown here, please see [this repository](https://github.com/langchain-ai/langgraph-example-pyproject) to see their implementation): diff --git a/docs/docs/cloud/how-tos/human_in_the_loop_time_travel.md b/docs/docs/cloud/how-tos/human_in_the_loop_time_travel.md index 9bec7eec1..d7d6f14fa 100644 --- a/docs/docs/cloud/how-tos/human_in_the_loop_time_travel.md +++ b/docs/docs/cloud/how-tos/human_in_the_loop_time_travel.md @@ -4,11 +4,11 @@ LangGraph provides the [**time travel**](../../concepts/time-travel.md) function To time travel using the LangGraph Server API (via the LangGraph SDK): -1. **Run the graph** with initial inputs using [LangGraph SDK](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/python_sdk_ref/)'s [`client.runs.wait`][langgraph_sdk.client.RunsClient.wait] or [`client.runs.stream`][langgraph_sdk.client.RunsClient.stream] APIs. -2. **Identify a checkpoint in an existing thread**: Use [`client.threads.get_history`][langgraph_sdk.client.ThreadsClient.get_history] method to retrieve the execution history for a specific `thread_id` and locate the desired `checkpoint_id`. +1. **Run the graph** with initial inputs using [LangGraph SDK](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/python_sdk_ref/)'s @[`client.runs.wait`][client.runs.wait] or @[`client.runs.stream`][client.runs.stream] APIs. +2. **Identify a checkpoint in an existing thread**: Use @[`client.threads.get_history`][client.threads.get_history] method to retrieve the execution history for a specific `thread_id` and locate the desired `checkpoint_id`. Alternatively, set a [breakpoint](./human_in_the_loop_breakpoint.md) before the node(s) where you want execution to pause. You can then find the most recent checkpoint recorded up to that breakpoint. -3. **(Optional) modify the graph state**: Use the [`client.threads.update_state`][langgraph_sdk.client.ThreadsClient.update_state] method to modify the graph’s state at the checkpoint and resume execution from alternative state. -4. **Resume execution from the checkpoint**: Use the [`client.runs.wait`][langgraph_sdk.client.RunsClient.wait] or [`client.runs.stream`][langgraph_sdk.client.RunsClient.stream] APIs with an input of `None` and the appropriate `thread_id` and `checkpoint_id`. +3. **(Optional) modify the graph state**: Use the @[`client.threads.update_state`][client.threads.update_state] method to modify the graph’s state at the checkpoint and resume execution from alternative state. +4. **Resume execution from the checkpoint**: Use the @[`client.runs.wait`][client.runs.wait] or @[`client.runs.stream`][client.runs.stream] APIs with an input of `None` and the appropriate `thread_id` and `checkpoint_id`. ## Use time travel in a workflow diff --git a/docs/docs/cloud/how-tos/use_stream_react.md b/docs/docs/cloud/how-tos/use_stream_react.md index 3ddcbe5b4..6f9d5016f 100644 --- a/docs/docs/cloud/how-tos/use_stream_react.md +++ b/docs/docs/cloud/how-tos/use_stream_react.md @@ -137,7 +137,7 @@ const thread = useStream<{ messages: Message[] }>({ You can also manually manage the resuming process by using the run callbacks to persist the run metadata and the `joinStream` function to resume the stream. Make sure to pass `streamResumable: true` when creating the run; otherwise some events might be lost. -````tsx +```tsx import type { Message } from "@langchain/langgraph-sdk"; import { useStream } from "@langchain/langgraph-sdk/react"; import { useCallback, useState, useEffect, useRef } from "react"; @@ -236,7 +236,7 @@ const thread = useStream<{ messages: Message[] }>({ threadId: threadId, onThreadId: setThreadId, }); -```` +``` We recommend storing the `threadId` in your URL's query parameters to let users resume conversations after page refreshes. diff --git a/docs/docs/concepts/application_structure.md b/docs/docs/concepts/application_structure.md index 4b1f79228..f2a96ddac 100644 --- a/docs/docs/concepts/application_structure.md +++ b/docs/docs/concepts/application_structure.md @@ -22,8 +22,9 @@ To deploy using the LangGraph Platform, the following information should be prov ## File Structure -Below are examples of directory structures for Python and JavaScript applications: +Below are examples of directory structures for applications: +:::python === "Python (requirements.txt)" ```plaintext @@ -40,6 +41,7 @@ Below are examples of directory structures for Python and JavaScript application ├── requirements.txt # package dependencies └── langgraph.json # configuration file for LangGraph ``` + === "Python (pyproject.toml)" ```plaintext @@ -57,20 +59,24 @@ Below are examples of directory structures for Python and JavaScript application └── pyproject.toml # dependencies for your project ``` -=== "JS (package.json)" +::: - ```plaintext - my-app/ - ├── src # all project code lies within here - │ ├── utils # optional utilities for your graph - │ │ ├── tools.ts # tools for your graph - │ │ ├── nodes.ts # node functions for your graph - │ │ └── state.ts # state definition of your graph - │ └── agent.ts # code for constructing your graph - ├── package.json # package dependencies - ├── .env # environment variables - └── langgraph.json # configuration file for LangGraph - ``` +:::js + +```plaintext +my-app/ +├── src # all project code lies within here +│ ├── utils # optional utilities for your graph +│ │ ├── tools.ts # tools for your graph +│ │ ├── nodes.ts # node functions for your graph +│ │ └── state.ts # state definition of your graph +│ └── agent.ts # code for constructing your graph +├── package.json # package dependencies +├── .env # environment variables +└── langgraph.json # configuration file for LangGraph +``` + +::: !!! note @@ -88,52 +94,66 @@ See the [LangGraph configuration file reference](../cloud/reference/cli.md#confi ### Examples -=== "Python" +:::python - * The dependencies involve a custom local package and the `langchain_openai` package. - * A single graph will be loaded from the file `./your_package/your_file.py` with the variable `variable`. - * The environment variables are loaded from the `.env` file. +- The dependencies involve a custom local package and the `langchain_openai` package. +- A single graph will be loaded from the file `./your_package/your_file.py` with the variable `variable`. +- The environment variables are loaded from the `.env` file. - ```json - { - "dependencies": [ - "langchain_openai", - "./your_package" - ], - "graphs": { - "my_agent": "./your_package/your_file.py:agent" - }, - "env": "./.env" - } - ``` +```json +{ + "dependencies": ["langchain_openai", "./your_package"], + "graphs": { + "my_agent": "./your_package/your_file.py:agent" + }, + "env": "./.env" +} +``` -=== "JavaScript" +::: - * The dependencies will be loaded from a dependency file in the local directory (e.g., `package.json`). - * A single graph will be loaded from the file `./your_package/your_file.js` with the function `agent`. - * The environment variable `OPENAI_API_KEY` is set inline. +:::js - ```json - { - "dependencies": [ - "." - ], - "graphs": { - "my_agent": "./your_package/your_file.js:agent" - }, - "env": { - "OPENAI_API_KEY": "secret-key" - } - } - ``` +- The dependencies will be loaded from a dependency file in the local directory (e.g., `package.json`). +- A single graph will be loaded from the file `./your_package/your_file.js` with the function `agent`. +- The environment variable `OPENAI_API_KEY` is set inline. + +```json +{ + "dependencies": ["."], + "graphs": { + "my_agent": "./your_package/your_file.js:agent" + }, + "env": { + "OPENAI_API_KEY": "secret-key" + } +} +``` + +::: ## Dependencies -A LangGraph application may depend on other Python packages or JavaScript libraries (depending on the programming language in which the application is written). +:::python +A LangGraph application may depend on other Python packages. +::: + +:::js +A LangGraph application may depend on other TypeScript/JavaScript libraries. +::: You will generally need to specify the following information for dependencies to be set up correctly: +:::python + 1. A file in the directory that specifies the dependencies (e.g. `requirements.txt`, `pyproject.toml`, or `package.json`). + ::: + +:::js + +1. A file in the directory that specifies the dependencies (e.g. `package.json`). + ::: + 2. A `dependencies` key in the [LangGraph configuration file](#configuration-file-concepts) that specifies the dependencies required to run the LangGraph application. 3. Any additional binaries or system libraries can be specified using `dockerfile_lines` key in the [LangGraph configuration file](#configuration-file-concepts). diff --git a/docs/docs/concepts/auth.md b/docs/docs/concepts/auth.md index 204764ed3..9c492f9ee 100644 --- a/docs/docs/concepts/auth.md +++ b/docs/docs/concepts/auth.md @@ -16,7 +16,13 @@ While often used interchangeably, these terms represent distinct security concep - [**Authentication**](#authentication) ("AuthN") verifies _who_ you are. This runs as middleware for every request. - [**Authorization**](#authorization) ("AuthZ") determines _what you can do_. This validates the user's privileges and roles on a per-resource basis. +:::python In LangGraph Platform, authentication is handled by your [`@auth.authenticate`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.authenticate) handler, and authorization is handled by your [`@auth.on`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.on) handlers. +::: + +:::js +In LangGraph Platform, authentication is handled by your [`@auth.authenticate`](../cloud/reference/sdk/typescript_sdk_ref.md#auth.authenticate) handler, and authorization is handled by your [`@auth.on`](../cloud/reference/sdk/typescript_sdk_ref.md#auth.on) handlers. +::: ## Default Security Models @@ -29,7 +35,7 @@ LangGraph Platform provides different security defaults: - Can be customized with your auth handler !!! note "Custom auth" - Custom auth **is supported** for all plans in LangGraph Platform. +Custom auth **is supported** for all plans in LangGraph Platform. ### Self-Hosted @@ -38,6 +44,7 @@ LangGraph Platform provides different security defaults: - You control all aspects of authentication and authorization !!! note "Custom auth" + Custom auth is supported for **Enterprise** self-hosted deployments. Standalone Container (Lite) deployments do not support custom auth natively. @@ -47,24 +54,24 @@ A typical authentication setup involves three main components: 1. **Authentication Provider** (Identity Provider/IdP) - * A dedicated service that manages user identities and credentials - * Handles user registration, login, password resets, etc. - * Issues tokens (JWT, session tokens, etc.) after successful authentication - * Examples: Auth0, Supabase Auth, Okta, or your own auth server + - A dedicated service that manages user identities and credentials + - Handles user registration, login, password resets, etc. + - Issues tokens (JWT, session tokens, etc.) after successful authentication + - Examples: Auth0, Supabase Auth, Okta, or your own auth server 2. **LangGraph Backend** (Resource Server) - * Your LangGraph application that contains business logic and protected resources - * Validates tokens with the auth provider - * Enforces access control based on user identity and permissions - * Doesn't store user credentials directly + - Your LangGraph application that contains business logic and protected resources + - Validates tokens with the auth provider + - Enforces access control based on user identity and permissions + - Doesn't store user credentials directly 3. **Client Application** (Frontend) - * Web app, mobile app, or API client - * Collects time-sensitive user credentials and sends to auth provider - * Receives tokens from auth provider - * Includes these tokens in requests to LangGraph backend + - Web app, mobile app, or API client + - Collects time-sensitive user credentials and sends to auth provider + - Receives tokens from auth provider + - Includes these tokens in requests to LangGraph backend Here's how these components typically interact: @@ -84,15 +91,22 @@ sequenceDiagram LG-->>Client: 8. Return resources ``` +:::python Your [`@auth.authenticate`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.authenticate) handler in LangGraph handles steps 4-6, while your [`@auth.on`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.on) handlers implement step 7. +::: + +:::js +Your [`auth.authenticate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#authenticate) handler in LangGraph handles steps 4-6, while your [`auth.on`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#on>) handlers implement step 7. +::: ## Authentication +:::python Authentication in LangGraph runs as middleware on every request. Your [`@auth.authenticate`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.authenticate) handler receives request information and should: 1. Validate the credentials 2. Return [user info](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.MinimalUserDict) containing the user's identity and user information if valid -3. Raise an [HTTP exception](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.exceptions.HTTPException) or AssertionError if invalid +3. Raise an [HTTPException](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.exceptions.HTTPException) or AssertionError if invalid ```python from langgraph_sdk import Auth @@ -126,9 +140,49 @@ The returned user information is available: - To your authorization handlers via [`ctx.user`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.AuthContext) - In your application via `config["configuration"]["langgraph_auth_user"]` + ::: + +:::js +Authentication in LangGraph runs as middleware on every request. Your [`authenticate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#authenticate>) handler receives request information and should: + +1. Validate the credentials +2. Return user information containing the user's identity and user information if valid +3. Raise an [HTTPException](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#class-httpexception>) if invalid + +```typescript +import { Auth, HTTPException } from "@langchain/langgraph-sdk"; + +export const auth = new Auth(); + +auth.authenticate(async (request) => { + // Validate credentials (e.g., API key, JWT token) + const apiKey = request.headers.get("x-api-key"); + if (!apiKey || !isValidKey(apiKey)) { + throw new HTTPException(401, "Invalid API key"); + } + + // Return user info - only identity and isAuthenticated are required + // Add any additional fields you need for authorization + return { + identity: "user-123", // Required: unique user identifier + isAuthenticated: true, // Optional: assumed true by default + permissions: ["read", "write"], // Optional: for permission-based auth + // You can add more custom fields if you want to implement other auth patterns + role: "admin", + orgId: "org-456", + }; +}); +``` + +The returned user information is available: + +- To your authorization handlers via the `user` property in a [callback handler](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#on) +- In your application via `config.configurable.langgraph_auth_user` + ::: ??? tip "Supported Parameters" + :::python The [`@auth.authenticate`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.authenticate) handler can accept any of the following parameters by name: * request (Request): The raw ASGI request object @@ -139,13 +193,27 @@ The returned user information is available: * query_params (dict[str, str]): URL query parameters, e.g., {"stream": "true"} * headers (dict[bytes, bytes]): Request headers * authorization (str | None): The Authorization header value (e.g., "Bearer <token>") - + ::: + + :::js + The [`authenticate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#authenticate) handler can accept any of the following parameters: + + * request (Request): The raw request object + * body (object): The parsed request body + * path (string): The request path, e.g., "/threads/abcd-1234-abcd-1234/runs/abcd-1234-abcd-1234/stream" + * method (string): The HTTP method, e.g., "GET" + * pathParams (Record<string, string>): URL path parameters, e.g., {"threadId": "abcd-1234-abcd-1234", "runId": "abcd-1234-abcd-1234"} + * queryParams (Record<string, string>): URL query parameters, e.g., {"stream": "true"} + * headers (Record<string, string>): Request headers + * authorization (string | null): The Authorization header value (e.g., "Bearer <token>") + ::: + In many of our tutorials, we will just show the "authorization" parameter to be concise, but you can opt to accept more information as needed to implement your custom authentication scheme. ### Agent authentication -Custom authentication permits delegated access. The values you return in `@auth.authenticate` are added to the run context, giving agents user-scoped credentials lets them access resources on the user’s behalf. +Custom authentication permits delegated access. The values you return in `@auth.authenticate` are added to the run context, giving agents user-scoped credentials lets them access resources on the user’s behalf. ```mermaid sequenceDiagram @@ -177,7 +245,7 @@ sequenceDiagram ExternalService -->> LangGraph: 10. Service response %% Return to caller - LangGraph -->> ClientApp: 11. Return resources + LangGraph -->> ClientApp: 11. Return resources ``` After authentication, the platform creates a special configuration object that is passed to your graph and all nodes via the configurable context. @@ -193,13 +261,16 @@ For information on how to authenticate an agent to an MCP server, see the [MCP c ## Authorization -After authentication, LangGraph calls your [`@auth.on`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.on) handlers to control access to specific resources (e.g., threads, assistants, crons). These handlers can: +After authentication, LangGraph calls your authorization handlers to control access to specific resources (e.g., threads, assistants, crons). These handlers can: -1. Add metadata to be saved during resource creation by mutating the `value["metadata"]` dictionary directly. See the [supported actions table](#supported-actions) for the list of types the value can take for each action. -2. Filter resources by metadata during search/list or read operations by returning a [filter dictionary](#filter-operations). +1. Add metadata to be saved during resource creation by mutating the metadata. See the [supported actions table](#supported-actions) for the list of types the value can take for each action. +2. Filter resources by metadata during search/list or read operations by returning a [filter](#filter-operations). 3. Raise an HTTP exception if access is denied. -If you want to just implement simple user-scoped access control, you can use a single [`@auth.on`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.on) handler for all resources and actions. If you want to have different control depending on the resource and action, you can use [resource-specific handlers](#resource-specific-handlers). See the [Supported Resources](#supported-resources) section for a full list of the resources that support access control. +If you want to just implement simple user-scoped access control, you can use a single authorization handler for all resources and actions. If you want to have different control depending on the resource and action, you can use [resource-specific handlers](#resource-specific-handlers). See the [Supported Resources](#supported-resources) section for a full list of the resources that support access control. + +:::python +Your [`@auth.on`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.on) handlers control access by mutating the `value["metadata"]` dictionary directly and returning a [filter dictionary](#filter-operations). ```python @auth.on @@ -241,9 +312,42 @@ async def add_owner( return filters ``` +::: + +:::js +You can granularly control access by mutating the `value.metadata` object directly and returning a [filter object](#filter-operations) when registering an [`on()`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#on) handler. + +```typescript +import { Auth, HTTPException } from "@langchain/langgraph-sdk/auth"; + +export const auth = new Auth() + .authenticate(async (request: Request) => ({ + identity: "user-123", + permissions: [], + })) + .on("*", ({ value, user }) => { + // Create filter to restrict access to just this user's resources + const filters = { owner: user.identity }; + + // If the operation supports metadata, add the user identity + // as metadata to the resource. + if ("metadata" in value) { + value.metadata ??= {}; + value.metadata.owner = user.identity; + } + + // Return filters to restrict access + // These filters are applied to ALL operations (create, read, update, search, etc.) + // to ensure users can only access their own resources + return filters; + }); +``` + +::: + ### Resource-Specific Handlers {#resource-specific-handlers} -You can register handlers for specific resources and actions by chaining the resource and action names together with the [`@auth.on`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.on) decorator. +You can register handlers for specific resources and actions by chaining the resource and action names together with the authorization decorator. When a request is made, the most specific handler that matches that resource and action is called. Below is an example of how to register handlers for specific resources and actions. For the following setup: 1. Authenticated users are able to create threads, read threads, and create runs on threads @@ -254,6 +358,8 @@ When a request is made, the most specific handler that matches that resource and For a full list of supported resources and actions, see the [Supported Resources](#supported-resources) section below. +:::python + ```python # Generic / global handler catches calls that aren't handled by more specific handlers @auth.on @@ -338,11 +444,104 @@ async def on_assistant_create( ) ``` +::: + +:::js + +```typescript +import { Auth, HTTPException } from "@langchain/langgraph-sdk/auth"; + +export const auth = new Auth() + .authenticate(async (request: Request) => ({ + identity: "user-123", + permissions: ["threads:write", "threads:read"], + })) + .on("*", ({ event, user }) => { + console.log(`Request for ${event} by ${user.identity}`); + throw new HTTPException(403, { message: "Forbidden" }); + }) + + // Matches the "threads" resource and all actions - create, read, update, delete, search + // Since this is **more specific** than the generic `on("*")` handler, it will take precedence over the generic handler for all actions on the "threads" resource + .on("threads", ({ permissions, value, user }) => { + if (!permissions.includes("write")) { + throw new HTTPException(403, { + message: "User lacks the required permissions.", + }); + } + + // Not all events do include `metadata` property in `value`. + // So we need to add this type guard. + if ("metadata" in value) { + value.metadata ??= {}; + value.metadata.owner = user.identity; + } + + return { owner: user.identity }; + }) + + // Thread creation. This will match only on thread create actions. + // Since this is **more specific** than both the generic `on("*")` handler and the `on("threads")` handler, it will take precedence for any "create" actions on the "threads" resources + .on("threads:create", ({ value, user, permissions }) => { + if (!permissions.includes("write")) { + throw new HTTPException(403, { + message: "User lacks the required permissions.", + }); + } + + // Setting metadata on the thread being created will ensure that the resource contains an "owner" field + // Then any time a user tries to access this thread or runs within the thread, + // we can filter by owner + value.metadata ??= {}; + value.metadata.owner = user.identity; + + return { owner: user.identity }; + }) + + // Reading a thread. Since this is also more specific than the generic `on("*")` handler, and the `on("threads")` handler, + .on("threads:read", ({ user }) => { + // Since we are reading (and not creating) a thread, + // we don't need to set metadata. We just need to + // return a filter to ensure users can only see their own threads. + return { owner: user.identity }; + }) + + // Run creation, streaming, updates, etc. + // This takes precedence over the generic `on("*")` handler and the `on("threads")` handler + .on("threads:create_run", ({ value, user }) => { + value.metadata ??= {}; + value.metadata.owner = user.identity; + + return { owner: user.identity }; + }) + + // Assistant creation. This will match only on assistant create actions. + // Since this is **more specific** than both the generic `on("*")` handler and the `on("assistants")` handler, it will take precedence for any "create" actions on the "assistants" resources + .on("assistants:create", ({ value, user, permissions }) => { + if (!permissions.includes("assistants:create")) { + throw new HTTPException(403, { + message: "User lacks the required permissions.", + }); + } + + // Setting metadata on the assistant being created will ensure that the resource contains an "owner" field. + // Then any time a user tries to access this assistant, we can filter by owner + value.metadata ??= {}; + value.metadata.owner = user.identity; + + return { owner: user.identity }; + }); +``` + +::: + Notice that we are mixing global and resource-specific handlers in the above example. Since each request is handled by the most specific handler, a request to create a `thread` would match the `on_thread_create` handler but NOT the `reject_unhandled_requests` handler. A request to `update` a thread, however would be handled by the global handler, since we don't have a more specific handler for that resource and action. ### Filter Operations {#filter-operations} -Authorization handlers can return `None`, a boolean, or a filter dictionary. +:::python +Authorization handlers can return different types of values: + - `None` and `True` mean "authorize access to all underling resources" - `False` means "deny access to all underling resources (raises a 403 exception)" - A metadata filter dictionary will restrict access to resources @@ -355,6 +554,24 @@ A filter dictionary is a dictionary with keys that match the resource metadata. A dictionary with multiple keys is treated using a logical `AND` filter. For example, `{"owner": org_id, "allowed_users": {"$contains": user_id}}` will only match resources with metadata whose "owner" is `org_id` and whose "allowed_users" list contains `user_id`. See the reference [here](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.FilterType) for more information. +::: + +:::js +Authorization handlers can return different types of values: + +- `null` and `true` mean "authorize access to all underling resources" +- `false` means "deny access to all underling resources (raises a 403 exception)" +- A metadata filter object will restrict access to resources + +A filter object is an object with keys that match the resource metadata. It supports three operators: + +- The default value is a shorthand for exact match, or "$eq", below. For example, `{ owner: userId}` will include only resources with metadata containing `{ owner: userId }` +- `$eq`: Exact match (e.g., `{ owner: { $eq: userId } }`) - this is equivalent to the shorthand above, `{ owner: userId }` +- `$contains`: List membership (e.g., `{ allowedUsers: { $contains: userId} }`) The value here must be an element of the list. The metadata in the stored resource must be a list/container type. + +An object with multiple keys is treated using a logical `AND` filter. For example, `{ owner: orgId, allowedUsers: { $contains: userId} }` will only match resources with metadata whose "owner" is `orgId` and whose "allowedUsers" list contains `userId`. +See the reference [here](../cloud/reference/sdk/typescript_sdk_ref.md#auth.types.FilterType) for more information. +::: ## Common Access Patterns @@ -364,6 +581,8 @@ Here are some typical authorization patterns: This common pattern lets you scope all threads, assistants, crons, and runs to a single user. It's useful for common single-user use cases like regular chatbot-style apps. +:::python + ```python @auth.on async def owner_only(ctx: Auth.types.AuthContext, value: dict): @@ -372,10 +591,33 @@ async def owner_only(ctx: Auth.types.AuthContext, value: dict): return {"owner": ctx.user.identity} ``` +::: + +:::js + +```typescript +export const auth = new Auth() + .authenticate(async (request: Request) => ({ + identity: "user-123", + permissions: ["threads:write", "threads:read"], + })) + .on("*", ({ value, user }) => { + if ("metadata" in value) { + value.metadata ??= {}; + value.metadata.owner = user.identity; + } + return { owner: user.identity }; + }); +``` + +::: + ### Permission-based Access This pattern lets you control access based on **permissions**. It's useful if you want certain roles to have broader or more restricted access to resources. +:::python + ```python # In your auth handler: @auth.authenticate @@ -412,19 +654,72 @@ async def rbac_create(ctx: Auth.types.AuthContext, value: dict): return _default(ctx, value) ``` +::: + +:::js + +```typescript +import { Auth, HTTPException } from "@langchain/langgraph-sdk/auth"; + +export const auth = new Auth() + .authenticate(async (request: Request) => ({ + identity: "user-123", + // Define permissions in auth + permissions: ["threads:write", "threads:read"], + })) + .on("threads:create", ({ value, user, permissions }) => { + if (!permissions.includes("threads:write")) { + throw new HTTPException(403, { message: "Unauthorized" }); + } + + if ("metadata" in value) { + value.metadata ??= {}; + value.metadata.owner = user.identity; + } + return { owner: user.identity }; + }) + .on("threads:read", ({ user, permissions }) => { + if ( + !permissions.includes("threads:read") && + !permissions.includes("threads:write") + ) { + throw new HTTPException(403, { message: "Unauthorized" }); + } + + return { owner: user.identity }; + }); +``` + +::: + ## Supported Resources LangGraph provides three levels of authorization handlers, from most general to most specific: +:::python + 1. **Global Handler** (`@auth.on`): Matches all resources and actions 2. **Resource Handler** (e.g., `@auth.on.threads`, `@auth.on.assistants`, `@auth.on.crons`): Matches all actions for a specific resource 3. **Action Handler** (e.g., `@auth.on.threads.create`, `@auth.on.threads.read`): Matches a specific action on a specific resource The most specific matching handler will be used. For example, `@auth.on.threads.create` takes precedence over `@auth.on.threads` for thread creation. If a more specific handler is registered, the more general handler will not be called for that resource and action. +::: +:::js + +1. **Global Handler** (`on("*")`): Matches all resources and actions +2. **Resource Handler** (e.g., `on("threads")`, `on("assistants")`, `on("crons")`): Matches all actions for a specific resource +3. **Action Handler** (e.g., `on("threads:create")`, `on("threads:read")`): Matches a specific action on a specific resource + +The most specific matching handler will be used. For example, `on("threads:create")` takes precedence over `on("threads")` for thread creation. +If a more specific handler is registered, the more general handler will not be called for that resource and action. +::: + +:::python ???+ tip "Type Safety" - Each handler has type hints available for its `value` parameter at `Auth.types.on.<resource>.<action>.value`. For example: +Each handler has type hints available for its `value` parameter. For example: + ```python @auth.on.threads.create async def on_thread_create( @@ -432,14 +727,14 @@ If a more specific handler is registered, the more general handler will not be c value: Auth.types.on.threads.create.value # Specific type for thread creation ): ... - + @auth.on.threads async def on_threads( ctx: Auth.types.AuthContext, value: Auth.types.on.threads.value # Union type of all thread actions ): ... - + @auth.on async def on_all( ctx: Auth.types.AuthContext, @@ -447,11 +742,16 @@ If a more specific handler is registered, the more general handler will not be c ): ... ``` + More specific handlers provide better type hints since they handle fewer action types. +::: + #### Supported actions and types {#supported-actions} + Here are all the supported action handlers: +:::python | Resource | Handler | Description | Value Type | |----------|---------|-------------|------------| | **Threads** | `@auth.on.threads.create` | Thread creation | [`ThreadsCreate`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.ThreadsCreate) | @@ -470,12 +770,40 @@ Here are all the supported action handlers: | | `@auth.on.crons.update` | Cron job updates | [`CronsUpdate`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.CronsUpdate) | | | `@auth.on.crons.delete` | Cron job deletion | [`CronsDelete`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.CronsDelete) | | | `@auth.on.crons.search` | Listing cron jobs | [`CronsSearch`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.CronsSearch) | +::: + +:::js +| Resource | Event | Description | Value Type | +| -------------- | -------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------ | +| **Threads** | `threads:create` | Thread creation | [`ThreadsCreate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#threadscreate) | +| | `threads:read` | Thread retrieval | [`ThreadsRead`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#threadsread) | +| | `threads:update` | Thread updates | [`ThreadsUpdate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#threadsupdate) | +| | `threads:delete` | Thread deletion | [`ThreadsDelete`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#threadsdelete) | +| | `threads:search` | Listing threads | [`ThreadsSearch`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#threadssearch) | +| | `threads:create_run` | Creating or updating a run | [`RunsCreate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#threadscreate_run) | +| **Assistants** | `assistants:create` | Assistant creation | [`AssistantsCreate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#assistantscreate) | +| | `assistants:read` | Assistant retrieval | [`AssistantsRead`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#assistantsread) | +| | `assistants:update` | Assistant updates | [`AssistantsUpdate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#assistantsupdate) | +| | `assistants:delete` | Assistant deletion | [`AssistantsDelete`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#assistantsdelete) | +| | `assistants:search` | Listing assistants | [`AssistantsSearch`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#assistantssearch) | +| **Crons** | `crons:create` | Cron job creation | [`CronsCreate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#cronscreate) | +| | `crons:read` | Cron job retrieval | [`CronsRead`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#cronsread) | +| | `crons:update` | Cron job updates | [`CronsUpdate`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#cronsupdate) | +| | `crons:delete` | Cron job deletion | [`CronsDelete`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#cronsdelete) | +| | `crons:search` | Listing cron jobs | [`CronsSearch`](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/js_ts_sdk_ref/#cronssearch) | +::: ???+ note "About Runs" Runs are scoped to their parent thread for access control. This means permissions are typically inherited from the thread, reflecting the conversational nature of the data model. All run operations (reading, listing) except creation are controlled by the thread's handlers. - There is a specific `create_run` handler for creating new runs because it had more arguments that you can view in the handler. + :::python + There is a specific `create_run` handler for creating new runs because it had more arguments that you can view in the handler. + ::: + + :::js + There is a specific `threads:create_run` handler for creating new runs because it had more arguments that you can view in the handler. + ::: ## Next Steps diff --git a/docs/docs/concepts/durable_execution.md b/docs/docs/concepts/durable_execution.md index 88822ecea..f49b989fe 100644 --- a/docs/docs/concepts/durable_execution.md +++ b/docs/docs/concepts/durable_execution.md @@ -5,7 +5,7 @@ search: # Durable Execution -**Durable execution** is a technique in which a process or workflow saves its progress at key points, allowing it to pause and later resume exactly where it left off. This is particularly useful in scenarios that require [human-in-the-loop](./human_in_the_loop.md), where users can inspect, validate, or modify the process before continuing, and in long-running tasks that might encounter interruptions or errors (e.g., calls to an LLM timing out). By preserving completed work, durable execution enables a process to resume without reprocessing previous steps -- even after a significant delay (e.g., a week later). +**Durable execution** is a technique in which a process or workflow saves its progress at key points, allowing it to pause and later resume exactly where it left off. This is particularly useful in scenarios that require [human-in-the-loop](./human_in_the_loop.md), where users can inspect, validate, or modify the process before continuing, and in long-running tasks that might encounter interruptions or errors (e.g., calls to an LLM timing out). By preserving completed work, durable execution enables a process to resume without reprocessing previous steps -- even after a significant delay (e.g., a week later). LangGraph's built-in [persistence](./persistence.md) layer provides durable execution for workflows, ensuring that the state of each execution step is saved to a durable store. This capability guarantees that if a workflow is interrupted -- whether by a system failure or for [human-in-the-loop](./human_in_the_loop.md) interactions -- it can be resumed from its last recorded state. @@ -20,7 +20,14 @@ To leverage durable execution in LangGraph, you need to: 1. Enable [persistence](./persistence.md) in your workflow by specifying a [checkpointer](./persistence.md#checkpointer-libraries) that will save workflow progress. 2. Specify a [thread identifier](./persistence.md#threads) when executing a workflow. This will track the execution history for a particular instance of the workflow. -3. Wrap any non-deterministic operations (e.g., random number generation) or operations with side effects (e.g., file writes, API calls) inside [tasks][langgraph.func.task] to ensure that when a workflow is resumed, these operations are not repeated for the particular run, and instead their results are retrieved from the persistence layer. For more information, see [Determinism and Consistent Replay](#determinism-and-consistent-replay). + +:::python +3. Wrap any non-deterministic operations (e.g., random number generation) or operations with side effects (e.g., file writes, API calls) inside @[tasks][task] to ensure that when a workflow is resumed, these operations are not repeated for the particular run, and instead their results are retrieved from the persistence layer. For more information, see [Determinism and Consistent Replay](#determinism-and-consistent-replay). +::: + +:::js +3. Wrap any non-deterministic operations (e.g., random number generation) or operations with side effects (e.g., file writes, API calls) inside @[tasks][task] to ensure that when a workflow is resumed, these operations are not repeated for the particular run, and instead their results are retrieved from the persistence layer. For more information, see [Determinism and Consistent Replay](#determinism-and-consistent-replay). +::: ## Determinism and Consistent Replay @@ -30,17 +37,25 @@ As a result, when you are writing a workflow for durable execution, you must wra To ensure that your workflow is deterministic and can be consistently replayed, follow these guidelines: -- **Avoid Repeating Work**: If a [node](./low_level.md#nodes) contains multiple operations with side effects (e.g., logging, file writes, or network calls), wrap each operation in a separate **task**. This ensures that when the workflow is resumed, the operations are not repeated, and their results are retrieved from the persistence layer. -- **Encapsulate Non-Deterministic Operations:** Wrap any code that might yield non-deterministic results (e.g., random number generation) inside **tasks** or **nodes**. This ensures that, upon resumption, the workflow follows the exact recorded sequence of steps with the same outcomes. +- **Avoid Repeating Work**: If a [node](./low_level.md#nodes) contains multiple operations with side effects (e.g., logging, file writes, or network calls), wrap each operation in a separate **task**. This ensures that when the workflow is resumed, the operations are not repeated, and their results are retrieved from the persistence layer. +- **Encapsulate Non-Deterministic Operations:** Wrap any code that might yield non-deterministic results (e.g., random number generation) inside **tasks** or **nodes**. This ensures that, upon resumption, the workflow follows the exact recorded sequence of steps with the same outcomes. - **Use Idempotent Operations**: When possible ensure that side effects (e.g., API calls, file writes) are idempotent. This means that if an operation is retried after a failure in the workflow, it will have the same effect as the first time it was executed. This is particularly important for operations that result in data writes. In the event that a **task** starts but fails to complete successfully, the workflow's resumption will re-run the **task**, relying on recorded outcomes to maintain consistency. Use idempotency keys or verify existing results to avoid unintended duplication, ensuring a smooth and predictable workflow execution. +:::python For some examples of pitfalls to avoid, see the [Common Pitfalls](./functional_api.md#common-pitfalls) section in the functional API, which shows -how to structure your code using **tasks** to avoid these issues. The same principles apply to the [StateGraph (Graph API)][langgraph.graph.state.StateGraph]. +how to structure your code using **tasks** to avoid these issues. The same principles apply to the @[StateGraph (Graph API)][StateGraph]. +::: + +:::js +For some examples of pitfalls to avoid, see the [Common Pitfalls](./functional_api.md#common-pitfalls) section in the functional API, which shows +how to structure your code using **tasks** to avoid these issues. The same principles apply to the @[StateGraph (Graph API)][StateGraph]. +::: ## Using tasks in nodes If a [node](./low_level.md#nodes) contains multiple operations, you may find it easier to convert each operation into a **task** rather than refactor the operations into individual nodes. +:::python === "Original" ```python @@ -142,16 +157,136 @@ If a [node](./low_level.md#nodes) contains multiple operations, you may find it graph.invoke({"urls": ["https://www.example.com"]}, config) ``` +::: + +:::js +=== "Original" + + ```typescript + import { StateGraph, START, END } from "@langchain/langgraph"; + import { MemorySaver } from "@langchain/langgraph"; + import { v4 as uuidv4 } from "uuid"; + import { z } from "zod"; + + // Define a Zod schema to represent the state + const State = z.object({ + url: z.string(), + result: z.string().optional(), + }); + + const callApi = async (state: z.infer<typeof State>) => { + // highlight-next-line + const response = await fetch(state.url); + const text = await response.text(); + const result = text.slice(0, 100); // Side-effect + return { + result, + }; + }; + + // Create a StateGraph builder and add a node for the callApi function + const builder = new StateGraph(State) + .addNode("callApi", callApi) + .addEdge(START, "callApi") + .addEdge("callApi", END); + + // Specify a checkpointer + const checkpointer = new MemorySaver(); + + // Compile the graph with the checkpointer + const graph = builder.compile({ checkpointer }); + + // Define a config with a thread ID. + const threadId = uuidv4(); + const config = { configurable: { thread_id: threadId } }; + + // Invoke the graph + await graph.invoke({ url: "https://www.example.com" }, config); + ``` + +=== "With task" + + ```typescript + import { StateGraph, START, END } from "@langchain/langgraph"; + import { MemorySaver } from "@langchain/langgraph"; + import { task } from "@langchain/langgraph"; + import { v4 as uuidv4 } from "uuid"; + import { z } from "zod"; + + // Define a Zod schema to represent the state + const State = z.object({ + urls: z.array(z.string()), + results: z.array(z.string()).optional(), + }); + + const makeRequest = task("makeRequest", async (url: string) => { + // highlight-next-line + const response = await fetch(url); + const text = await response.text(); + return text.slice(0, 100); + }); + + const callApi = async (state: z.infer<typeof State>) => { + // highlight-next-line + const requests = state.urls.map((url) => makeRequest(url)); + const results = await Promise.all(requests); + return { + results, + }; + }; + + // Create a StateGraph builder and add a node for the callApi function + const builder = new StateGraph(State) + .addNode("callApi", callApi) + .addEdge(START, "callApi") + .addEdge("callApi", END); + + // Specify a checkpointer + const checkpointer = new MemorySaver(); + + // Compile the graph with the checkpointer + const graph = builder.compile({ checkpointer }); + + // Define a config with a thread ID. + const threadId = uuidv4(); + const config = { configurable: { thread_id: threadId } }; + + // Invoke the graph + await graph.invoke({ urls: ["https://www.example.com"] }, config); + ``` + +::: + ## Resuming Workflows Once you have enabled durable execution in your workflow, you can resume execution for the following scenarios: -- **Pausing and Resuming Workflows:** Use the [interrupt][langgraph.types.interrupt] function to pause a workflow at specific points and the [Command][langgraph.types.Command] primitive to resume it with updated state. See [**Human-in-the-Loop**](./human_in_the_loop.md) for more details. +:::python + +- **Pausing and Resuming Workflows:** Use the @[interrupt][interrupt] function to pause a workflow at specific points and the @[Command] primitive to resume it with updated state. See [**Human-in-the-Loop**](./human_in_the_loop.md) for more details. - **Recovering from Failures:** Automatically resume workflows from the last successful checkpoint after an exception (e.g., LLM provider outage). This involves executing the workflow with the same thread identifier by providing it with a `None` as the input value (see this [example](../how-tos/use-functional-api.md#resuming-after-an-error) with the functional API). + ::: + +:::js + +- **Pausing and Resuming Workflows:** Use the @[interrupt][interrupt] function to pause a workflow at specific points and the @[Command] primitive to resume it with updated state. See [**Human-in-the-Loop**](./human_in_the_loop.md) for more details. +- **Recovering from Failures:** Automatically resume workflows from the last successful checkpoint after an exception (e.g., LLM provider outage). This involves executing the workflow with the same thread identifier by providing it with a `null` as the input value (see this [example](../how-tos/use-functional-api.md#resuming-after-an-error) with the functional API). + ::: ## Starting Points for Resuming Workflows -* If you're using a [StateGraph (Graph API)][langgraph.graph.state.StateGraph], the starting point is the beginning of the [**node**](./low_level.md#nodes) where execution stopped. -* If you're making a subgraph call inside a node, the starting point will be the **parent** node that called the subgraph that was halted. -Inside the subgraph, the starting point will be the specific [**node**](./low_level.md#nodes) where execution stopped. -* If you're using the Functional API, the starting point is the beginning of the [**entrypoint**](./functional_api.md#entrypoint) where execution stopped. \ No newline at end of file +:::python + +- If you're using a @[StateGraph (Graph API)][StateGraph], the starting point is the beginning of the [**node**](./low_level.md#nodes) where execution stopped. +- If you're making a subgraph call inside a node, the starting point will be the **parent** node that called the subgraph that was halted. + Inside the subgraph, the starting point will be the specific [**node**](./low_level.md#nodes) where execution stopped. +- If you're using the Functional API, the starting point is the beginning of the [**entrypoint**](./functional_api.md#entrypoint) where execution stopped. + ::: + +:::js + +- If you're using a [StateGraph (Graph API)](./low_level.md), the starting point is the beginning of the [**node**](./low_level.md#nodes) where execution stopped. +- If you're making a subgraph call inside a node, the starting point will be the **parent** node that called the subgraph that was halted. + Inside the subgraph, the starting point will be the specific [**node**](./low_level.md#nodes) where execution stopped. +- If you're using the Functional API, the starting point is the beginning of the [**entrypoint**](./functional_api.md#entrypoint) where execution stopped. + ::: diff --git a/docs/docs/concepts/faq.md b/docs/docs/concepts/faq.md index 9ff68ff9f..97c703ca7 100644 --- a/docs/docs/concepts/faq.md +++ b/docs/docs/concepts/faq.md @@ -13,7 +13,7 @@ No. LangGraph is an orchestration framework for complex agentic systems and is m ## How is LangGraph different from other agent frameworks? -Other agentic frameworks can work for simple, generic tasks but fall short for complex tasks bespoke to a company’s needs. LangGraph provides a more expressive framework to handle companies’ unique tasks without restricting users to a single black-box cognitive architecture. +Other agentic frameworks can work for simple, generic tasks but fall short for complex tasks. LangGraph provides a more expressive framework to handle your unique tasks without restricting you to a single black-box cognitive architecture. ## Does LangGraph impact the performance of my app? @@ -28,14 +28,14 @@ Yes. LangGraph is an MIT-licensed open-source library and is free to use. LangGraph is a stateful, orchestration framework that brings added control to agent workflows. LangGraph Platform is a service for deploying and scaling LangGraph applications, with an opinionated API for building agent UXs, plus an integrated developer studio. | Features | LangGraph (open source) | LangGraph Platform | -|---------------------|-----------------------------------------------------------|--------------------------------------------------------------------------------------------------------| +| ------------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | | Description | Stateful orchestration framework for agentic applications | Scalable infrastructure for deploying LangGraph applications | | SDKs | Python and JavaScript | Python and JavaScript | | HTTP APIs | None | Yes - useful for retrieving & updating state or long-term memory, or creating a configurable assistant | | Streaming | Basic | Dedicated mode for token-by-token messages | | Checkpointer | Community contributed | Supported out-of-the-box | | Persistence Layer | Self-managed | Managed Postgres with efficient storage | -| Deployment | Self-managed | • Cloud SaaS <br> • Free self-hosted <br> • Enterprise (paid self-hosted) | +| Deployment | Self-managed | • Cloud SaaS <br> • Free self-hosted <br> • Enterprise (paid self-hosted) | | Scalability | Self-managed | Auto-scaling of task queues and servers | | Fault-tolerance | Self-managed | Automated retries | | Concurrency Control | Simple threading | Supports double-texting | @@ -67,4 +67,4 @@ If you set an environment variable of `LANGSMITH_TRACING=false`, then no traces ## What does "nodes executed" mean for LangGraph Platform usage? -**Nodes Executed** is the aggregate number of nodes in a LangGraph application that are called and completed successfully during an invocation of the application. If a node in the graph is not called during execution or ends in an error state, these nodes will not be counted. If a node is called and completes successfully multiple times, each occurrence will be counted. \ No newline at end of file +**Nodes Executed** is the aggregate number of nodes in a LangGraph application that are called and completed successfully during an invocation of the application. If a node in the graph is not called during execution or ends in an error state, these nodes will not be counted. If a node is called and completes successfully multiple times, each occurrence will be counted. diff --git a/docs/docs/concepts/functional_api.md b/docs/docs/concepts/functional_api.md index b43cba9e5..7e8f27efc 100644 --- a/docs/docs/concepts/functional_api.md +++ b/docs/docs/concepts/functional_api.md @@ -9,12 +9,21 @@ search: The **Functional API** allows you to add LangGraph's key features — [persistence](./persistence.md), [memory](../how-tos/memory/add-memory.md), [human-in-the-loop](./human_in_the_loop.md), and [streaming](./streaming.md) — to your applications with minimal changes to your existing code. -It is designed to integrate these features into existing code that may use standard language primitives for branching and control flow, such as `if` statements, `for` loops, and function calls. Unlike many data orchestration frameworks that require restructuring code into an explicit pipeline or DAG, the Functional API allows you to incorporate these capabilities without enforcing a rigid execution model. +It is designed to integrate these features into existing code that may use standard language primitives for branching and control flow, such as `if` statements, `for` loops, and function calls. Unlike many data orchestration frameworks that require restructuring code into an explicit pipeline or DAG, the Functional API allows you to incorporate these capabilities without enforcing a rigid execution model. -The Functional API uses two key building blocks: +The Functional API uses two key building blocks: -- **`@entrypoint`** – Marks a function as the starting point of a workflow, encapsulating logic and managing execution flow, including handling long-running tasks and interrupts. +:::python + +- **`@entrypoint`** – Marks a function as the starting point of a workflow, encapsulating logic and managing execution flow, including handling long-running tasks and interrupts. - **`@task`** – Represents a discrete unit of work, such as an API call or data processing step, that can be executed asynchronously within an entrypoint. Tasks return a future-like object that can be awaited or resolved synchronously. + ::: + +:::js + +- **`entrypoint`** – An entrypoint encapsulates workflow logic and manages execution flow, including handling long-running tasks and interrupts. +- **`task`** – Represents a discrete unit of work, such as an API call or data processing step, that can be executed asynchronously within an entrypoint. Tasks return a future-like object that can be awaited or resolved synchronously. + ::: This provides a minimal abstraction for building workflows with state management and streaming. @@ -33,17 +42,17 @@ Here are some key differences: - **Checkpointing**: Both APIs generate and use checkpoints. In the **Graph API** a new checkpoint is generated after every [superstep](./low_level.md). In the **Functional API**, when tasks are executed, their results are saved to an existing checkpoint associated with the given entrypoint instead of creating a new checkpoint. - **Visualization**: The Graph API makes it easy to visualize the workflow as a graph which can be useful for debugging, understanding the workflow, and sharing with others. The Functional API does not support visualization as the graph is dynamically generated during runtime. - ## Example Below we demonstrate a simple application that writes an essay and [interrupts](human_in_the_loop.md) to request human review. +:::python + ```python from langgraph.checkpoint.memory import InMemorySaver from langgraph.func import entrypoint, task from langgraph.types import interrupt - @task def write_essay(topic: str) -> str: """Write an essay about the given topic.""" @@ -70,12 +79,50 @@ def workflow(topic: str) -> dict: } ``` +::: + +:::js + +```typescript +import { MemorySaver, entrypoint, task, interrupt } from "@langchain/langgraph"; + +const writeEssay = task("writeEssay", async (topic: string) => { + // A placeholder for a long-running task. + await new Promise((resolve) => setTimeout(resolve, 1000)); + return `An essay about topic: ${topic}`; +}); + +const workflow = entrypoint( + { checkpointer: new MemorySaver(), name: "workflow" }, + async (topic: string) => { + const essay = await writeEssay(topic); + const isApproved = interrupt({ + // Any json-serializable payload provided to interrupt as argument. + // It will be surfaced on the client side as an Interrupt when streaming data + // from the workflow. + essay, // The essay we want reviewed. + // We can add any additional information that we need. + // For example, introduce a key called "action" with some instructions. + action: "Please approve/reject the essay", + }); + + return { + essay, // The essay that was generated + isApproved, // Response from HIL + }; + } +); +``` + +::: + ??? example "Detailed Explanation" This workflow will write an essay about the topic "cat" and then pause to get a review from a human. The workflow can be interrupted for an indefinite amount of time until a review is provided. - When the workflow is resumed, it executes from the very start, but because the result of the `write_essay` task was already saved, the task result will be loaded from the checkpoint instead of being recomputed. + When the workflow is resumed, it executes from the very start, but because the result of the `writeEssay` task was already saved, the task result will be loaded from the checkpoint instead of being recomputed. + :::python ```python import time import uuid @@ -147,18 +194,104 @@ def workflow(topic: str) -> dict: ``` The workflow has been completed and the review has been added to the essay. + ::: + + :::js + ```typescript + import { v4 as uuidv4 } from "uuid"; + import { MemorySaver, entrypoint, task, interrupt } from "@langchain/langgraph"; + + const writeEssay = task("writeEssay", async (topic: string) => { + // This is a placeholder for a long-running task. + await new Promise(resolve => setTimeout(resolve, 1000)); + return `An essay about topic: ${topic}`; + }); + + const workflow = entrypoint( + { checkpointer: new MemorySaver(), name: "workflow" }, + async (topic: string) => { + const essay = await writeEssay(topic); + const isApproved = interrupt({ + // Any json-serializable payload provided to interrupt as argument. + // It will be surfaced on the client side as an Interrupt when streaming data + // from the workflow. + essay, // The essay we want reviewed. + // We can add any additional information that we need. + // For example, introduce a key called "action" with some instructions. + action: "Please approve/reject the essay", + }); + + return { + essay, // The essay that was generated + isApproved, // Response from HIL + }; + } + ); + + const threadId = uuidv4(); + + const config = { + configurable: { + thread_id: threadId + } + }; + + for await (const item of workflow.stream("cat", config)) { + console.log(item); + } + ``` + + ```console + { writeEssay: 'An essay about topic: cat' } + { + __interrupt__: [{ + value: { essay: 'An essay about topic: cat', action: 'Please approve/reject the essay' }, + resumable: true, + ns: ['workflow:f7b8508b-21c0-8b4c-5958-4e8de74d2684'], + when: 'during' + }] + } + ``` + + An essay has been written and is ready for review. Once the review is provided, we can resume the workflow: + + ```typescript + import { Command } from "@langchain/langgraph"; + + // Get review from a user (e.g., via a UI) + // In this case, we're using a bool, but this can be any json-serializable value. + const humanReview = true; + + for await (const item of workflow.stream(new Command({ resume: humanReview }), config)) { + console.log(item); + } + ``` + + ```console + { workflow: { essay: 'An essay about topic: cat', isApproved: true } } + ``` + + The workflow has been completed and the review has been added to the essay. + ::: ## Entrypoint -The [`@entrypoint`][langgraph.func.entrypoint] decorator can be used to create a workflow from a function. It encapsulates workflow logic and manages execution flow, including handling *long-running tasks* and [interrupts](./human_in_the_loop.md). +:::python +The @[`@entrypoint`][entrypoint] decorator can be used to create a workflow from a function. It encapsulates workflow logic and manages execution flow, including handling _long-running tasks_ and [interrupts](./human_in_the_loop.md). +::: + +:::js +The @[`entrypoint`][entrypoint] function can be used to create a workflow from a function. It encapsulates workflow logic and manages execution flow, including handling _long-running tasks_ and [interrupts](./human_in_the_loop.md). +::: ### Definition -An **entrypoint** is defined by decorating a function with the `@entrypoint` decorator. +:::python +An **entrypoint** is defined by decorating a function with the `@entrypoint` decorator. The function **must accept a single positional argument**, which serves as the workflow input. If you need to pass multiple pieces of data, use a dictionary as the input type for the first argument. -Decorating a function with an `entrypoint` produces a [`Pregel`][langgraph.pregel.Pregel.stream] instance which helps to manage the execution of the workflow (e.g., handles streaming, resumption, and checkpointing). +Decorating a function with an `entrypoint` produces a @[`Pregel`][Pregel.stream] instance which helps to manage the execution of the workflow (e.g., handles streaming, resumption, and checkpointing). You will usually want to pass a **checkpointer** to the `@entrypoint` decorator to enable persistence and use features like **human-in-the-loop**. @@ -185,22 +318,48 @@ You will usually want to pass a **checkpointer** to the `@entrypoint` decorator # some logic that may involve long-running tasks like API calls, # and may be interrupted for human-in-the-loop ... - return result + return result ``` +::: + +:::js +An **entrypoint** is defined by calling the `entrypoint` function with configuration and a function. + +The function **must accept a single positional argument**, which serves as the workflow input. If you need to pass multiple pieces of data, use an object as the input type for the first argument. + +Creating an entrypoint with a function produces a workflow instance which helps to manage the execution of the workflow (e.g., handles streaming, resumption, and checkpointing). + +You will often want to pass a **checkpointer** to the `entrypoint` function to enable persistence and use features like **human-in-the-loop**. + +```typescript +import { entrypoint } from "@langchain/langgraph"; + +const myWorkflow = entrypoint( + { checkpointer, name: "workflow" }, + async (someInput: Record<string, any>): Promise<number> => { + // some logic that may involve long-running tasks like API calls, + // and may be interrupted for human-in-the-loop + return result; + } +); +``` + +::: + !!! important "Serialization" The **inputs** and **outputs** of entrypoints must be JSON-serializable to support checkpointing. Please see the [serialization](#serialization) section for more details. +:::python ### Injectable parameters When declaring an `entrypoint`, you can request access to additional parameters that will be injected automatically at run time. These parameters include: - | Parameter | Description | -|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| **previous** | Access the state associated with the previous `checkpoint` for the given thread. See [short-term-memory](#short-term-memory). | +| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| **previous** | Access the state associated with the previous `checkpoint` for the given thread. See [short-term-memory](#short-term-memory). | | **store** | An instance of [BaseStore][langgraph.store.base.BaseStore]. Useful for [long-term memory](../how-tos/use-functional-api.md#long-term-memory). | | **writer** | Use to access the StreamWriter when working with Async Python < 3.11. See [streaming with functional API for details](../how-tos/use-functional-api.md#streaming). | | **config** | For accessing run time configuration. See [RunnableConfig](https://python.langchain.com/docs/concepts/runnables/#runnableconfig) for information. | @@ -222,7 +381,7 @@ When declaring an `entrypoint`, you can request access to additional parameters @entrypoint( checkpointer=checkpointer, # Specify the checkpointer store=in_memory_store # Specify the store - ) + ) def my_workflow( some_input: dict, # The input (e.g., passed via `invoke`) *, @@ -233,9 +392,12 @@ When declaring an `entrypoint`, you can request access to additional parameters ) -> ...: ``` +::: + ### Executing -Using the [`@entrypoint`](#entrypoint) yields a [`Pregel`][langgraph.pregel.Pregel.stream] object that can be executed using the `invoke`, `ainvoke`, `stream`, and `astream` methods. +:::python +Using the [`@entrypoint`](#entrypoint) yields a @[`Pregel`][Pregel.stream] object that can be executed using the `invoke`, `ainvoke`, `stream`, and `astream` methods. === "Invoke" @@ -260,7 +422,7 @@ Using the [`@entrypoint`](#entrypoint) yields a [`Pregel`][langgraph.pregel.Preg ``` === "Stream" - + ```python config = { "configurable": { @@ -285,9 +447,42 @@ Using the [`@entrypoint`](#entrypoint) yields a [`Pregel`][langgraph.pregel.Preg print(chunk) ``` +::: + +:::js +Using the [`entrypoint`](#entrypoint) function will return an object that can be executed using the `invoke` and `stream` methods. + +=== "Invoke" + + ```typescript + const config = { + configurable: { + thread_id: "some_thread_id" + } + }; + await myWorkflow.invoke(someInput, config); // Wait for the result + ``` + +=== "Stream" + + ```typescript + const config = { + configurable: { + thread_id: "some_thread_id" + } + }; + + for await (const chunk of myWorkflow.stream(someInput, config)) { + console.log(chunk); + } + ``` + +::: + ### Resuming -Resuming an execution after an [interrupt][langgraph.types.interrupt] can be done by passing a **resume** value to the [Command][langgraph.types.Command] primitive. +:::python +Resuming an execution after an @[interrupt][interrupt] can be done by passing a **resume** value to the @[Command] primitive. === "Invoke" @@ -299,7 +494,7 @@ Resuming an execution after an [interrupt][langgraph.types.interrupt] can be don "thread_id": "some_thread_id" } } - + my_workflow.invoke(Command(resume=some_resume_value), config) ``` @@ -313,7 +508,7 @@ Resuming an execution after an [interrupt][langgraph.types.interrupt] can be don "thread_id": "some_thread_id" } } - + await my_workflow.ainvoke(Command(resume=some_resume_value), config) ``` @@ -327,7 +522,7 @@ Resuming an execution after an [interrupt][langgraph.types.interrupt] can be don "thread_id": "some_thread_id" } } - + for chunk in my_workflow.stream(Command(resume=some_resume_value), config): print(chunk) ``` @@ -347,8 +542,51 @@ Resuming an execution after an [interrupt][langgraph.types.interrupt] can be don print(chunk) ``` -**Resuming after an error** +::: +:::js +Resuming an execution after an @[interrupt][interrupt] can be done by passing a **resume** value to the @[`Command`][Command] primitive. + +=== "Invoke" + + ```typescript + import { Command } from "@langchain/langgraph"; + + const config = { + configurable: { + thread_id: "some_thread_id" + } + }; + + await myWorkflow.invoke(new Command({ resume: someResumeValue }), config); + ``` + +=== "Stream" + + ```typescript + import { Command } from "@langchain/langgraph"; + + const config = { + configurable: { + thread_id: "some_thread_id" + } + }; + + const stream = await myWorkflow.stream( + new Command({ resume: someResumableValue }), + config, + ) + + for await (const chunk of stream) { + console.log(chunk); + } + ``` + +::: + +:::python + +**Resuming after an error** To resume after an error, run the `entrypoint` with a `None` and the same **thread id** (config). @@ -363,7 +601,7 @@ This assumes that the underlying **error** has been resolved and execution can p "thread_id": "some_thread_id" } } - + my_workflow.invoke(None, config) ``` @@ -376,7 +614,7 @@ This assumes that the underlying **error** has been resolved and execution can p "thread_id": "some_thread_id" } } - + await my_workflow.ainvoke(None, config) ``` @@ -389,7 +627,7 @@ This assumes that the underlying **error** has been resolved and execution can p "thread_id": "some_thread_id" } } - + for chunk in my_workflow.stream(None, config): print(chunk) ``` @@ -408,10 +646,49 @@ This assumes that the underlying **error** has been resolved and execution can p print(chunk) ``` +::: + +:::js + +**Resuming after an error** + +To resume after an error, run the `entrypoint` with `null` and the same **thread id** (config). + +This assumes that the underlying **error** has been resolved and execution can proceed successfully. + +=== "Invoke" + + ```typescript + const config = { + configurable: { + thread_id: "some_thread_id" + } + }; + + await myWorkflow.invoke(null, config); + ``` + +=== "Stream" + + ```typescript + const config = { + configurable: { + thread_id: "some_thread_id" + } + }; + + for await (const chunk of myWorkflow.stream(null, config)) { + console.log(chunk); + } + ``` + +::: + ### Short-term memory -When an `entrypoint` is defined with a `checkpointer`, it stores information between successive invocations on the same **thread id** in [checkpoints](persistence.md#checkpoints). +When an `entrypoint` is defined with a `checkpointer`, it stores information between successive invocations on the same **thread id** in [checkpoints](persistence.md#checkpoints). +:::python This allows accessing the state from the previous invocation using the `previous` parameter. By default, the `previous` parameter is the return value of the previous invocation. @@ -432,9 +709,40 @@ my_workflow.invoke(1, config) # 1 (previous was None) my_workflow.invoke(2, config) # 3 (previous was 1 from the previous invocation) ``` +::: + +:::js +This allows accessing the state from the previous invocation using the `getPreviousState` function. + +By default, the `getPreviousState` function returns the return value of the previous invocation. + +```typescript +import { entrypoint, getPreviousState } from "@langchain/langgraph"; + +const myWorkflow = entrypoint( + { checkpointer, name: "workflow" }, + async (number: number) => { + const previous = getPreviousState<number>() ?? 0; + return number + previous; + } +); + +const config = { + configurable: { + thread_id: "some_thread_id", + }, +}; + +await myWorkflow.invoke(1, config); // 1 (previous was undefined) +await myWorkflow.invoke(2, config); // 3 (previous was 1 from the previous invocation) +``` + +::: + #### `entrypoint.final` -[entrypoint.final][langgraph.func.entrypoint.final] is a special primitive that can be returned from an entrypoint and allows **decoupling** the value that is **saved in the checkpoint** from the **return value of the entrypoint**. +:::python +@[`entrypoint.final`][entrypoint.final] is a special primitive that can be returned from an entrypoint and allows **decoupling** the value that is **saved in the checkpoint** from the **return value of the entrypoint**. The first value is the return value of the entrypoint, and the second value is the value that will be saved in the checkpoint. The type annotation is `entrypoint.final[return_type, save_type]`. @@ -443,7 +751,7 @@ The first value is the return value of the entrypoint, and the second value is t def my_workflow(number: int, *, previous: Any = None) -> entrypoint.final[int, int]: previous = previous or 0 # This will return the previous value to the caller, saving - # 2 * number to the checkpoint, which will be used in the next invocation + # 2 * number to the checkpoint, which will be used in the next invocation # for the `previous` parameter. return entrypoint.final(value=previous, save=2 * number) @@ -457,15 +765,52 @@ my_workflow.invoke(3, config) # 0 (previous was None) my_workflow.invoke(1, config) # 6 (previous was 3 * 2 from the previous invocation) ``` +::: + +:::js +@[`entrypoint.final`][entrypoint.final] is a special primitive that can be returned from an entrypoint and allows **decoupling** the value that is **saved in the checkpoint** from the **return value of the entrypoint**. + +The first value is the return value of the entrypoint, and the second value is the value that will be saved in the checkpoint. + +```typescript +import { entrypoint, getPreviousState } from "@langchain/langgraph"; + +const myWorkflow = entrypoint( + { checkpointer, name: "workflow" }, + async (number: number) => { + const previous = getPreviousState<number>() ?? 0; + // This will return the previous value to the caller, saving + // 2 * number to the checkpoint, which will be used in the next invocation + // for the `previous` parameter. + return entrypoint.final({ + value: previous, + save: 2 * number, + }); + } +); + +const config = { + configurable: { + thread_id: "1", + }, +}; + +await myWorkflow.invoke(3, config); // 0 (previous was undefined) +await myWorkflow.invoke(1, config); // 6 (previous was 3 * 2 from the previous invocation) +``` + +::: + ## Task A **task** represents a discrete unit of work, such as an API call or data processing step. It has two key characteristics: -* **Asynchronous Execution**: Tasks are designed to be executed asynchronously, allowing multiple operations to run concurrently without blocking. -* **Checkpointing**: Task results are saved to a checkpoint, enabling resumption of the workflow from the last saved state. (See [persistence](persistence.md) for more details). +- **Asynchronous Execution**: Tasks are designed to be executed asynchronously, allowing multiple operations to run concurrently without blocking. +- **Checkpointing**: Task results are saved to a checkpoint, enabling resumption of the workflow from the last saved state. (See [persistence](persistence.md) for more details). ### Definition +:::python Tasks are defined using the `@task` decorator, which wraps a regular Python function. ```python @@ -478,21 +823,37 @@ def slow_computation(input_value): return result ``` +::: + +:::js +Tasks are defined using the `task` function, which wraps a regular function. + +```typescript +import { task } from "@langchain/langgraph"; + +const slowComputation = task("slowComputation", async (inputValue: any) => { + // Simulate a long-running operation + return result; +}); +``` + +::: + !!! important "Serialization" The **outputs** of tasks must be JSON-serializable to support checkpointing. ### Execution -**Tasks** can only be called from within an **entrypoint**, another **task**, or a [state graph node](./low_level.md#nodes). +**Tasks** can only be called from within an **entrypoint**, another **task**, or a [state graph node](./low_level.md#nodes). -Tasks *cannot* be called directly from the main application code. +Tasks _cannot_ be called directly from the main application code. -When you call a **task**, it returns *immediately* with a future object. A future is a placeholder for a result that will be available later. +:::python +When you call a **task**, it returns _immediately_ with a future object. A future is a placeholder for a result that will be available later. To obtain the result of a **task**, you can either wait for it synchronously (using `result()`) or await it asynchronously (using `await`). - === "Synchronous Invocation" ```python @@ -510,6 +871,22 @@ To obtain the result of a **task**, you can either wait for it synchronously (us return await slow_computation(some_input) # Await result asynchronously ``` +::: + +:::js +When you call a **task**, it returns a Promise that can be awaited. + +```typescript +const myWorkflow = entrypoint( + { checkpointer, name: "workflow" }, + async (someInput: number): Promise<number> => { + return await slowComputation(someInput); + } +); +``` + +::: + ## When to use a task **Tasks** are useful in the following scenarios: @@ -519,16 +896,21 @@ To obtain the result of a **task**, you can either wait for it synchronously (us - **Parallel Execution**: For I/O-bound tasks, **tasks** enable parallel execution, allowing multiple operations to run concurrently without blocking (e.g., calling multiple APIs). - **Observability**: Wrapping operations in **tasks** provides a way to track the progress of the workflow and monitor the execution of individual operations using [LangSmith](https://docs.smith.langchain.com/). - **Retryable Work**: When work needs to be retried to handle failures or inconsistencies, **tasks** provide a way to encapsulate and manage the retry logic. - + ## Serialization There are two key aspects to serialization in LangGraph: -1. `@entrypoint` inputs and outputs must be JSON-serializable. -2. `@task` outputs must be JSON-serializable. +1. `entrypoint` inputs and outputs must be JSON-serializable. +2. `task` outputs must be JSON-serializable. -These requirements are necessary for enabling checkpointing and workflow resumption. Use python primitives -like dictionaries, lists, strings, numbers, and booleans to ensure that your inputs and outputs are serializable. +:::python +These requirements are necessary for enabling checkpointing and workflow resumption. Use python primitives like dictionaries, lists, strings, numbers, and booleans to ensure that your inputs and outputs are serializable. +::: + +:::js +These requirements are necessary for enabling checkpointing and workflow resumption. Use primitives like objects, arrays, strings, numbers, and booleans to ensure that your inputs and outputs are serializable. +::: Serialization ensures that workflow state, such as task results and intermediate values, can be reliably saved and restored. This is critical for enabling human-in-the-loop interactions, fault tolerance, and parallel execution. @@ -536,9 +918,9 @@ Providing non-serializable inputs or outputs will result in a runtime error when ## Determinism -To utilize features like **human-in-the-loop**, any randomness should be encapsulated inside of **tasks**. This guarantees that when execution is halted (e.g., for human in the loop) and then resumed, it will follow the same *sequence of steps*, even if **task** results are non-deterministic. +To utilize features like **human-in-the-loop**, any randomness should be encapsulated inside of **tasks**. This guarantees that when execution is halted (e.g., for human in the loop) and then resumed, it will follow the same _sequence of steps_, even if **task** results are non-deterministic. -LangGraph achieves this behavior by persisting **task** and [**subgraph**](./subgraphs.md) results as they execute. A well-designed workflow ensures that resuming execution follows the *same sequence of steps*, allowing previously computed results to be retrieved correctly without having to re-execute them. This is particularly useful for long-running **tasks** or **tasks** with non-deterministic results, as it avoids repeating previously done work and allows resuming from essentially the same. +LangGraph achieves this behavior by persisting **task** and [**subgraph**](./subgraphs.md) results as they execute. A well-designed workflow ensures that resuming execution follows the _same sequence of steps_, allowing previously computed results to be retrieved correctly without having to re-execute them. This is particularly useful for long-running **tasks** or **tasks** with non-deterministic results, as it avoids repeating previously done work and allows resuming from essentially the same. While different runs of a workflow can produce different results, resuming a **specific** run should always follow the same sequence of recorded steps. This allows LangGraph to efficiently look up **task** and **subgraph** results that were executed prior to the graph being interrupted and avoid recomputing them. @@ -556,6 +938,7 @@ Encapsulate side effects (e.g., writing to a file, sending an email) in tasks to In this example, a side effect (writing to a file) is directly included in the workflow, so it will be executed a second time when resuming the workflow. + :::python ```python @entrypoint(checkpointer=checkpointer) def my_workflow(inputs: dict) -> int: @@ -568,11 +951,31 @@ Encapsulate side effects (e.g., writing to a file, sending an email) in tasks to value = interrupt("question") return value ``` + ::: + + :::js + ```typescript + import { entrypoint, interrupt } from "@langchain/langgraph"; + import fs from "fs"; + + const myWorkflow = entrypoint( + { checkpointer, name: "workflow }, + async (inputs: Record<string, any>) => { + // This code will be executed a second time when resuming the workflow. + // Which is likely not what you want. + fs.writeFileSync("output.txt", "Side effect executed"); + const value = interrupt("question"); + return value; + } + ); + ``` + ::: === "Correct" In this example, the side effect is encapsulated in a task, ensuring consistent execution upon resumption. + :::python ```python from langgraph.func import task @@ -590,17 +993,43 @@ Encapsulate side effects (e.g., writing to a file, sending an email) in tasks to value = interrupt("question") return value ``` + ::: + + :::js + ```typescript + import { entrypoint, task, interrupt } from "@langchain/langgraph"; + import * as fs from "fs"; + + const writeToFile = task("writeToFile", async () => { + fs.writeFileSync("output.txt", "Side effect executed"); + }); + + const myWorkflow = entrypoint( + { checkpointer, name: "workflow" }, + async (inputs: Record<string, any>) => { + // The side effect is now encapsulated in a task. + await writeToFile(); + const value = interrupt("question"); + return value; + } + ); + ``` + ::: ### Non-deterministic control flow Operations that might give different results each time (like getting current time or random numbers) should be encapsulated in tasks to ensure that on resume, the same result is returned. -* In a task: Get random number (5) → interrupt → resume → (returns 5 again) → ... -* Not in a task: Get random number (5) → interrupt → resume → get new random number (7) → ... +- In a task: Get random number (5) → interrupt → resume → (returns 5 again) → ... +- Not in a task: Get random number (5) → interrupt → resume → get new random number (7) → ... -This is especially important when using **human-in-the-loop** workflows with multiple interrupts calls. LangGraph keeps a list -of resume values for each task/entrypoint. When an interrupt is encountered, it's matched with the corresponding resume value. -This matching is strictly **index-based**, so the order of the resume values should match the order of the interrupts. +:::python +This is especially important when using **human-in-the-loop** workflows with multiple interrupts calls. LangGraph keeps a list of resume values for each task/entrypoint. When an interrupt is encountered, it's matched with the corresponding resume value. This matching is strictly **index-based**, so the order of the resume values should match the order of the interrupts. +::: + +:::js +This is especially important when using **human-in-the-loop** workflows with multiple interrupt calls. LangGraph keeps a list of resume values for each task/entrypoint. When an interrupt is encountered, it's matched with the corresponding resume value. This matching is strictly **index-based**, so the order of the resume values should match the order of the interrupts. +::: If order of execution is not maintained when resuming, one `interrupt` call may be matched with the wrong `resume` value, leading to incorrect results. @@ -610,6 +1039,7 @@ Please read the section on [determinism](#determinism) for more details. In this example, the workflow uses the current time to determine which task to execute. This is non-deterministic because the result of the workflow depends on the time at which it is executed. + :::python ```python from langgraph.func import entrypoint @@ -618,24 +1048,51 @@ Please read the section on [determinism](#determinism) for more details. t0 = inputs["t0"] # highlight-next-line t1 = time.time() - + delta_t = t1 - t0 - + if delta_t > 1: result = slow_task(1).result() value = interrupt("question") else: result = slow_task(2).result() value = interrupt("question") - + return { "result": result, "value": value } ``` + ::: + + :::js + ```typescript + import { entrypoint, interrupt } from "@langchain/langgraph"; + + const myWorkflow = entrypoint( + { checkpointer, name: "workflow" }, + async (inputs: { t0: number }) => { + const t1 = Date.now(); + + const deltaT = t1 - inputs.t0; + + if (deltaT > 1000) { + const result = await slowTask(1); + const value = interrupt("question"); + return { result, value }; + } else { + const result = await slowTask(2); + const value = interrupt("question"); + return { result, value }; + } + } + ); + ``` + ::: === "Correct" + :::python In this example, the workflow uses the input `t0` to determine which task to execute. This is deterministic because the result of the workflow depends only on the input. ```python @@ -654,19 +1111,48 @@ Please read the section on [determinism](#determinism) for more details. t0 = inputs["t0"] # highlight-next-line t1 = get_time().result() - + delta_t = t1 - t0 - + if delta_t > 1: result = slow_task(1).result() value = interrupt("question") else: result = slow_task(2).result() value = interrupt("question") - + return { "result": result, "value": value } ``` + ::: + :::js + In this example, the workflow uses the input `t0` to determine which task to execute. This is deterministic because the result of the workflow depends only on the input. + + ```typescript + import { entrypoint, task, interrupt } from "@langchain/langgraph"; + + const getTime = task("getTime", () => Date.now()); + + const myWorkflow = entrypoint( + { checkpointer, name: "workflow" }, + async (inputs: { t0: number }): Promise<any> => { + const t1 = await getTime(); + + const deltaT = t1 - inputs.t0; + + if (deltaT > 1000) { + const result = await slowTask(1); + const value = interrupt("question"); + return { result, value }; + } else { + const result = await slowTask(2); + const value = interrupt("question"); + return { result, value }; + } + } + ); + ``` + ::: diff --git a/docs/docs/concepts/langgraph_cli.md b/docs/docs/concepts/langgraph_cli.md index 888568d42..e98e33e17 100644 --- a/docs/docs/concepts/langgraph_cli.md +++ b/docs/docs/concepts/langgraph_cli.md @@ -7,29 +7,66 @@ search: **LangGraph CLI** is a multi-platform command-line tool for building and running the [LangGraph API server](./langgraph_server.md) locally. The resulting server includes all API endpoints for your graph's runs, threads, assistants, etc. as well as the other services required to run your agent, including a managed database for checkpointing and storage. +:::python + ## Installation The LangGraph CLI can be installed via pip or [Homebrew](https://brew.sh/): -=== "pip" +=== "pip" + ```bash pip install langgraph-cli ``` === "Homebrew" + ```bash brew install langgraph-cli ``` +::: + +:::js + +## Installation + +The LangGraph.js CLI can be installed from the NPM registry: + +=== "npx" + ```bash + npx @langchain/langgraph-cli + ``` + +=== "npm" + ```bash + npm install @langchain/langgraph-cli + ``` + +=== "yarn" + ```bash + yarn add @langchain/langgraph-cli + ``` + +=== "pnpm" + ```bash + pnpm add @langchain/langgraph-cli + ``` + +=== "bun" + ```bash + bun add @langchain/langgraph-cli + ``` +::: ## Commands LangGraph CLI provides the following core functionality: -| Command | Description | -| -------- | -------| -| [`langgraph build`](../cloud/reference/cli.md#build) | Builds a Docker image for the [LangGraph API server](./langgraph_server.md) that can be directly deployed. | -| [`langgraph dev`](../cloud/reference/cli.md#dev) | Starts a lightweight development server that requires no Docker installation. This server is ideal for rapid development and testing. This is available in version 0.1.55 and up. +| Command | Description | +| -------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [`langgraph build`](../cloud/reference/cli.md#build) | Builds a Docker image for the [LangGraph API server](./langgraph_server.md) that can be directly deployed. | +| [`langgraph dev`](../cloud/reference/cli.md#dev) | Starts a lightweight development server that requires no Docker installation. This server is ideal for rapid development and testing. | | [`langgraph dockerfile`](../cloud/reference/cli.md#dockerfile) | Generates a [Dockerfile](https://docs.docker.com/reference/dockerfile/) that can be used to build images for and deploy instances of the [LangGraph API server](./langgraph_server.md). This is useful if you want to further customize the dockerfile or deploy in a more custom way. | -| [`langgraph up`](../cloud/reference/cli.md#up) | Starts an instance of the [LangGraph API server](./langgraph_server.md) locally in a docker container. This requires the docker server to be running locally. It also requires a LangSmith API key for local development or a license key for production use. | +| [`langgraph up`](../cloud/reference/cli.md#up) | Starts an instance of the [LangGraph API server](./langgraph_server.md) locally in a docker container. This requires the docker server to be running locally. It also requires a LangSmith API key for local development or a license key for production use. | For more information, see the [LangGraph CLI Reference](../cloud/reference/cli.md). diff --git a/docs/docs/concepts/langgraph_cloud.md b/docs/docs/concepts/langgraph_cloud.md index b9c835913..b8b2a3de5 100644 --- a/docs/docs/concepts/langgraph_cloud.md +++ b/docs/docs/concepts/langgraph_cloud.md @@ -11,11 +11,11 @@ To deploy a [LangGraph Server](../concepts/langgraph_server.md), follow the how- The Cloud SaaS deployment option is a fully managed model for deployment where we manage the [control plane](./langgraph_control_plane.md) and [data plane](./langgraph_data_plane.md) in our cloud. -| | [Control plane](../concepts/langgraph_control_plane.md) | [Data plane](../concepts/langgraph_data_plane.md) | -|-------------------|-------------------|------------| -| **What is it?** | <ul><li>Control plane UI for creating deployments and revisions</li><li>Control plane APIs for creating deployments and revisions</li></ul> | <ul><li>Data plane "listener" for reconciling deployments with control plane state</li><li>LangGraph Servers</li><li>Postgres, Redis, etc</li></ul> | -| **Where is it hosted?** | LangChain's cloud | LangChain's cloud | -| **Who provisions and manages it?** | LangChain | LangChain | +| | [Control plane](../concepts/langgraph_control_plane.md) | [Data plane](../concepts/langgraph_data_plane.md) | +| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| **What is it?** | <ul><li>Control plane UI for creating deployments and revisions</li><li>Control plane APIs for creating deployments and revisions</li></ul> | <ul><li>Data plane "listener" for reconciling deployments with control plane state</li><li>LangGraph Servers</li><li>Postgres, Redis, etc</li></ul> | +| **Where is it hosted?** | LangChain's cloud | LangChain's cloud | +| **Who provisions and manages it?** | LangChain | LangChain | ## Architecture diff --git a/docs/docs/concepts/langgraph_components.md b/docs/docs/concepts/langgraph_components.md index 78b9e7c8d..9ae6ff57c 100644 --- a/docs/docs/concepts/langgraph_components.md +++ b/docs/docs/concepts/langgraph_components.md @@ -10,4 +10,4 @@ The LangGraph Platform consists of components that work together to support the - [LangGraph control plane](./langgraph_control_plane.md): The LangGraph Control Plane refers to the Control Plane UI where users create and update LangGraph Servers and the Control Plane APIs that support the UI experience. - [LangGraph data plane](./langgraph_data_plane.md): The LangGraph Data Plane refers to LangGraph Servers, the corresponding infrastructure for each server, and the "listener" application that continuously polls for updates from the LangGraph Control Plane. -![LangGraph components](img/lg_platform.png) \ No newline at end of file +![LangGraph components](img/lg_platform.png) diff --git a/docs/docs/concepts/langgraph_control_plane.md b/docs/docs/concepts/langgraph_control_plane.md index e689a1a79..6cd031870 100644 --- a/docs/docs/concepts/langgraph_control_plane.md +++ b/docs/docs/concepts/langgraph_control_plane.md @@ -44,8 +44,8 @@ This section describes various features of the control plane. For simplicity, the control plane offers two deployment types with different resource allocations: `Development` and `Production`. -| **Deployment Type** | **CPU/Memory** | **Scaling** | **Database** | -|---------------------|-----------------|---------------------|----------------------------------------------------------------------------------| +| **Deployment Type** | **CPU/Memory** | **Scaling** | **Database** | +| ------------------- | --------------- | ----------------- | -------------------------------------------------------------------------------- | | Development | 1 CPU, 1 GB RAM | Up to 1 replica | 10 GB disk, no backups | | Production | 2 CPU, 2 GB RAM | Up to 10 replicas | Autoscaling disk, automatic backups, highly available (multi-zone configuration) | @@ -56,7 +56,7 @@ CPU and memory resources are per replica. Once a deployment is created, the deployment type cannot be changed. !!! info "Self-Hosted Deployment" - Resources for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_data_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments can be fully customized. Deployment types are only applicable for [Cloud SaaS](../concepts/langgraph_cloud.md) deployments. +Resources for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_data_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments can be fully customized. Deployment types are only applicable for [Cloud SaaS](../concepts/langgraph_cloud.md) deployments. #### Production @@ -69,12 +69,12 @@ Resources for `Production` type deployments can be manually increased on a case- `Development` type deployments are suitable development and testing. For example, select `Development` for internal testing environments. `Development` type deployments are not suitable for "production" workloads. !!! danger "Preemptible Compute Infrastructure" - `Development` type deployments (API server, queue server, and database) are provisioned on preemptible compute infrastructure. This means the compute infrastructure **may be terminated at any time without notice**. This may result in intermittent... +`Development` type deployments (API server, queue server, and database) are provisioned on preemptible compute infrastructure. This means the compute infrastructure **may be terminated at any time without notice**. This may result in intermittent... - Redis connection timeouts/errors - Postgres connection timeouts/errors - Failed or retrying background runs - + This behavior is expected. Preemptible compute infrastructure **significantly reduces the cost to provision a `Development` type deployment**. By design, LangGraph Server is fault-tolerant. The implementation will automatically attempt to recover from Redis/Postgres connection errors and retry failed background runs. `Production` type deployments are provisioned on durable compute infrastructure, not preemptible compute infrastructure. @@ -92,7 +92,7 @@ There is no direct access to the database. All access to the database occurs thr The database is never deleted until the deployment itself is deleted. !!! info - A custom Postgres instance can be configured for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_data_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments. +A custom Postgres instance can be configured for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_data_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments. ### Asynchronous Deployment diff --git a/docs/docs/concepts/langgraph_data_plane.md b/docs/docs/concepts/langgraph_data_plane.md index 5d7473cb6..150759327 100644 --- a/docs/docs/concepts/langgraph_data_plane.md +++ b/docs/docs/concepts/langgraph_data_plane.md @@ -78,25 +78,25 @@ Scale down actions are delayed for 30 minutes before any action is taken. In oth ### Static IP Addresses !!! info "Only for Cloud SaaS" - Static IP addresses are only available for [Cloud SaaS](../concepts/langgraph_cloud.md) deployments. +Static IP addresses are only available for [Cloud SaaS](../concepts/langgraph_cloud.md) deployments. All traffic from deployments created after January 6th 2025 will come through a NAT gateway. This NAT gateway will have several static IP addresses depending on the data region. Refer to the table below for the list of static IP addresses: | US | EU | -|----------------|----------------| +| -------------- | -------------- | | 35.197.29.146 | 34.13.192.67 | | 34.145.102.123 | 34.147.105.64 | | 34.169.45.153 | 34.90.22.166 | | 34.82.222.17 | 34.147.36.213 | -| 35.227.171.135 | 34.32.137.113 | +| 35.227.171.135 | 34.32.137.113 | | 34.169.88.30 | 34.91.238.184 | | 34.19.93.202 | 35.204.101.241 | | 34.19.34.50 | 35.204.48.32 | ### Custom Postgres -!!! info - Custom Postgres instances are only available for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_data_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments. +!!! info +Custom Postgres instances are only available for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_data_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments. A custom Postgres instance can be used instead of the [one automatically created by the control plane](./langgraph_control_plane.md#database-provisioning). Specify the [`POSTGRES_URI_CUSTOM`](../cloud/reference/env_var.md#postgres_uri_custom) environment variable to use a custom Postgres instance. @@ -105,33 +105,32 @@ Multiple deployments can share the same Postgres instance. For example, for `Dep ### Custom Redis !!! info - Custom Redis instances are only available for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_control_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments. +Custom Redis instances are only available for [Self-Hosted Data Plane](../concepts/langgraph_self_hosted_control_plane.md) and [Self-Hosted Control Plane](../concepts/langgraph_self_hosted_control_plane.md) deployments. A custom Redis instance can be used instead of the one automatically created by the control plane. Specify the [REDIS_URI_CUSTOM](../cloud/reference/env_var.md#redis_uri_custom) environment variable to use a custom Redis instance. - Multiple deployments can share the same Redis instance. For example, for `Deployment A`, `REDIS_URI_CUSTOM` can be set to `redis://<hostname_1>:<port>/1` and for `Deployment B`, `REDIS_URI_CUSTOM` can be set to `redis://<hostname_1>:<port>/2`. `1` and `2` are different database numbers within the same instance, but `<hostname_1>` is shared. **The same database number cannot be used for separate deployments**. ### LangSmith Tracing LangGraph Server is automatically configured to send traces to LangSmith. See the table below for details with respect to each deployment option. -| Cloud SaaS | Self-Hosted Data Plane | Self-Hosted Control Plane | Standalone Container | -|------------|------------------------|---------------------------|----------------------| +| Cloud SaaS | Self-Hosted Data Plane | Self-Hosted Control Plane | Standalone Container | +| ---------------------------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------- | | Required<br><br>Trace to LangSmith SaaS. | Optional<br><br>Disable tracing or trace to LangSmith SaaS. | Optional<br><br>Disable tracing or trace to Self-Hosted LangSmith. | Optional<br><br>Disable tracing, trace to LangSmith SaaS, or trace to Self-Hosted LangSmith. | ### Telemetry LangGraph Server is automatically configured to report telemetry metadata for billing purposes. See the table below for details with respect to each deployment option. -| Cloud SaaS | Self-Hosted Data Plane | Self-Hosted Control Plane | Standalone Container | -|------------|------------------------|---------------------------|----------------------| +| Cloud SaaS | Self-Hosted Data Plane | Self-Hosted Control Plane | Standalone Container | +| --------------------------------- | --------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | | Telemetry sent to LangSmith SaaS. | Telemetry sent to LangSmith SaaS. | Self-reported usage (audit) for air-gapped license key.<br><br>Telemetry sent to LangSmith SaaS for LangGraph Platform License Key. | Self-reported usage (audit) for air-gapped license key.<br><br>Telemetry sent to LangSmith SaaS for LangGraph Platform License Key. | ### Licensing LangGraph Server is automatically configured to perform license key validation. See the table below for details with respect to each deployment option. -| Cloud SaaS | Self-Hosted Data Plane | Self-Hosted Control Plane | Standalone Container | -|------------|------------------------|---------------------------|----------------------| +| Cloud SaaS | Self-Hosted Data Plane | Self-Hosted Control Plane | Standalone Container | +| --------------------------------------------------- | --------------------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ | | LangSmith API Key validated against LangSmith SaaS. | LangSmith API Key validated against LangSmith SaaS. | Air-gapped license key or LangGraph Platform License Key validated against LangSmith SaaS. | Air-gapped license key or LangGraph Platform License Key validated against LangSmith SaaS. | diff --git a/docs/docs/concepts/langgraph_self_hosted_control_plane.md b/docs/docs/concepts/langgraph_self_hosted_control_plane.md index 02fef232d..88f691484 100644 --- a/docs/docs/concepts/langgraph_self_hosted_control_plane.md +++ b/docs/docs/concepts/langgraph_self_hosted_control_plane.md @@ -3,11 +3,12 @@ There are two versions of the self-hosted deployment: [Self-Hosted Data Plane](./deployment_options.md#self-hosted-data-plane) and [Self-Hosted Control Plane](./deployment_options.md#self-hosted-control-plane). !!! info "Important" + The Self-Hosted Control Plane deployment option requires an [Enterprise](plans.md) plan. ## Requirements -- You use `langgraph-cli` and/or [LangGraph Studio](./langgraph_studio.md) app to test graph locally. +- You use the [LangGraph CLI](./langgraph_cli.md) and/or [LangGraph Studio](./langgraph_studio.md) app to test graph locally. - You use `langgraph build` command to build image. - You have a Self-Hosted LangSmith instance deployed. - You are using Ingress for your LangSmith instance. All agents will be deployed as Kubernetes services behind this ingress. @@ -16,11 +17,11 @@ There are two versions of the self-hosted deployment: [Self-Hosted Data Plane](. The [Self-Hosted Control Plane](./langgraph_self_hosted_control_plane.md) deployment option is a fully self-hosted model for deployment where you manage the [control plane](./langgraph_control_plane.md) and [data plane](./langgraph_data_plane.md) in your cloud. This option gives you full control and responsibility of the control plane and data plane infrastructure. -| | [Control plane](../concepts/langgraph_control_plane.md) | [Data plane](../concepts/langgraph_data_plane.md) | -|-------------------|-------------------|------------| -| **What is it?** | <ul><li>Control plane UI for creating deployments and revisions</li><li>Control plane APIs for creating deployments and revisions</li></ul> | <ul><li>Data plane "listener" for reconciling deployments with control plane state</li><li>LangGraph Servers</li><li>Postgres, Redis, etc</li></ul> | -| **Where is it hosted?** | Your cloud | Your cloud | -| **Who provisions and manages it?** | You | You | +| | [Control plane](../concepts/langgraph_control_plane.md) | [Data plane](../concepts/langgraph_data_plane.md) | +| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| **What is it?** | <ul><li>Control plane UI for creating deployments and revisions</li><li>Control plane APIs for creating deployments and revisions</li></ul> | <ul><li>Data plane "listener" for reconciling deployments with control plane state</li><li>LangGraph Servers</li><li>Postgres, Redis, etc</li></ul> | +| **Where is it hosted?** | Your cloud | Your cloud | +| **Who provisions and manages it?** | You | You | ### Architecture @@ -28,7 +29,7 @@ The [Self-Hosted Control Plane](./langgraph_self_hosted_control_plane.md) deploy ### Compute Platforms - - **Kubernetes**: The Self-Hosted Control Plane deployment option supports deploying control plane and data plane infrastructure to any Kubernetes cluster. +- **Kubernetes**: The Self-Hosted Control Plane deployment option supports deploying control plane and data plane infrastructure to any Kubernetes cluster. !!! tip - If you would like to enable this on your LangSmith instance, please follow the [Self-Hosted Control Plane deployment guide](../cloud/deployment/self_hosted_control_plane.md). \ No newline at end of file +If you would like to enable this on your LangSmith instance, please follow the [Self-Hosted Control Plane deployment guide](../cloud/deployment/self_hosted_control_plane.md). diff --git a/docs/docs/concepts/langgraph_self_hosted_data_plane.md b/docs/docs/concepts/langgraph_self_hosted_data_plane.md index 15c91ef27..5b69e25eb 100644 --- a/docs/docs/concepts/langgraph_self_hosted_data_plane.md +++ b/docs/docs/concepts/langgraph_self_hosted_data_plane.md @@ -8,6 +8,7 @@ search: There are two versions of the self-hosted deployment: [Self-Hosted Data Plane](./deployment_options.md#self-hosted-data-plane) and [Self-Hosted Control Plane](./deployment_options.md#self-hosted-control-plane). !!! info "Important" + The Self-Hosted Data Plane deployment option requires an [Enterprise](plans.md) plan. ## Requirements @@ -19,11 +20,11 @@ There are two versions of the self-hosted deployment: [Self-Hosted Data Plane](. The [Self-Hosted Data Plane](../cloud/deployment/self_hosted_data_plane.md) deployment option is a "hybrid" model for deployment where we manage the [control plane](./langgraph_control_plane.md) in our cloud and you manage the [data plane](./langgraph_data_plane.md) in your cloud. This option provides a way to securely manage your data plane infrastructure, while offloading control plane management to us. When using the Self-Hosted Data Plane version, you authenticate with a [LangSmith](https://smith.langchain.com/) API key. -| | [Control plane](../concepts/langgraph_control_plane.md) | [Data plane](../concepts/langgraph_data_plane.md) | -|-------------------|-------------------|------------| -| **What is it?** | <ul><li>Control plane UI for creating deployments and revisions</li><li>Control plane APIs for creating deployments and revisions</li></ul> | <ul><li>Data plane "listener" for reconciling deployments with control plane state</li><li>LangGraph Servers</li><li>Postgres, Redis, etc</li></ul> | -| **Where is it hosted?** | LangChain's cloud | Your cloud | -| **Who provisions and manages it?** | LangChain | You | +| | [Control plane](../concepts/langgraph_control_plane.md) | [Data plane](../concepts/langgraph_data_plane.md) | +| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| **What is it?** | <ul><li>Control plane UI for creating deployments and revisions</li><li>Control plane APIs for creating deployments and revisions</li></ul> | <ul><li>Data plane "listener" for reconciling deployments with control plane state</li><li>LangGraph Servers</li><li>Postgres, Redis, etc</li></ul> | +| **Where is it hosted?** | LangChain's cloud | Your cloud | +| **Who provisions and manages it?** | LangChain | You | For information on how to deploy a [LangGraph Server](../concepts/langgraph_server.md) to Self-Hosted Data Plane, see [Deploy to Self-Hosted Data Plane](../cloud/deployment/self_hosted_data_plane.md) @@ -37,4 +38,4 @@ For information on how to deploy a [LangGraph Server](../concepts/langgraph_serv - **Amazon ECS**: Coming soon! !!! tip - If you would like to deploy to Kubernetes, you can follow the [Self-Hosted Data Plane deployment guide](../cloud/deployment/self_hosted_data_plane.md). \ No newline at end of file +If you would like to deploy to Kubernetes, you can follow the [Self-Hosted Data Plane deployment guide](../cloud/deployment/self_hosted_data_plane.md). diff --git a/docs/docs/concepts/low_level.md b/docs/docs/concepts/low_level.md index 15d5db4e7..5dfa87e06 100644 --- a/docs/docs/concepts/low_level.md +++ b/docs/docs/concepts/low_level.md @@ -9,13 +9,13 @@ search: At its core, LangGraph models agent workflows as graphs. You define the behavior of your agents using three key components: -1. [`State`](#state): A shared data structure that represents the current snapshot of your application. It can be any Python type, but is typically a `TypedDict` or Pydantic `BaseModel`. +1. [`State`](#state): A shared data structure that represents the current snapshot of your application. It can be any data type, but is typically defined using a shared state schema. -2. [`Nodes`](#nodes): Python functions that encode the logic of your agents. They receive the current `State` as input, perform some computation or side-effect, and return an updated `State`. +2. [`Nodes`](#nodes): Functions that encode the logic of your agents. They receive the current state as input, perform some computation or side-effect, and return an updated state. -3. [`Edges`](#edges): Python functions that determine which `Node` to execute next based on the current `State`. They can be conditional branches or fixed transitions. +3. [`Edges`](#edges): Functions that determine which `Node` to execute next based on the current state. They can be conditional branches or fixed transitions. -By composing `Nodes` and `Edges`, you can create complex, looping workflows that evolve the `State` over time. The real power, though, comes from how LangGraph manages that `State`. To emphasize: `Nodes` and `Edges` are nothing more than Python functions - they can contain an LLM or just good ol' Python code. +By composing `Nodes` and `Edges`, you can create complex, looping workflows that evolve the state over time. The real power, though, comes from how LangGraph manages that state. To emphasize: `Nodes` and `Edges` are nothing more than functions - they can contain an LLM or just good ol' code. In short: _nodes do the work, edges tell what to do next_. @@ -33,21 +33,51 @@ To build your graph, you first define the [state](#state), you then add [nodes]( Compiling is a pretty simple step. It provides a few basic checks on the structure of your graph (no orphaned nodes, etc). It is also where you can specify runtime args like [checkpointers](./persistence.md) and breakpoints. You compile your graph by just calling the `.compile` method: +:::python + ```python graph = graph_builder.compile(...) ``` +::: + +:::js + +```typescript +const graph = new StateGraph(StateAnnotation) + .addNode("nodeA", nodeA) + .addEdge(START, "nodeA") + .addEdge("nodeA", END) + .compile(); +``` + +::: + You **MUST** compile your graph before you can use it. ## State +:::python The first thing you do when you define a graph is define the `State` of the graph. The `State` consists of the [schema of the graph](#schema) as well as [`reducer` functions](#reducers) which specify how to apply updates to the state. The schema of the `State` will be the input schema to all `Nodes` and `Edges` in the graph, and can be either a `TypedDict` or a `Pydantic` model. All `Nodes` will emit updates to the `State` which are then applied using the specified `reducer` function. +::: + +:::js +The first thing you do when you define a graph is define the `State` of the graph. The `State` consists of the [schema of the graph](#schema) as well as [`reducer` functions](#reducers) which specify how to apply updates to the state. The schema of the `State` will be the input schema to all `Nodes` and `Edges` in the graph, and can be either a Zod schema or a schema built using `Annotation.Root`. All `Nodes` will emit updates to the `State` which are then applied using the specified `reducer` function. +::: ### Schema +:::python The main documented way to specify the schema of a graph is by using a [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict). If you want to provide default values in your state, use a [`dataclass`](https://docs.python.org/3/library/dataclasses.html). We also support using a Pydantic [BaseModel](../how-tos/graph-api.md#use-pydantic-models-for-graph-state) as your graph state if you want recursive data validation (though note that pydantic is less performant than a `TypedDict` or `dataclass`). By default, the graph will have the same input and output schemas. If you want to change this, you can also specify explicit input and output schemas directly. This is useful when you have a lot of keys, and some are explicitly for input and others for output. See the [guide here](../how-tos/graph-api.md#define-input-and-output-schemas) for how to use. +::: + +:::js +The main documented way to specify the schema of a graph is by using Zod schemas. However, we also support using the `Annotation` API to define the schema of the graph. + +By default, the graph will have the same input and output schemas. If you want to change this, you can also specify explicit input and output schemas directly. This is useful when you have a lot of keys, and some are explicitly for input and others for output. +::: #### Multiple schemas @@ -56,12 +86,16 @@ Typically, all graph nodes communicate with a single schema. This means that the - Internal nodes can pass information that is not required in the graph's input / output. - We may also want to use different input / output schemas for the graph. The output might, for example, only contain a single relevant output key. -It is possible to have nodes write to private state channels inside the graph for internal node communication. We can simply define a private schema, `PrivateState`. See [this guide](../how-tos/graph-api.md#pass-private-state-between-nodes) for more detail. +It is possible to have nodes write to private state channels inside the graph for internal node communication. We can simply define a private schema, `PrivateState`. + +See [this guide](../how-tos/graph-api.ipynb#pass-private-state-between-nodes) for more detail. It is also possible to define explicit input and output schemas for a graph. In these cases, we define an "internal" schema that contains _all_ keys relevant to graph operations. But, we also define `input` and `output` schemas that are sub-sets of the "internal" schema to constrain the input and output of the graph. See [this guide](../how-tos/graph-api.md#define-input-and-output-schemas) for more detail. Let's look at an example: +:::python + ```python class InputState(TypedDict): user_input: str @@ -100,14 +134,80 @@ builder.add_edge("node_3", END) graph = builder.compile() graph.invoke({"user_input":"My"}) -{'graph_output': 'My name is Lance'} +# {'graph_output': 'My name is Lance'} ``` +::: + +:::js + +```typescript +const InputState = z.object({ + userInput: z.string(), +}); + +const OutputState = z.object({ + graphOutput: z.string(), +}); + +const OverallState = z.object({ + foo: z.string(), + userInput: z.string(), + graphOutput: z.string(), +}); + +const PrivateState = z.object({ + bar: z.string(), +}); + +const graph = new StateGraph({ + state: OverallState, + input: InputState, + output: OutputState, +}) + .addNode("node1", (state) => { + // Write to OverallState + return { foo: state.userInput + " name" }; + }) + .addNode("node2", (state) => { + // Read from OverallState, write to PrivateState + return { bar: state.foo + " is" }; + }) + .addNode( + "node3", + (state) => { + // Read from PrivateState, write to OutputState + return { graphOutput: state.bar + " Lance" }; + }, + { input: PrivateState } + ) + .addEdge(START, "node1") + .addEdge("node1", "node2") + .addEdge("node2", "node3") + .addEdge("node3", END) + .compile(); + +await graph.invoke({ userInput: "My" }); +// { graphOutput: 'My name is Lance' } +``` + +::: + There are two subtle and important points to note here: +:::python + 1. We pass `state: InputState` as the input schema to `node_1`. But, we write out to `foo`, a channel in `OverallState`. How can we write out to a state channel that is not included in the input schema? This is because a node _can write to any state channel in the graph state._ The graph state is the union of the state channels defined at initialization, which includes `OverallState` and the filters `InputState` and `OutputState`. 2. We initialize the graph with `StateGraph(OverallState,input_schema=InputState,output_schema=OutputState)`. So, how can we write to `PrivateState` in `node_2`? How does the graph gain access to this schema if it was not passed in the `StateGraph` initialization? We can do this because _nodes can also declare additional state channels_ as long as the state schema definition exists. In this case, the `PrivateState` schema is defined, so we can add `bar` as a new state channel in the graph and write to it. + ::: + +:::js + +1. We pass `state` as the input schema to `node1`. But, we write out to `foo`, a channel in `OverallState`. How can we write out to a state channel that is not included in the input schema? This is because a node _can write to any state channel in the graph state._ The graph state is the union of the state channels defined at initialization, which includes `OverallState` and the filters `InputState` and `OutputState`. + +2. We initialize the graph with `StateGraph({ state: OverallState, input: InputState, output: OutputState })`. So, how can we write to `PrivateState` in `node2`? How does the graph gain access to this schema if it was not passed in the `StateGraph` initialization? We can do this because _nodes can also declare additional state channels_ as long as the state schema definition exists. In this case, the `PrivateState` schema is defined, so we can add `bar` as a new state channel in the graph and write to it. + ::: ### Reducers @@ -119,6 +219,8 @@ These two examples show how to use the default reducer: **Example A:** +:::python + ```python from typing_extensions import TypedDict @@ -127,10 +229,33 @@ class State(TypedDict): bar: list[str] ``` -In this example, no reducer functions are specified for any key. Let's assume the input to the graph is `{"foo": 1, "bar": ["hi"]}`. Let's then assume the first `Node` returns `{"foo": 2}`. This is treated as an update to the state. Notice that the `Node` does not need to return the whole `State` schema - just an update. After applying this update, the `State` would then be `{"foo": 2, "bar": ["hi"]}`. If the second node returns `{"bar": ["bye"]}` then the `State` would then be `{"foo": 2, "bar": ["bye"]}` +::: + +:::js + +```typescript +const State = z.object({ + foo: z.number(), + bar: z.array(z.string()), +}); +``` + +::: + +In this example, no reducer functions are specified for any key. Let's assume the input to the graph is: + +:::python +`{"foo": 1, "bar": ["hi"]}`. Let's then assume the first `Node` returns `{"foo": 2}`. This is treated as an update to the state. Notice that the `Node` does not need to return the whole `State` schema - just an update. After applying this update, the `State` would then be `{"foo": 2, "bar": ["hi"]}`. If the second node returns `{"bar": ["bye"]}` then the `State` would then be `{"foo": 2, "bar": ["bye"]}` +::: + +:::js +`{ foo: 1, bar: ["hi"] }`. Let's then assume the first `Node` returns `{ foo: 2 }`. This is treated as an update to the state. Notice that the `Node` does not need to return the whole `State` schema - just an update. After applying this update, the `State` would then be `{ foo: 2, bar: ["hi"] }`. If the second node returns `{ bar: ["bye"] }` then the `State` would then be `{ foo: 2, bar: ["bye"] }` +::: **Example B:** +:::python + ```python from typing import Annotated from typing_extensions import TypedDict @@ -142,21 +267,56 @@ class State(TypedDict): ``` In this example, we've used the `Annotated` type to specify a reducer function (`operator.add`) for the second key (`bar`). Note that the first key remains unchanged. Let's assume the input to the graph is `{"foo": 1, "bar": ["hi"]}`. Let's then assume the first `Node` returns `{"foo": 2}`. This is treated as an update to the state. Notice that the `Node` does not need to return the whole `State` schema - just an update. After applying this update, the `State` would then be `{"foo": 2, "bar": ["hi"]}`. If the second node returns `{"bar": ["bye"]}` then the `State` would then be `{"foo": 2, "bar": ["hi", "bye"]}`. Notice here that the `bar` key is updated by adding the two lists together. +::: + +:::js + +```typescript +import { z } from "zod"; +import { withLangGraph } from "@langchain/langgraph/zod"; + +const State = z.object({ + foo: z.number(), + bar: withLangGraph(z.array(z.string()), { + reducer: { + fn: (x, y) => x.concat(y), + }, + }), +}); +``` + +In this example, we've used the `withLangGraph` function to specify a reducer function for the second key (`bar`). Note that the first key remains unchanged. Let's assume the input to the graph is `{ foo: 1, bar: ["hi"] }`. Let's then assume the first `Node` returns `{ foo: 2 }`. This is treated as an update to the state. Notice that the `Node` does not need to return the whole `State` schema - just an update. After applying this update, the `State` would then be `{ foo: 2, bar: ["hi"] }`. If the second node returns `{ bar: ["bye"] }` then the `State` would then be `{ foo: 2, bar: ["hi", "bye"] }`. Notice here that the `bar` key is updated by adding the two arrays together. +::: ### Working with Messages in Graph State #### Why use messages? +:::python Most modern LLM providers have a chat model interface that accepts a list of messages as input. LangChain's [`ChatModel`](https://python.langchain.com/docs/concepts/#chat-models) in particular accepts a list of `Message` objects as inputs. These messages come in a variety of forms such as `HumanMessage` (user input) or `AIMessage` (LLM response). To read more about what message objects are, please refer to [this](https://python.langchain.com/docs/concepts/#messages) conceptual guide. +::: + +:::js +Most modern LLM providers have a chat model interface that accepts a list of messages as input. LangChain's [`ChatModel`](https://js.langchain.com/docs/concepts/#chat-models) in particular accepts a list of `Message` objects as inputs. These messages come in a variety of forms such as `HumanMessage` (user input) or `AIMessage` (LLM response). To read more about what message objects are, please refer to [this](https://js.langchain.com/docs/concepts/#messages) conceptual guide. +::: #### Using Messages in your Graph +:::python In many cases, it is helpful to store prior conversation history as a list of messages in your graph state. To do so, we can add a key (channel) to the graph state that stores a list of `Message` objects and annotate it with a reducer function (see `messages` key in the example below). The reducer function is vital to telling the graph how to update the list of `Message` objects in the state with each state update (for example, when a node sends an update). If you don't specify a reducer, every state update will overwrite the list of messages with the most recently provided value. If you wanted to simply append messages to the existing list, you could use `operator.add` as a reducer. However, you might also want to manually update messages in your graph state (e.g. human-in-the-loop). If you were to use `operator.add`, the manual state updates you send to the graph would be appended to the existing list of messages, instead of updating existing messages. To avoid that, you need a reducer that can keep track of message IDs and overwrite existing messages, if updated. To achieve this, you can use the prebuilt `add_messages` function. For brand new messages, it will simply append to existing list, but it will also handle the updates for existing messages correctly. +::: + +:::js +In many cases, it is helpful to store prior conversation history as a list of messages in your graph state. To do so, we can add a key (channel) to the graph state that stores a list of `Message` objects and annotate it with a reducer function (see `messages` key in the example below). The reducer function is vital to telling the graph how to update the list of `Message` objects in the state with each state update (for example, when a node sends an update). If you don't specify a reducer, every state update will overwrite the list of messages with the most recently provided value. If you wanted to simply append messages to the existing list, you could use a function that concatenates arrays as a reducer. + +However, you might also want to manually update messages in your graph state (e.g. human-in-the-loop). If you were to use a simple concatenation function, the manual state updates you send to the graph would be appended to the existing list of messages, instead of updating existing messages. To avoid that, you need a reducer that can keep track of message IDs and overwrite existing messages, if updated. To achieve this, you can use the prebuilt `MessagesZodState` schema. For brand new messages, it will simply append to existing list, but it will also handle the updates for existing messages correctly. +::: #### Serialization +:::python In addition to keeping track of message IDs, the `add_messages` function will also try to deserialize messages into LangChain `Message` objects whenever a state update is received on the `messages` channel. See more information on LangChain serialization/deserialization [here](https://python.langchain.com/docs/how_to/serialization/). This allows sending graph inputs / state updates in the following format: ```python @@ -179,6 +339,45 @@ class GraphState(TypedDict): messages: Annotated[list[AnyMessage], add_messages] ``` +::: + +:::js +In addition to keeping track of message IDs, `MessagesZodState` will also try to deserialize messages into LangChain `Message` objects whenever a state update is received on the `messages` channel. This allows sending graph inputs / state updates in the following format: + +```typescript +// this is supported +{ + messages: [new HumanMessage("message")]; +} + +// and this is also supported +{ + messages: [{ role: "human", content: "message" }]; +} +``` + +Since the state updates are always deserialized into LangChain `Messages` when using `MessagesZodState`, you should use dot notation to access message attributes, like `state.messages[state.messages.length - 1].content`. Below is an example of a graph that uses `MessagesZodState`: + +```typescript +import { StateGraph, MessagesZodState } from "@langchain/langgraph"; + +const graph = new StateGraph(MessagesZodState) + ... +``` + +`MessagesZodState` is defined with a single `messages` key which is a list of `BaseMessage` objects and uses the appropriate reducer. Typically, there is more state to track than just messages, so we see people extend this state and add more fields, like: + +```typescript +const State = z.object({ + messages: MessagesZodState.shape.messages, + documents: z.array(z.string()), +}); +``` + +::: + +:::python + #### MessagesState Since having a list of messages in your state is so common, there exists a prebuilt state called `MessagesState` which makes it easy to use messages. `MessagesState` is defined with a single `messages` key which is a list of `AnyMessage` objects and uses the `add_messages` reducer. Typically, there is more state to track than just messages, so we see people subclass this state and add more fields, like: @@ -190,16 +389,19 @@ class State(MessagesState): documents: list[str] ``` +::: + ## Nodes +:::python + In LangGraph, nodes are Python functions (either synchronous or asynchronous) that accept the following arguments: 1. `state`: The [state](#state) of the graph 2. `config`: A `RunnableConfig` object that contains configuration information like `thread_id` and tracing information like `tags` 3. `runtime`: A `Runtime` object that contains [runtime `context`](#runtime-context) and other information like `store` and `stream_writer` - -Similar to `NetworkX`, you add these nodes to a graph using the [add_node][langgraph.graph.StateGraph.add_node] method: +Similar to `NetworkX`, you add these nodes to a graph using the @[add_node][add_node] method: ```python from dataclasses import dataclass @@ -237,47 +439,123 @@ builder.add_node("node_with_config", node_with_config) ... ``` +::: + +:::js + +In LangGraph, nodes are typically functions (sync or async) that accept the following arguments: + +1. `state`: The [state](#state) of the graph +2. `config`: A `RunnableConfig` object that contains configuration information like `thread_id` and tracing information like `tags` + +You can add nodes to a graph using the `addNode` method. + +```typescript +import { StateGraph } from "@langchain/langgraph"; +import { RunnableConfig } from "@langchain/core/runnables"; +import { z } from "zod"; + +const State = z.object({ + input: z.string(), + results: z.string(), +}); + +const builder = new StateGraph(State); + .addNode("myNode", (state, config) => { + console.log("In node: ", config?.configurable?.user_id); + return { results: `Hello, ${state.input}!` }; + }) + addNode("otherNode", (state) => { + return state; + }) + ... +``` + +::: + Behind the scenes, functions are converted to [RunnableLambda](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.base.RunnableLambda.html#langchain_core.runnables.base.RunnableLambda)s, which add batch and async support to your function, along with native tracing and debugging. If you add a node to a graph without specifying a name, it will be given a default name equivalent to the function name. +:::python + ```python builder.add_node(my_node) # You can then create edges to/from this node by referencing it as `"my_node"` ``` +::: + +:::js + +```typescript +builder.addNode(myNode); +// You can then create edges to/from this node by referencing it as `"myNode"` +``` + +::: + ### `START` Node The `START` Node is a special node that represents the node that sends user input to the graph. The main purpose for referencing this node is to determine which nodes should be called first. +:::python + ```python from langgraph.graph import START graph.add_edge(START, "node_a") ``` +::: + +:::js + +```typescript +import { START } from "@langchain/langgraph"; + +graph.addEdge(START, "nodeA"); +``` + +::: + ### `END` Node The `END` Node is a special node that represents a terminal node. This node is referenced when you want to denote which edges have no actions after they are done. -``` +:::python + +```python from langgraph.graph import END graph.add_edge("node_a", END) ``` +::: + +:::js + +```typescript +import { END } from "@langchain/langgraph"; + +graph.addEdge("nodeA", END); +``` + +::: + ### Node Caching +:::python LangGraph supports caching of tasks/nodes based on the input to the node. To use caching: -* Specify a cache when compiling a graph (or specifying an entrypoint) -* Specify a cache policy for nodes. Each cache policy supports: - * `key_func` used to generate a cache key based on the input to a node, which defaults to a `hash` of the input with pickle. - * `ttl`, the time to live for the cache in seconds. If not specified, the cache will never expire. +- Specify a cache when compiling a graph (or specifying an entrypoint) +- Specify a cache policy for nodes. Each cache policy supports: + - `key_func` used to generate a cache key based on the input to a node, which defaults to a `hash` of the input with pickle. + - `ttl`, the time to live for the cache in seconds. If not specified, the cache will never expire. For example: -```py +```python import time from typing_extensions import TypedDict from langgraph.graph import StateGraph @@ -313,6 +591,40 @@ print(graph.invoke({"x": 5}, stream_mode='updates')) # (2)! 1. First run takes two seconds to run (due to mocked expensive computation). 2. Second run utilizes cache and returns quickly. + ::: + +:::js +LangGraph supports caching of tasks/nodes based on the input to the node. To use caching: + +- Specify a cache when compiling a graph (or specifying an entrypoint) +- Specify a cache policy for nodes. Each cache policy supports: + - `keyFunc`, which is used to generate a cache key based on the input to a node. + - `ttl`, the time to live for the cache in seconds. If not specified, the cache will never expire. + +```typescript +import { StateGraph, MessagesZodState } from "@langchain/langgraph"; +import { InMemoryCache } from "@langchain/langgraph-checkpoint"; + +const graph = new StateGraph(MessagesZodState) + .addNode( + "expensive_node", + async () => { + // Simulate an expensive operation + await new Promise((resolve) => setTimeout(resolve, 3000)); + return { result: 10 }; + }, + { cachePolicy: { ttl: 3 } } + ) + .addEdge(START, "expensive_node") + .compile({ cache: new InMemoryCache() }); + +await graph.invoke({ x: 5 }, { streamMode: "updates" }); // (1)! +// [{"expensive_node": {"result": 10}}] +await graph.invoke({ x: 5 }, { streamMode: "updates" }); // (2)! +// [{"expensive_node": {"result": 10}, "__metadata__": {"cached": true}}] +``` + +::: ## Edges @@ -327,15 +639,28 @@ A node can have MULTIPLE outgoing edges. If a node has multiple out-going edges, ### Normal Edges -If you **always** want to go from node A to node B, you can use the [add_edge][langgraph.graph.StateGraph.add_edge] method directly. +:::python +If you **always** want to go from node A to node B, you can use the @[add_edge][add_edge] method directly. ```python graph.add_edge("node_a", "node_b") ``` +::: + +:::js +If you **always** want to go from node A to node B, you can use the @[`addEdge`][add_edge] method directly. + +```typescript +graph.addEdge("nodeA", "nodeB"); +``` + +::: + ### Conditional Edges -If you want to **optionally** route to 1 or more edges (or optionally terminate), you can use the [add_conditional_edges][langgraph.graph.StateGraph.add_conditional_edges] method. This method accepts the name of a node and a "routing function" to call after that node is executed: +:::python +If you want to **optionally** route to 1 or more edges (or optionally terminate), you can use the @[add_conditional_edges][add_conditional_edges] method. This method accepts the name of a node and a "routing function" to call after that node is executed: ```python graph.add_conditional_edges("node_a", routing_function) @@ -351,12 +676,37 @@ You can optionally provide a dictionary that maps the `routing_function`'s outpu graph.add_conditional_edges("node_a", routing_function, {True: "node_b", False: "node_c"}) ``` +::: + +:::js +If you want to **optionally** route to 1 or more edges (or optionally terminate), you can use the @[`addConditionalEdges`][add_conditional_edges] method. This method accepts the name of a node and a "routing function" to call after that node is executed: + +```typescript +graph.addConditionalEdges("nodeA", routingFunction); +``` + +Similar to nodes, the `routingFunction` accepts the current `state` of the graph and returns a value. + +By default, the return value `routingFunction` is used as the name of the node (or list of nodes) to send the state to next. All those nodes will be run in parallel as a part of the next superstep. + +You can optionally provide an object that maps the `routingFunction`'s output to the name of the next node. + +```typescript +graph.addConditionalEdges("nodeA", routingFunction, { + true: "nodeB", + false: "nodeC", +}); +``` + +::: + !!! tip - Use [`Command`](#command) instead of conditional edges if you want to combine state updates and routing in a single function. +Use [`Command`](#command) instead of conditional edges if you want to combine state updates and routing in a single function. ### Entry Point -The entry point is the first node(s) that are run when the graph starts. You can use the [`add_edge`][langgraph.graph.StateGraph.add_edge] method from the virtual [`START`][langgraph.constants.START] node to the first node to execute to specify where to enter the graph. +:::python +The entry point is the first node(s) that are run when the graph starts. You can use the @[`add_edge`][add_edge] method from the virtual @[`START`][START] node to the first node to execute to specify where to enter the graph. ```python from langgraph.graph import START @@ -364,9 +714,23 @@ from langgraph.graph import START graph.add_edge(START, "node_a") ``` +::: + +:::js +The entry point is the first node(s) that are run when the graph starts. You can use the @[`addEdge`][add_edge] method from the virtual @[`START`][START] node to the first node to execute to specify where to enter the graph. + +```typescript +import { START } from "@langchain/langgraph"; + +graph.addEdge(START, "nodeA"); +``` + +::: + ### Conditional Entry Point -A conditional entry point lets you start at different nodes depending on custom logic. You can use [`add_conditional_edges`][langgraph.graph.StateGraph.add_conditional_edges] from the virtual [`START`][langgraph.constants.START] node to accomplish this. +:::python +A conditional entry point lets you start at different nodes depending on custom logic. You can use @[`add_conditional_edges`][add_conditional_edges] from the virtual @[`START`][START] node to accomplish this. ```python from langgraph.graph import START @@ -380,11 +744,34 @@ You can optionally provide a dictionary that maps the `routing_function`'s outpu graph.add_conditional_edges(START, routing_function, {True: "node_b", False: "node_c"}) ``` +::: + +:::js +A conditional entry point lets you start at different nodes depending on custom logic. You can use @[`addConditionalEdges`][add_conditional_edges] from the virtual @[`START`][START] node to accomplish this. + +```typescript +import { START } from "@langchain/langgraph"; + +graph.addConditionalEdges(START, routingFunction); +``` + +You can optionally provide an object that maps the `routingFunction`'s output to the name of the next node. + +```typescript +graph.addConditionalEdges(START, routingFunction, { + true: "nodeB", + false: "nodeC", +}); +``` + +::: + ## `Send` +:::python By default, `Nodes` and `Edges` are defined ahead of time and operate on the same shared state. However, there can be cases where the exact edges are not known ahead of time and/or you may want different versions of `State` to exist at the same time. A common example of this is with [map-reduce](https://langchain-ai.github.io/langgraph/how-tos/map-reduce/) design patterns. In this design pattern, a first node may generate a list of objects, and you may want to apply some other node to all those objects. The number of objects may be unknown ahead of time (meaning the number of edges may not be known) and the input `State` to the downstream `Node` should be different (one for each generated object). -To support this design pattern, LangGraph supports returning [`Send`][langgraph.types.Send] objects from conditional edges. `Send` takes two arguments: first is the name of the node, and second is the state to pass to that node. +To support this design pattern, LangGraph supports returning @[`Send`][Send] objects from conditional edges. `Send` takes two arguments: first is the name of the node, and second is the state to pass to that node. ```python def continue_to_jokes(state: OverallState): @@ -393,9 +780,27 @@ def continue_to_jokes(state: OverallState): graph.add_conditional_edges("node_a", continue_to_jokes) ``` +::: + +:::js +By default, `Nodes` and `Edges` are defined ahead of time and operate on the same shared state. However, there can be cases where the exact edges are not known ahead of time and/or you may want different versions of `State` to exist at the same time. A common example of this is with map-reduce design patterns. In this design pattern, a first node may generate a list of objects, and you may want to apply some other node to all those objects. The number of objects may be unknown ahead of time (meaning the number of edges may not be known) and the input `State` to the downstream `Node` should be different (one for each generated object). + +To support this design pattern, LangGraph supports returning @[`Send`][Send] objects from conditional edges. `Send` takes two arguments: first is the name of the node, and second is the state to pass to that node. + +```typescript +import { Send } from "@langchain/langgraph"; + +graph.addConditionalEdges("nodeA", (state) => { + return state.subjects.map((subject) => new Send("generateJoke", { subject })); +}); +``` + +::: + ## `Command` -It can be useful to combine control flow (edges) and state updates (nodes). For example, you might want to BOTH perform state updates AND decide which node to go to next in the SAME node. LangGraph provides a way to do so by returning a [`Command`][langgraph.types.Command] object from node functions: +:::python +It can be useful to combine control flow (edges) and state updates (nodes). For example, you might want to BOTH perform state updates AND decide which node to go to next in the SAME node. LangGraph provides a way to do so by returning a @[`Command`][Command] object from node functions: ```python def my_node(state: State) -> Command[Literal["my_other_node"]]: @@ -415,6 +820,49 @@ def my_node(state: State) -> Command[Literal["my_other_node"]]: return Command(update={"foo": "baz"}, goto="my_other_node") ``` +Check out this [how-to guide](../how-tos/graph-api.ipynb#combine-control-flow-and-state-updates-with-command) for an end-to-end example of how to use `Command`. +::: + +:::js +It can be useful to combine control flow (edges) and state updates (nodes). For example, you might want to BOTH perform state updates AND decide which node to go to next in the SAME node. LangGraph provides a way to do so by returning a `Command` object from node functions: + +```typescript +import { Command } from "@langchain/langgraph"; + +graph.addNode("myNode", (state) => { + return new Command({ + update: { foo: "bar" }, + goto: "myOtherNode", + }); +}); +``` + +With `Command` you can also achieve dynamic control flow behavior (identical to [conditional edges](#conditional-edges)): + +```typescript +import { Command } from "@langchain/langgraph"; + +graph.addNode("myNode", (state) => { + if (state.foo === "bar") { + return new Command({ + update: { foo: "baz" }, + goto: "myOtherNode", + }); + } +}); +``` + +When using `Command` in your node functions, you must add the `ends` parameter when adding the node to specify which nodes it can route to: + +```typescript +builder.addNode("myNode", myNode, { + ends: ["myOtherNode", END], +}); +``` + +Check out this [how-to guide](../how-tos/graph-api.ipynb#combine-control-flow-and-state-updates-with-command) for an end-to-end example of how to use `Command`. +::: + !!! important When returning `Command` in your node functions, you must add return type annotations with the list of node names the node is routing to, e.g. `Command[Literal["my_other_node"]]`. This is necessary for the graph rendering and tells LangGraph that `my_node` can navigate to `my_other_node`. @@ -423,12 +871,12 @@ Check out this [how-to guide](../how-tos/graph-api.md#combine-control-flow-and-s ### When should I use Command instead of conditional edges? -Use `Command` when you need to **both** update the graph state **and** route to a different node. For example, when implementing [multi-agent handoffs](./multi_agent.md#handoffs) where it's important to route to a different agent and pass some information to that agent. - -Use [conditional edges](#conditional-edges) to route between nodes conditionally without updating the state. +- Use `Command` when you need to **both** update the graph state **and** route to a different node. For example, when implementing [multi-agent handoffs](./multi_agent.md#handoffs) where it's important to route to a different agent and pass some information to that agent. +- Use [conditional edges](#conditional-edges) to route between nodes conditionally without updating the state. ### Navigating to a node in a parent graph +:::python If you are using [subgraphs](./subgraphs.md), you might want to navigate from a node within a subgraph to a different subgraph (i.e. a different node in the parent graph). To do so, you can specify `graph=Command.PARENT` in `Command`: ```python @@ -448,6 +896,58 @@ def my_node(state: State) -> Command[Literal["other_subgraph"]]: When you send updates from a subgraph node to a parent graph node for a key that's shared by both parent and subgraph [state schemas](#schema), you **must** define a [reducer](#reducers) for the key you're updating in the parent graph state. See this [example](../how-tos/graph-api.md#navigate-to-a-node-in-a-parent-graph). +::: + +:::js +If you are using [subgraphs](./subgraphs.md), you might want to navigate from a node within a subgraph to a different subgraph (i.e. a different node in the parent graph). To do so, you can specify `graph: Command.PARENT` in `Command`: + +```typescript +import { Command } from "@langchain/langgraph"; + +graph.addNode("myNode", (state) => { + return new Command({ + update: { foo: "bar" }, + goto: "otherSubgraph", // where `otherSubgraph` is a node in the parent graph + graph: Command.PARENT, + }); +}); +``` + +!!! note + + Setting `graph` to `Command.PARENT` will navigate to the closest parent graph. + +!!! important "State updates with `Command.PARENT`" + + When you send updates from a subgraph node to a parent graph node for a key that's shared by both parent and subgraph [state schemas](#schema), you **must** define a [reducer](#reducers) for the key you're updating in the parent graph state. + +::: + +:::js +If you are using [subgraphs](./subgraphs.md), you might want to navigate from a node within a subgraph to a different subgraph (i.e. a different node in the parent graph). To do so, you can specify `graph: Command.PARENT` in `Command`: + +```typescript +import { Command } from "@langchain/langgraph"; + +graph.addNode("myNode", (state) => { + return new Command({ + update: { foo: "bar" }, + goto: "otherSubgraph", // where `otherSubgraph` is a node in the parent graph + graph: Command.PARENT, + }); +}); +``` + +!!! note + + Setting `graph` to `Command.PARENT` will navigate to the closest parent graph. + +!!! important "State updates with `Command.PARENT`" + + When you send updates from a subgraph node to a parent graph node for a key that's shared by both parent and subgraph [state schemas](#schema), you **must** define a [reducer](#reducers) for the key you're updating in the parent graph state. + +::: + This is particularly useful when implementing [multi-agent handoffs](./multi_agent.md#handoffs). Check out [this guide](../how-tos/graph-api.md#navigate-to-a-node-in-a-parent-graph) for detail. @@ -460,7 +960,13 @@ Refer to [this guide](../how-tos/graph-api.md#use-inside-tools) for detail. ### Human-in-the-loop +:::python `Command` is an important part of human-in-the-loop workflows: when using `interrupt()` to collect user input, `Command` is then used to supply the input and resume execution via `Command(resume="User input")`. Check out [this conceptual guide](./human_in_the_loop.md) for more information. +::: + +:::js +`Command` is an important part of human-in-the-loop workflows: when using `interrupt()` to collect user input, `Command` is then used to supply the input and resume execution via `new Command({ resume: "User input" })`. Check out the [human-in-the-loop conceptual guide](./human_in_the_loop.md) for more information. +::: ## Graph Migrations @@ -472,6 +978,8 @@ LangGraph can easily handle migrations of graph definitions (nodes, edges, and s - State keys that are renamed lose their saved state in existing threads - State keys whose types change in incompatible ways could currently cause issues in threads with state from before the change -- if this is a blocker please reach out and we can prioritize a solution. +:::python + ## Runtime Context When creating a graph, you can specify a `context_schema` for runtime context passed to nodes. This is useful for passing @@ -485,12 +993,46 @@ class ContextSchema: graph = StateGraph(State, context_schema=ContextSchema) ``` +::: + +:::js + +When creating a graph, you can also mark that certain parts of the graph are configurable. This is commonly done to enable easily switching between models or system prompts. This allows you to create a single "cognitive architecture" (the graph) but have multiple different instance of it. + +You can optionally specify a config schema when creating a graph. + +```typescript +import { z } from "zod"; + +const ConfigSchema = z.object({ + llm: z.string(), +}); + +const graph = new StateGraph(State, ConfigSchema); +``` + +::: + +:::python You can then pass this context into the graph using the `context` parameter of the `invoke` method. ```python graph.invoke(inputs, context={"llm_provider": "anthropic"}) ``` +::: + +:::js +You can then pass this configuration into the graph using the `configurable` config field. + +```typescript +const config = { configurable: { llm: "anthropic" } }; + +await graph.invoke(inputs, config); +``` + +::: + You can then access and use this context inside a node or conditional edge: ```python @@ -501,10 +1043,24 @@ def node_a(state: State, runtime: Runtime[ContextSchema]): ... ``` -See [this guide](../how-tos/graph-api.md#add-runtime-configuration) for a full breakdown on configuration. +See [this guide](../how-tos/graph-api.ipynb#add-runtime-configuration) for a full breakdown on configuration. +::: + +:::js + +```typescript +graph.addNode("myNode", (state, config) => { + const llmType = config?.configurable?.llm || "openai"; + const llm = getLlm(llmType); + return { results: `Hello, ${state.input}!` }; +}); +``` + +::: ### Recursion Limit +:::python The recursion limit sets the maximum number of [super-steps](#graphs) the graph can execute during a single execution. Once the limit is reached, LangGraph will raise `GraphRecursionError`. By default this value is set to 25 steps. The recursion limit can be set on any graph at runtime, and is passed to `.invoke`/`.stream` via the config dictionary. Importantly, `recursion_limit` is a standalone `config` key and should not be passed inside the `configurable` key as all other user-defined configuration. See the example below: ```python @@ -512,6 +1068,19 @@ graph.invoke(inputs, config={"recursion_limit": 5}, context={"llm": "anthropic"} ``` Read [this how-to](https://langchain-ai.github.io/langgraph/how-tos/recursion-limit/) to learn more about how the recursion limit works. +::: + +:::js +The recursion limit sets the maximum number of [super-steps](#graphs) the graph can execute during a single execution. Once the limit is reached, LangGraph will raise `GraphRecursionError`. By default this value is set to 25 steps. The recursion limit can be set on any graph at runtime, and is passed to `.invoke`/`.stream` via the config object. Importantly, `recursionLimit` is a standalone `config` key and should not be passed inside the `configurable` key as all other user-defined configuration. See the example below: + +```typescript +await graph.invoke(inputs, { + recursionLimit: 5, + configurable: { llm: "anthropic" }, +}); +``` + +::: ## Visualization diff --git a/docs/docs/concepts/mcp.md b/docs/docs/concepts/mcp.md index 4b05d008e..4544f00bc 100644 --- a/docs/docs/concepts/mcp.md +++ b/docs/docs/concepts/mcp.md @@ -12,9 +12,10 @@ pip install langchain-mcp-adapters ## Authenticate to an MCP server -You can set up [custom authentication middleware](../how-tos/auth/custom_auth.md) to authenticate a user with an MCP server to get access to user-scoped tools within your LangGraph Platform deployment. +You can set up [custom authentication middleware](../how-tos/auth/custom_auth.md) to authenticate a user with an MCP server to get access to user-scoped tools within your LangGraph Platform deployment. !!! note + Custom authentication is a LangGraph Platform feature. An example architecture for this flow: @@ -53,5 +54,4 @@ sequenceDiagram LangGraph -->> ClientApp: 12. Return resources / tool output ``` -For more information, see [MCP endpoint in LangGraph Server](../concepts/server-mcp.md#use-user-scoped-mcp-tools-in-your-deployment). - +For more information, see [MCP endpoint in LangGraph Server](../concepts/server-mcp.md). diff --git a/docs/docs/concepts/memory.md b/docs/docs/concepts/memory.md index 6e59a4cff..83d3f29a0 100644 --- a/docs/docs/concepts/memory.md +++ b/docs/docs/concepts/memory.md @@ -87,11 +87,25 @@ Regardless of memory management approach, the central point is that the agent wi [Episodic memory](https://en.wikipedia.org/wiki/Episodic_memory), in both humans and AI agents, involves recalling past events or actions. The [CoALA paper](https://arxiv.org/pdf/2309.02427) frames this well: facts can be written to semantic memory, whereas *experiences* can be written to episodic memory. For AI agents, episodic memory is often used to help an agent remember how to accomplish a task. +:::python In practice, episodic memories are often implemented through [few-shot example prompting](https://python.langchain.com/docs/concepts/few_shot_prompting/), where agents learn from past sequences to perform tasks correctly. Sometimes it's easier to "show" than "tell" and LLMs learn well from examples. Few-shot learning lets you ["program"](https://x.com/karpathy/status/1627366413840322562) your LLM by updating the prompt with input-output examples to illustrate the intended behavior. While various [best-practices](https://python.langchain.com/docs/concepts/#1-generating-examples) can be used to generate few-shot examples, often the challenge lies in selecting the most relevant examples based on user input. +::: +:::js +In practice, episodic memories are often implemented through few-shot example prompting, where agents learn from past sequences to perform tasks correctly. Sometimes it's easier to "show" than "tell" and LLMs learn well from examples. Few-shot learning lets you ["program"](https://x.com/karpathy/status/1627366413840322562) your LLM by updating the prompt with input-output examples to illustrate the intended behavior. While various best-practices can be used to generate few-shot examples, often the challenge lies in selecting the most relevant examples based on user input. +::: + +:::python Note that the memory [store](persistence.md#memory-store) is just one way to store data as few-shot examples. If you want to have more developer involvement, or tie few-shots more closely to your evaluation harness, you can also use a [LangSmith Dataset](https://docs.smith.langchain.com/evaluation/how_to_guides/datasets/index_datasets_for_dynamic_few_shot_example_selection) to store your data. Then dynamic few-shot example selectors can be used out-of-the box to achieve this same goal. LangSmith will index the dataset for you and enable retrieval of few shot examples that are most relevant to the user input based upon keyword similarity ([using a BM25-like algorithm](https://docs.smith.langchain.com/how_to_guides/datasets/index_datasets_for_dynamic_few_shot_example_selection) for keyword based similarity). See this how-to [video](https://www.youtube.com/watch?v=37VaU7e7t5o) for example usage of dynamic few-shot example selection in LangSmith. Also, see this [blog post](https://blog.langchain.dev/few-shot-prompting-to-improve-tool-calling-performance/) showcasing few-shot prompting to improve tool calling performance and this [blog post](https://blog.langchain.dev/aligning-llm-as-a-judge-with-human-preferences/) using few-shot example to align an LLMs to human preferences. +::: + +:::js +Note that the memory [store](persistence.md#memory-store) is just one way to store data as few-shot examples. If you want to have more developer involvement, or tie few-shots more closely to your evaluation harness, you can also use a LangSmith Dataset to store your data. Then dynamic few-shot example selectors can be used out-of-the box to achieve this same goal. LangSmith will index the dataset for you and enable retrieval of few shot examples that are most relevant to the user input based upon keyword similarity. + +See this how-to [video](https://www.youtube.com/watch?v=37VaU7e7t5o) for example usage of dynamic few-shot example selection in LangSmith. Also, see this [blog post](https://blog.langchain.dev/few-shot-prompting-to-improve-tool-calling-performance/) showcasing few-shot prompting to improve tool calling performance and this [blog post](https://blog.langchain.dev/aligning-llm-as-a-judge-with-human-preferences/) using few-shot example to align an LLMs to human preferences. +::: #### Procedural memory @@ -105,6 +119,7 @@ For example, we built a [Tweet generator](https://www.youtube.com/watch?v=Vn8A3B The below pseudo-code shows how you might implement this with the LangGraph memory [store](persistence.md#memory-store), using the store to save a prompt, the `update_instructions` node to get the current prompt (as well as feedback from the conversation with the user captured in `state["messages"]`), update the prompt, and save the new prompt back to the store. Then, the `call_model` get the updated prompt from the store and uses it to generate a response. +:::python ```python # Node that *uses* the instructions def call_model(state: State, store: BaseStore): @@ -125,6 +140,39 @@ def update_instructions(state: State, store: BaseStore): store.put(("agent_instructions",), "agent_a", {"instructions": new_instructions}) ... ``` +::: + +:::js +```typescript +// Node that *uses* the instructions +const callModel = async (state: State, store: BaseStore) => { + const namespace = ["agent_instructions"]; + const instructions = await store.get(namespace, "agent_a"); + // Application logic + const prompt = promptTemplate.format({ + instructions: instructions[0].value.instructions + }); + // ... +}; + +// Node that updates instructions +const updateInstructions = async (state: State, store: BaseStore) => { + const namespace = ["instructions"]; + const currentInstructions = await store.search(namespace); + // Memory logic + const prompt = promptTemplate.format({ + instructions: currentInstructions[0].value.instructions, + conversation: state.messages + }); + const output = await llm.invoke(prompt); + const newInstructions = output.new_instructions; + await store.put(["agent_instructions"], "agent_a", { + instructions: newInstructions + }); + // ... +}; +``` +::: ![](img/memory/update-instructions.png) @@ -154,6 +202,7 @@ See our [memory-service](https://github.com/langchain-ai/memory-template) templa LangGraph stores long-term memories as JSON documents in a [store](persistence.md#memory-store). Each memory is organized under a custom `namespace` (similar to a folder) and a distinct `key` (like a file name). Namespaces often include user or org IDs or other labels that makes it easier to organize information. This structure enables hierarchical organization of memories. Cross-namespace searching is then supported through content filters. +:::python ```python from langgraph.store.memory import InMemoryStore @@ -186,5 +235,47 @@ items = store.search( namespace, filter={"my-key": "my-value"}, query="language preferences" ) ``` +::: + +:::js +```typescript +import { InMemoryStore } from "@langchain/langgraph"; + +const embed = (texts: string[]): number[][] => { + // Replace with an actual embedding function or LangChain embeddings object + return texts.map(() => [1.0, 2.0]); +}; + +// InMemoryStore saves data to an in-memory dictionary. Use a DB-backed store in production use. +const store = new InMemoryStore({ index: { embed, dims: 2 } }); +const userId = "my-user"; +const applicationContext = "chitchat"; +const namespace = [userId, applicationContext]; + +await store.put( + namespace, + "a-memory", + { + rules: [ + "User likes short, direct language", + "User only speaks English & TypeScript", + ], + "my-key": "my-value", + } +); + +// get the "memory" by ID +const item = await store.get(namespace, "a-memory"); + +// search for "memories" within this namespace, filtering on content equivalence, sorted by vector similarity +const items = await store.search( + namespace, + { + filter: { "my-key": "my-value" }, + query: "language preferences" + } +); +``` +::: For more information about the memory store, see the [Persistence](persistence.md#memory-store) guide. \ No newline at end of file diff --git a/docs/docs/concepts/multi_agent.md b/docs/docs/concepts/multi_agent.md index 5d3e30a29..53f8bb3cd 100644 --- a/docs/docs/concepts/multi_agent.md +++ b/docs/docs/concepts/multi_agent.md @@ -1,8 +1,3 @@ ---- -search: - boost: 2 ---- - # Multi-agent systems An [agent](./agentic_concepts.md#agent-architectures) is _a system that uses an LLM to decide the control flow of an application_. As you develop these systems, they might grow more complex over time, making them harder to manage and scale. For example, you might run into the following problems: @@ -25,21 +20,23 @@ The primary benefits of using multi-agent systems are: There are several ways to connect agents in a multi-agent system: -- **Network**: each agent can communicate with [every other agent](https://langchain-ai.github.io/langgraph/tutorials/multi_agent/multi-agent-collaboration/). Any agent can decide which other agent to call next. -- **Supervisor**: each agent communicates with a single [supervisor](../tutorials/multi_agent/agent_supervisor.md) agent. Supervisor agent makes decisions on which agent should be called next. +- **Network**: each agent can communicate with [every other agent](../tutorials/multi_agent/multi-agent-collaboration.ipynb/). Any agent can decide which other agent to call next. +- **Supervisor**: each agent communicates with a single [supervisor](../tutorials/multi_agent/agent_supervisor.md/) agent. Supervisor agent makes decisions on which agent should be called next. - **Supervisor (tool-calling)**: this is a special case of supervisor architecture. Individual agents can be represented as tools. In this case, a supervisor agent uses a tool-calling LLM to decide which of the agent tools to call, as well as the arguments to pass to those agents. -- **Hierarchical**: you can define a multi-agent system with [a supervisor of supervisors](https://langchain-ai.github.io/langgraph/tutorials/multi_agent/hierarchical_agent_teams/). This is a generalization of the supervisor architecture and allows for more complex control flows. +- **Hierarchical**: you can define a multi-agent system with [a supervisor of supervisors](../tutorials/multi_agent/hierarchical_agent_teams.ipynb/). This is a generalization of the supervisor architecture and allows for more complex control flows. - **Custom multi-agent workflow**: each agent communicates with only a subset of agents. Parts of the flow are deterministic, and only some agents can decide which other agents to call next. ### Handoffs -In multi-agent architectures, agents can be represented as graph nodes. Each agent node executes its step(s) and decides whether to finish execution or route to another agent, including potentially routing to itself (e.g., running in a loop). A common pattern in multi-agent interactions is **handoffs**, where one agent *hands off* control to another. Handoffs allow you to specify: +In multi-agent architectures, agents can be represented as graph nodes. Each agent node executes its step(s) and decides whether to finish execution or route to another agent, including potentially routing to itself (e.g., running in a loop). A common pattern in multi-agent interactions is **handoffs**, where one agent _hands off_ control to another. Handoffs allow you to specify: -- __destination__: target agent to navigate to (e.g., name of the node to go to) -- __payload__: [information to pass to that agent](#communication-and-state-management) (e.g., state update) +- **destination**: target agent to navigate to (e.g., name of the node to go to) +- **payload**: [information to pass to that agent](#communication-and-state-management) (e.g., state update) To implement handoffs in LangGraph, agent nodes can return [`Command`](./low_level.md#command) object that allows you to combine both control flow and state updates: +:::python + ```python def agent(state) -> Command[Literal["agent", "another_agent"]]: # the condition for routing/halting can be anything, e.g. LLM tool call / structured output, etc. @@ -52,6 +49,26 @@ def agent(state) -> Command[Literal["agent", "another_agent"]]: ) ``` +::: + +:::js + +```typescript +graph.addNode((state) => { + // the condition for routing/halting can be anything, e.g. LLM tool call / structured output, etc. + const goto = getNextAgent(...); // 'agent' / 'another_agent' + return new Command({ + // Specify which agent to call next + goto, + // Update the graph state + update: { myStateKey: "myStateValue" } + }); +}) +``` + +::: + +:::python In a more complex scenario where each agent node is itself a graph (i.e., a [subgraph](./subgraphs.md)), a node in one of the agent subgraphs might want to navigate to a different agent. For example, if you have two agents, `alice` and `bob` (subgraph nodes in a parent graph), and `alice` needs to navigate to `bob`, you can set `graph=Command.PARENT` in the `Command` object: ```python @@ -64,8 +81,30 @@ def some_node_inside_alice(state): ) ``` +::: + +:::js +In a more complex scenario where each agent node is itself a graph (i.e., a [subgraph](./subgraphs.md)), a node in one of the agent subgraphs might want to navigate to a different agent. For example, if you have two agents, `alice` and `bob` (subgraph nodes in a parent graph), and `alice` needs to navigate to `bob`, you can set `graph: Command.PARNT` in the `Command` object: + +```typescript +alice.addNode((state) => { + return new Command({ + goto: "bob", + update: { myStateKey: "myStateValue" }, + // specify which graph to navigate to (defaults to the current graph) + graph: Command.PARENT, + }); +}); +``` + +::: + !!! note - If you need to support visualization for subgraphs communicating using `Command(graph=Command.PARENT)` you would need to wrap them in a node function with `Command` annotation, e.g. instead of this: + + :::python + + If you need to support visualization for subgraphs communicating using `Command(graph=Command.PARENT)` you would need to wrap them in a node function with `Command` annotation: + Instead of this: ```python builder.add_node(alice) @@ -80,9 +119,30 @@ def some_node_inside_alice(state): builder.add_node("alice", call_alice) ``` + ::: + + :::js + If you need to support visualization for subgraphs communicating using/ `Command({ graph: Command.PARENT })` you would need to wrap them in a node function with `Command` annotation: + + Instead of this: + + ```typescript + builder.addNode("alice", alice); + ``` + + you would need to do this: + + ```typescript + builder.addNode("alice", (state) => alice.invoke(state), { ends: ["bob"] }); + ``` + + ::: + #### Handoffs as tools -One of the most common agent types is a [tool-calling agent](../agents/overview.md). For those types of agents, a common pattern is wrapping a handoff in a tool call, e.g.: +One of the most common agent types is a [tool-calling agent](../agents/overview.md). For those types of agents, a common pattern is wrapping a handoff in a tool call: + +:::python ```python from langchain_core.tools import tool @@ -101,18 +161,65 @@ def transfer_to_bob(): ) ``` +::: + +:::js + +```typescript +import { tool } from "@langchain/core/tools"; +import { Command } from "@langchain/langgraph"; +import { z } from "zod"; + +const transferToBob = tool( + async () => { + return new Command({ + // name of the agent (node) to go to + goto: "bob", + // data to send to the agent + update: { myStateKey: "myStateValue" }, + // indicate to LangGraph that we need to navigate to + // agent node in a parent graph + graph: Command.PARENT, + }); + }, + { + name: "transfer_to_bob", + description: "Transfer to bob.", + schema: z.object({}), + } +); +``` + +::: + This is a special case of updating the graph state from tools where, in addition to the state update, the control flow is included as well. !!! important - If you want to use tools that return `Command`, you can either use prebuilt [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] / [`ToolNode`][langgraph.prebuilt.tool_node.ToolNode] components, or implement your own tool-executing node that collects `Command` objects returned by the tools and returns a list of them, e.g.: - - ```python - def call_tools(state): - ... - commands = [tools_by_name[tool_call["name"]].invoke(tool_call) for tool_call in tool_calls] - return commands - ``` + :::python + If you want to use tools that return `Command`, you can use the prebuilt @[`create_react_agent`][create_react_agent] / @[`ToolNode`][ToolNode] components, or else implement your own logic: + + ```python + def call_tools(state): + ... + commands = [tools_by_name[tool_call["name"]].invoke(tool_call) for tool_call in tool_calls] + return commands + ``` + ::: + + :::js + If you want to use tools that return `Command`, you can use the prebuilt @[`createReactAgent`][create_react_agent] / @[ToolNode] components, or else implement your own logic: + + ```typescript + graph.addNode("call_tools", async (state) => { + // ... tool execution logic + const commands = toolCalls.map((toolCall) => + toolsByName[toolCall.name].invoke(toolCall) + ); + return commands; + }); + ``` + ::: Let's now take a closer look at the different multi-agent architectures. @@ -120,6 +227,7 @@ Let's now take a closer look at the different multi-agent architectures. In this architecture, agents are defined as graph nodes. Each agent can communicate with every other agent (many-to-many connections) and can decide which agent to call next. This architecture is good for problems that do not have a clear hierarchy of agents or a specific sequence in which agents should be called. +:::python ```python from typing import Literal @@ -164,10 +272,70 @@ builder.add_edge(START, "agent_1") network = builder.compile() ``` +::: + +:::js + +```typescript +import { StateGraph, MessagesZodState, START, END } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; +import { Command } from "@langchain/langgraph"; +import { z } from "zod"; + +const model = new ChatOpenAI(); + +const agent1 = async (state: z.infer<typeof MessagesZodState>) => { + // you can pass relevant parts of the state to the LLM (e.g., state.messages) + // to determine which agent to call next. a common pattern is to call the model + // with a structured output (e.g. force it to return an output with a "next_agent" field) + const response = await model.invoke(...); + // route to one of the agents or exit based on the LLM's decision + // if the LLM returns "__end__", the graph will finish execution + return new Command({ + goto: response.nextAgent, + update: { messages: [response.content] }, + }); +}; + +const agent2 = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return new Command({ + goto: response.nextAgent, + update: { messages: [response.content] }, + }); +}; + +const agent3 = async (state: z.infer<typeof MessagesZodState>) => { + // ... + return new Command({ + goto: response.nextAgent, + update: { messages: [response.content] }, + }); +}; + +const builder = new StateGraph(MessagesZodState) + .addNode("agent1", agent1, { + ends: ["agent2", "agent3", END] + }) + .addNode("agent2", agent2, { + ends: ["agent1", "agent3", END] + }) + .addNode("agent3", agent3, { + ends: ["agent1", "agent2", END] + }) + .addEdge(START, "agent1"); + +const network = builder.compile(); +``` + +::: + ### Supervisor In this architecture, we define agents as nodes and add a supervisor node (LLM) that decides which agent nodes should be called next. We use [`Command`](./low_level.md#command) to route execution to the appropriate agent node based on supervisor's decision. This architecture also lends itself well to running multiple agents in parallel or using [map-reduce](../how-tos/graph-api.md#map-reduce-and-the-send-api) pattern. +:::python + ```python from typing import Literal from langchain_openai import ChatOpenAI @@ -211,12 +379,124 @@ builder.add_edge(START, "supervisor") supervisor = builder.compile() ``` +::: + +:::js + +```typescript +import { StateGraph, MessagesZodState, Command, START, END } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; +import { z } from "zod"; + +const model = new ChatOpenAI(); + +const supervisor = async (state: z.infer<typeof MessagesZodState>) => { + // you can pass relevant parts of the state to the LLM (e.g., state.messages) + // to determine which agent to call next. a common pattern is to call the model + // with a structured output (e.g. force it to return an output with a "next_agent" field) + const response = await model.invoke(...); + // route to one of the agents or exit based on the supervisor's decision + // if the supervisor returns "__end__", the graph will finish execution + return new Command({ goto: response.nextAgent }); +}; + +const agent1 = async (state: z.infer<typeof MessagesZodState>) => { + // you can pass relevant parts of the state to the LLM (e.g., state.messages) + // and add any additional logic (different models, custom prompts, structured output, etc.) + const response = await model.invoke(...); + return new Command({ + goto: "supervisor", + update: { messages: [response] }, + }); +}; + +const agent2 = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return new Command({ + goto: "supervisor", + update: { messages: [response] }, + }); +}; + +const builder = new StateGraph(MessagesZodState) + .addNode("supervisor", supervisor, { + ends: ["agent1", "agent2", END] + }) + .addNode("agent1", agent1, { + ends: ["supervisor"] + }) + .addNode("agent2", agent2, { + ends: ["supervisor"] + }) + .addEdge(START, "supervisor"); + +const supervisorGraph = builder.compile(); +``` + +::: + +:::js + +```typescript +import { StateGraph, MessagesZodState, Command, START, END } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; +import { z } from "zod"; + +const model = new ChatOpenAI(); + +const supervisor = async (state: z.infer<typeof MessagesZodState>) => { + // you can pass relevant parts of the state to the LLM (e.g., state.messages) + // to determine which agent to call next. a common pattern is to call the model + // with a structured output (e.g. force it to return an output with a "next_agent" field) + const response = await model.invoke(...); + // route to one of the agents or exit based on the supervisor's decision + // if the supervisor returns "__end__", the graph will finish execution + return new Command({ goto: response.nextAgent }); +}; + +const agent1 = async (state: z.infer<typeof MessagesZodState>) => { + // you can pass relevant parts of the state to the LLM (e.g., state.messages) + // and add any additional logic (different models, custom prompts, structured output, etc.) + const response = await model.invoke(...); + return new Command({ + goto: "supervisor", + update: { messages: [response] }, + }); +}; + +const agent2 = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return new Command({ + goto: "supervisor", + update: { messages: [response] }, + }); +}; + +const builder = new StateGraph(MessagesZodState) + .addNode("supervisor", supervisor, { + ends: ["agent1", "agent2", END] + }) + .addNode("agent1", agent1, { + ends: ["supervisor"] + }) + .addNode("agent2", agent2, { + ends: ["supervisor"] + }) + .addEdge(START, "supervisor"); + +const supervisorGraph = builder.compile(); +``` + +::: + Check out this [tutorial](../tutorials/multi_agent/agent_supervisor.md) for an example of supervisor multi-agent architecture. ### Supervisor (tool-calling) In this variant of the [supervisor](#supervisor) architecture, we define a supervisor [agent](./agentic_concepts.md#agent-architectures) which is responsible for calling sub-agents. The sub-agents are exposed to the supervisor as tools, and the supervisor agent decides which tool to call next. The supervisor agent follows a [standard implementation](./agentic_concepts.md#tool-calling-agent) as an LLM running in a while loop calling tools until it decides to stop. +:::python + ```python from typing import Annotated from langchain_openai import ChatOpenAI @@ -245,12 +525,67 @@ tools = [agent_1, agent_2] supervisor = create_react_agent(model, tools) ``` +::: + +:::js + +```typescript +import { ChatOpenAI } from "@langchain/openai"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const model = new ChatOpenAI(); + +// this is the agent function that will be called as tool +// notice that you can pass the state to the tool via config parameter +const agent1 = tool( + async (_, config) => { + const state = config.configurable?.state; + // you can pass relevant parts of the state to the LLM (e.g., state.messages) + // and add any additional logic (different models, custom prompts, structured output, etc.) + const response = await model.invoke(...); + // return the LLM response as a string (expected tool response format) + // this will be automatically turned to ToolMessage + // by the prebuilt createReactAgent (supervisor) + return response.content; + }, + { + name: "agent1", + description: "Agent 1 description", + schema: z.object({}), + } +); + +const agent2 = tool( + async (_, config) => { + const state = config.configurable?.state; + const response = await model.invoke(...); + return response.content; + }, + { + name: "agent2", + description: "Agent 2 description", + schema: z.object({}), + } +); + +const tools = [agent1, agent2]; +// the simplest way to build a supervisor w/ tool-calling is to use prebuilt ReAct agent graph +// that consists of a tool-calling LLM node (i.e. supervisor) and a tool-executing node +const supervisor = createReactAgent({ llm: model, tools }); +``` + +::: + ### Hierarchical As you add more agents to your system, it might become too hard for the supervisor to manage all of them. The supervisor might start making poor decisions about which agent to call next, or the context might become too complex for a single supervisor to keep track of. In other words, you end up with the same problems that motivated the multi-agent architecture in the first place. To address this, you can design your system _hierarchically_. For example, you can create separate, specialized teams of agents managed by individual supervisors, and a top-level supervisor to manage the teams. +:::python + ```python from typing import Literal from langchain_openai import ChatOpenAI @@ -319,6 +654,97 @@ builder.add_edge("team_2_graph", "top_level_supervisor") graph = builder.compile() ``` +::: + +:::js + +```typescript +import { StateGraph, MessagesZodState, Command, START, END } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; +import { z } from "zod"; + +const model = new ChatOpenAI(); + +// define team 1 (same as the single supervisor example above) + +const team1Supervisor = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return new Command({ goto: response.nextAgent }); +}; + +const team1Agent1 = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return new Command({ + goto: "team1Supervisor", + update: { messages: [response] } + }); +}; + +const team1Agent2 = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return new Command({ + goto: "team1Supervisor", + update: { messages: [response] } + }); +}; + +const team1Builder = new StateGraph(MessagesZodState) + .addNode("team1Supervisor", team1Supervisor, { + ends: ["team1Agent1", "team1Agent2", END] + }) + .addNode("team1Agent1", team1Agent1, { + ends: ["team1Supervisor"] + }) + .addNode("team1Agent2", team1Agent2, { + ends: ["team1Supervisor"] + }) + .addEdge(START, "team1Supervisor"); +const team1Graph = team1Builder.compile(); + +// define team 2 (same as the single supervisor example above) +const team2Supervisor = async (state: z.infer<typeof MessagesZodState>) => { + // ... +}; + +const team2Agent1 = async (state: z.infer<typeof MessagesZodState>) => { + // ... +}; + +const team2Agent2 = async (state: z.infer<typeof MessagesZodState>) => { + // ... +}; + +const team2Builder = new StateGraph(MessagesZodState); +// ... build team2Graph +const team2Graph = team2Builder.compile(); + +// define top-level supervisor + +const topLevelSupervisor = async (state: z.infer<typeof MessagesZodState>) => { + // you can pass relevant parts of the state to the LLM (e.g., state.messages) + // to determine which team to call next. a common pattern is to call the model + // with a structured output (e.g. force it to return an output with a "next_team" field) + const response = await model.invoke(...); + // route to one of the teams or exit based on the supervisor's decision + // if the supervisor returns "__end__", the graph will finish execution + return new Command({ goto: response.nextTeam }); +}; + +const builder = new StateGraph(MessagesZodState) + .addNode("topLevelSupervisor", topLevelSupervisor, { + ends: ["team1Graph", "team2Graph", END] + }) + .addNode("team1Graph", team1Graph) + .addNode("team2Graph", team2Graph) + .addEdge(START, "topLevelSupervisor") + .addEdge("team1Graph", "topLevelSupervisor") + .addEdge("team2Graph", "topLevelSupervisor"); + +const graph = builder.compile(); +``` + +::: + ### Custom multi-agent workflow In this architecture we add individual agents as graph nodes and define the order in which agents are called ahead of time, in a custom workflow. In LangGraph the workflow can be defined in two ways: @@ -327,6 +753,8 @@ In this architecture we add individual agents as graph nodes and define the orde - **Dynamic control flow (Command)**: in LangGraph you can allow LLMs to decide parts of your application control flow. This can be achieved by using [`Command`](./low_level.md#command). A special case of this is a [supervisor tool-calling](#supervisor-tool-calling) architecture. In that case, the tool-calling LLM powering the supervisor agent will make decisions about the order in which the tools (agents) are being called. +:::python + ```python from langchain_openai import ChatOpenAI from langgraph.graph import StateGraph, MessagesState, START @@ -349,6 +777,37 @@ builder.add_edge(START, "agent_1") builder.add_edge("agent_1", "agent_2") ``` +::: + +:::js + +```typescript +import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; +import { z } from "zod"; + +const model = new ChatOpenAI(); + +const agent1 = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return { messages: [response] }; +}; + +const agent2 = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(...); + return { messages: [response] }; +}; + +const builder = new StateGraph(MessagesZodState) + .addNode("agent1", agent1) + .addNode("agent2", agent2) + // define the flow explicitly + .addEdge(START, "agent1") + .addEdge("agent1", "agent2"); +``` + +::: + ## Communication and state management The most important thing when building multi-agent systems is figuring out how the agents communicate. @@ -390,12 +849,27 @@ It can be helpful to indicate which agent a particular AI message is from, espec ### Representing handoffs in message history +:::python Handoffs are typically done via the LLM calling a dedicated [handoff tool](#handoffs-as-tools). This is represented as an [AI message](https://python.langchain.com/docs/concepts/messages/#aimessage) with tool calls that is passed to the next agent (LLM). Most LLM providers don't support receiving AI messages with tool calls **without** corresponding tool messages. +::: + +:::js +Handoffs are typically done via the LLM calling a dedicated [handoff tool](#handoffs-as-tools). This is represented as an [AI message](https://js.langchain.com/docs/concepts/messages/#aimessage) with tool calls that is passed to the next agent (LLM). Most LLM providers don't support receiving AI messages with tool calls **without** corresponding tool messages. +::: You therefore have two options: +:::python + 1. Add an extra [tool message](https://python.langchain.com/docs/concepts/messages/#toolmessage) to the message list, e.g., "Successfully transferred to agent X" 2. Remove the AI message with the tool calls + ::: + +:::js + +1. Add an extra [tool message](https://js.langchain.com/docs/concepts/messages/#toolmessage) to the message list, e.g., "Successfully transferred to agent X" +2. Remove the AI message with the tool calls +::: In practice, we see that most developers opt for option (1). @@ -403,16 +877,25 @@ In practice, we see that most developers opt for option (1). A common practice is to have multiple agents communicating on a shared message list, but only [adding their final messages to the list](#sharing-only-final-results). This means that any intermediate messages (e.g., tool calls) are not saved in this list. -What if you __do__ want to save these messages so that if this particular subagent is invoked in the future you can pass those back in? +What if you **do** want to save these messages so that if this particular subagent is invoked in the future you can pass those back in? There are two high-level approaches to achieve that: +:::python + 1. Store these messages in the shared message list, but filter the list before passing it to the subagent LLM. For example, you can choose to filter out all tool calls from **other** agents. 2. Store a separate message list for each agent (e.g., `alice_messages`) in the subagent's graph state. This would be their "view" of what the message history looks like. +::: + +:::js + +1. Store these messages in the shared message list, but filter the list before passing it to the subagent LLM. For example, you can choose to filter out all tool calls from **other** agents. +2. Store a separate message list for each agent (e.g., `aliceMessages`) in the subagent's graph state. This would be their "view" of what the message history looks like. +::: ### Using different state schemas An agent might need to have a different state schema from the rest of the agents. For example, a search agent might only need to keep track of queries and retrieved documents. There are two ways to achieve this in LangGraph: -- Define [subgraph](./subgraphs.md) agents with a separate state schema. If there are no shared state keys (channels) between the subgraph and the parent graph, it’s important to [add input / output transformations](../how-tos/subgraph.md#different-state-schemas) so that the parent graph knows how to communicate with the subgraphs. -- Define agent node functions with a [private input state schema](../how-tos/graph-api.md/#pass-private-state-between-nodes) that is distinct from the overall graph state schema. This allows passing information that is only needed for executing that particular agent. +- Define [subgraph](./subgraphs.md) agents with a separate state schema. If there are no shared state keys (channels) between the subgraph and the parent graph, it's important to [add input / output transformations](../how-tos/subgraph.ipynb#different-state-schemas) so that the parent graph knows how to communicate with the subgraphs. +- Define agent node functions with a [private input state schema](../how-tos/graph-api.ipynb#pass-private-state-between-nodes) that is distinct from the overall graph state schema. This allows passing information that is only needed for executing that particular agent. diff --git a/docs/docs/concepts/persistence.md b/docs/docs/concepts/persistence.md index 7091cc03d..2481411e7 100644 --- a/docs/docs/concepts/persistence.md +++ b/docs/docs/concepts/persistence.md @@ -5,7 +5,7 @@ search: # Persistence -LangGraph has a built-in persistence layer, implemented through checkpointers. When you compile a graph with a checkpointer, the checkpointer saves a `checkpoint` of the graph state at every super-step. Those checkpoints are saved to a `thread`, which can be accessed after graph execution. Because `threads` allow access to graph's state after execution, several powerful capabilities including human-in-the-loop, memory, time travel, and fault-tolerance are all possible. Below, we'll discuss each of these concepts in more detail. +LangGraph has a built-in persistence layer, implemented through checkpointers. When you compile a graph with a checkpointer, the checkpointer saves a `checkpoint` of the graph state at every super-step. Those checkpoints are saved to a `thread`, which can be accessed after graph execution. Because `threads` allow access to graph's state after execution, several powerful capabilities including human-in-the-loop, memory, time travel, and fault-tolerance are all possible. Below, we'll discuss each of these concepts in more detail. ![Checkpoints](img/persistence/checkpoints.jpg) @@ -17,19 +17,35 @@ LangGraph has a built-in persistence layer, implemented through checkpointers. W A thread is a unique ID or thread identifier assigned to each checkpoint saved by a checkpointer. It contains the accumulated state of a sequence of [runs](./assistants.md#execution). When a run is executed, the [state](../concepts/low_level.md#state) of the underlying graph of the assistant will be persisted to the thread. -When invoking graph with a checkpointer, you **must** specify a `thread_id` as part of the `configurable` portion of the config: +When invoking a graph with a checkpointer, you **must** specify a `thread_id` as part of the `configurable` portion of the config: + +:::python ```python {"configurable": {"thread_id": "1"}} ``` +::: + +:::js + +```typescript +{ + configurable: { + thread_id: "1"; + } +} +``` + +::: + A thread's current and historical state can be retrieved. To persist state, a thread must be created prior to executing a run. The LangGraph Platform API provides several endpoints for creating and managing threads and thread state. See the [API reference](../cloud/reference/api/api_ref.html#tag/threads) for more details. ## Checkpoints The state of a thread at a particular point in time is called a checkpoint. Checkpoint is a snapshot of the graph state saved at each super-step and is represented by `StateSnapshot` object with the following key properties: -- `config`: Config associated with this checkpoint. +- `config`: Config associated with this checkpoint. - `metadata`: Metadata associated with this checkpoint. - `values`: Values of the state channels at this point in time. - `next` A tuple of the node names to execute next in the graph. @@ -39,6 +55,8 @@ Checkpoints are persisted and can be used to restore the state of a thread at a Let's see what checkpoints are saved when a simple graph is invoked as follows: +:::python + ```python from langgraph.graph import StateGraph, START, END from langgraph.checkpoint.memory import InMemorySaver @@ -71,18 +89,111 @@ config = {"configurable": {"thread_id": "1"}} graph.invoke({"foo": ""}, config) ``` +::: + +:::js + +```typescript +import { StateGraph, START, END, MemoryServer } from "@langchain/langgraph"; +import { withLangGraph } from "@langchain/langgraph/zod"; +import { z } from "zod"; + +const State = z.object({ + foo: z.string(), + bar: withLangGraph(z.array(z.string()), { + reducer: { + fn: (x, y) => x.concat(y), + }, + default: () => [], + }), +}); + +const workflow = new StateGraph(State) + .addNode("nodeA", (state) => { + return { foo: "a", bar: ["a"] }; + }) + .addNode("nodeB", (state) => { + return { foo: "b", bar: ["b"] }; + }) + .addEdge(START, "nodeA") + .addEdge("nodeA", "nodeB") + .addEdge("nodeB", END); + +const checkpointer = new MemorySaver(); +const graph = workflow.compile({ checkpointer }); + +const config = { configurable: { thread_id: "1" } }; +await graph.invoke({ foo: "" }, config); +``` + +::: + +:::js + +```typescript +import { StateGraph, START, END, MemoryServer } from "@langchain/langgraph"; +import { withLangGraph } from "@langchain/langgraph/zod"; +import { z } from "zod"; + +const State = z.object({ + foo: z.string(), + bar: withLangGraph(z.array(z.string()), { + reducer: { + fn: (x, y) => x.concat(y), + }, + default: () => [], + }), +}); + +const workflow = new StateGraph(State) + .addNode("nodeA", (state) => { + return { foo: "a", bar: ["a"] }; + }) + .addNode("nodeB", (state) => { + return { foo: "b", bar: ["b"] }; + }) + .addEdge(START, "nodeA") + .addEdge("nodeA", "nodeB") + .addEdge("nodeB", END); + +const checkpointer = new MemorySaver(); +const graph = workflow.compile({ checkpointer }); + +const config = { configurable: { thread_id: "1" } }; +await graph.invoke({ foo: "" }, config); +``` + +::: + +:::python + After we run the graph, we expect to see exactly 4 checkpoints: -* empty checkpoint with `START` as the next node to be executed -* checkpoint with the user input `{'foo': '', 'bar': []}` and `node_a` as the next node to be executed -* checkpoint with the outputs of `node_a` `{'foo': 'a', 'bar': ['a']}` and `node_b` as the next node to be executed -* checkpoint with the outputs of `node_b` `{'foo': 'b', 'bar': ['a', 'b']}` and no next nodes to be executed +- empty checkpoint with `START` as the next node to be executed +- checkpoint with the user input `{'foo': '', 'bar': []}` and `node_a` as the next node to be executed +- checkpoint with the outputs of `node_a` `{'foo': 'a', 'bar': ['a']}` and `node_b` as the next node to be executed +- checkpoint with the outputs of `node_b` `{'foo': 'b', 'bar': ['a', 'b']}` and no next nodes to be executed -Note that the `bar` channel values contain outputs from both nodes as we have a reducer for `bar` channel. +Note that we `bar` channel values contain outputs from both nodes as we have a reducer for `bar` channel. + +::: + +:::js + +After we run the graph, we expect to see exactly 4 checkpoints: + +- empty checkpoint with `START` as the next node to be executed +- checkpoint with the user input `{'foo': '', 'bar': []}` and `nodeA` as the next node to be executed +- checkpoint with the outputs of `nodeA` `{'foo': 'a', 'bar': ['a']}` and `nodeB` as the next node to be executed +- checkpoint with the outputs of `nodeB` `{'foo': 'b', 'bar': ['a', 'b']}` and no next nodes to be executed + +Note that the `bar` channel values contain outputs from both nodes as we have a reducer for the `bar` channel. +::: ### Get state -When interacting with the saved graph state, you **must** specify a [thread identifier](#threads). You can view the *latest* state of the graph by calling `graph.get_state(config)`. This will return a `StateSnapshot` object that corresponds to the latest checkpoint associated with the thread ID provided in the config or a checkpoint associated with a checkpoint ID for the thread, if provided. +:::python +When interacting with the saved graph state, you **must** specify a [thread identifier](#threads). You can view the _latest_ state of the graph by calling `graph.get_state(config)`. This will return a `StateSnapshot` object that corresponds to the latest checkpoint associated with the thread ID provided in the config or a checkpoint associated with a checkpoint ID for the thread, if provided. ```python # get the latest state snapshot @@ -94,6 +205,29 @@ config = {"configurable": {"thread_id": "1", "checkpoint_id": "1ef663ba-28fe-652 graph.get_state(config) ``` +::: + +:::js +When interacting with the saved graph state, you **must** specify a [thread identifier](#threads). You can view the _latest_ state of the graph by calling `graph.getState(config)`. This will return a `StateSnapshot` object that corresponds to the latest checkpoint associated with the thread ID provided in the config or a checkpoint associated with a checkpoint ID for the thread, if provided. + +```typescript +// get the latest state snapshot +const config = { configurable: { thread_id: "1" } }; +await graph.getState(config); + +// get a state snapshot for a specific checkpoint_id +const config = { + configurable: { + thread_id: "1", + checkpoint_id: "1ef663ba-28fe-6528-8002-5a559208592c", + }, +}; +await graph.getState(config); +``` + +::: + +:::python In our example, the output of `get_state` will look like this: ``` @@ -107,8 +241,44 @@ StateSnapshot( ) ``` +::: + +:::js +In our example, the output of `getState` will look like this: + +``` +StateSnapshot { + values: { foo: 'b', bar: ['a', 'b'] }, + next: [], + config: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28fe-6528-8002-5a559208592c' + } + }, + metadata: { + source: 'loop', + writes: { nodeB: { foo: 'b', bar: ['b'] } }, + step: 2 + }, + createdAt: '2024-08-29T19:19:38.821749+00:00', + parentConfig: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28f9-6ec4-8001-31981c2c39f8' + } + }, + tasks: [] +} +``` + +::: + ### Get state history +:::python You can get the full history of the graph execution for a given thread by calling `graph.get_state_history(config)`. This will return a list of `StateSnapshot` objects associated with the thread ID provided in the config. Importantly, the checkpoints will be ordered chronologically with the most recent checkpoint / `StateSnapshot` being the first in the list. ```python @@ -116,6 +286,21 @@ config = {"configurable": {"thread_id": "1"}} list(graph.get_state_history(config)) ``` +::: + +:::js +You can get the full history of the graph execution for a given thread by calling `graph.getStateHistory(config)`. This will return a list of `StateSnapshot` objects associated with the thread ID provided in the config. Importantly, the checkpoints will be ordered chronologically with the most recent checkpoint / `StateSnapshot` being the first in the list. + +```typescript +const config = { configurable: { thread_id: "1" } }; +for await (const state of graph.getStateHistory(config)) { + console.log(state); +} +``` + +::: + +:::python In our example, the output of `get_state_history` will look like this: ``` @@ -158,29 +343,184 @@ In our example, the output of `get_state_history` will look like this: ] ``` +::: + +:::js +In our example, the output of `getStateHistory` will look like this: + +``` +[ + StateSnapshot { + values: { foo: 'b', bar: ['a', 'b'] }, + next: [], + config: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28fe-6528-8002-5a559208592c' + } + }, + metadata: { + source: 'loop', + writes: { nodeB: { foo: 'b', bar: ['b'] } }, + step: 2 + }, + createdAt: '2024-08-29T19:19:38.821749+00:00', + parentConfig: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28f9-6ec4-8001-31981c2c39f8' + } + }, + tasks: [] + }, + StateSnapshot { + values: { foo: 'a', bar: ['a'] }, + next: ['nodeB'], + config: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28f9-6ec4-8001-31981c2c39f8' + } + }, + metadata: { + source: 'loop', + writes: { nodeA: { foo: 'a', bar: ['a'] } }, + step: 1 + }, + createdAt: '2024-08-29T19:19:38.819946+00:00', + parentConfig: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28f4-6b4a-8000-ca575a13d36a' + } + }, + tasks: [ + PregelTask { + id: '6fb7314f-f114-5413-a1f3-d37dfe98ff44', + name: 'nodeB', + error: null, + interrupts: [] + } + ] + }, + StateSnapshot { + values: { foo: '', bar: [] }, + next: ['node_a'], + config: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28f4-6b4a-8000-ca575a13d36a' + } + }, + metadata: { + source: 'loop', + writes: null, + step: 0 + }, + createdAt: '2024-08-29T19:19:38.817813+00:00', + parentConfig: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28f0-6c66-bfff-6723431e8481' + } + }, + tasks: [ + PregelTask { + id: 'f1b14528-5ee5-579c-949b-23ef9bfbed58', + name: 'node_a', + error: null, + interrupts: [] + } + ] + }, + StateSnapshot { + values: { bar: [] }, + next: ['__start__'], + config: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1ef663ba-28f0-6c66-bfff-6723431e8481' + } + }, + metadata: { + source: 'input', + writes: { foo: '' }, + step: -1 + }, + createdAt: '2024-08-29T19:19:38.816205+00:00', + parentConfig: null, + tasks: [ + PregelTask { + id: '6d27aa2e-d72b-5504-a36f-8620e54a76dd', + name: '__start__', + error: null, + interrupts: [] + } + ] + } +] +``` + +::: + ![State](img/persistence/get_state.jpg) ### Replay -It's also possible to play-back a prior graph execution. If we `invoke` a graph with a `thread_id` and a `checkpoint_id`, then we will *re-play* the previously executed steps _before_ a checkpoint that corresponds to the `checkpoint_id`, and only execute the steps _after_ the checkpoint. +It's also possible to play-back a prior graph execution. If we `invoke` a graph with a `thread_id` and a `checkpoint_id`, then we will _re-play_ the previously executed steps _before_ a checkpoint that corresponds to the `checkpoint_id`, and only execute the steps _after_ the checkpoint. -* `thread_id` is the ID of a thread. -* `checkpoint_id` is an identifier that refers to a specific checkpoint within a thread. +- `thread_id` is the ID of a thread. +- `checkpoint_id` is an identifier that refers to a specific checkpoint within a thread. You must pass these when invoking the graph as part of the `configurable` portion of the config: +:::python + ```python config = {"configurable": {"thread_id": "1", "checkpoint_id": "0c62ca34-ac19-445d-bbb0-5b4984975b2a"}} graph.invoke(None, config=config) ``` -Importantly, LangGraph knows whether a particular step has been executed previously. If it has, LangGraph simply *re-plays* that particular step in the graph and does not re-execute the step, but only for the steps _before_ the provided `checkpoint_id`. All of the steps _after_ `checkpoint_id` will be executed (i.e., a new fork), even if they have been executed previously. See this [how to guide on time-travel to learn more about replaying](../how-tos/human_in_the_loop/time-travel.md). +::: + +:::js + +```typescript +const config = { + configurable: { + thread_id: "1", + checkpoint_id: "0c62ca34-ac19-445d-bbb0-5b4984975b2a", + }, +}; +await graph.invoke(null, config); +``` + +::: + +Importantly, LangGraph knows whether a particular step has been executed previously. If it has, LangGraph simply _re-plays_ that particular step in the graph and does not re-execute the step, but only for the steps _before_ the provided `checkpoint_id`. All of the steps _after_ `checkpoint_id` will be executed (i.e., a new fork), even if they have been executed previously. See this [how to guide on time-travel to learn more about replaying](../how-tos/human_in_the_loop/time-travel.md). ![Replay](img/persistence/re_play.png) ### Update state -In addition to re-playing the graph from specific `checkpoints`, we can also *edit* the graph state. We do this using `graph.update_state()`. This method accepts three different arguments: +:::python + +In addition to re-playing the graph from specific `checkpoints`, we can also _edit_ the graph state. We do this using `graph.update_state()`. This method accepts three different arguments: + +::: + +:::js + +In addition to re-playing the graph from specific `checkpoints`, we can also _edit_ the graph state. We do this using `graph.updateState()`. This method accepts three different arguments: + +::: #### `config` @@ -192,6 +532,8 @@ These are the values that will be used to update the state. Note that this updat Let's assume you have defined the state of your graph with the following schema (see full example above): +:::python + ```python from typing import Annotated from typing_extensions import TypedDict @@ -202,29 +544,92 @@ class State(TypedDict): bar: Annotated[list[str], add] ``` +::: + +:::js + +```typescript +import { withLangGraph } from "@langchain/langgraph/zod"; +import { z } from "zod"; + +const State = z.object({ + foo: z.number(), + bar: withLangGraph(z.array(z.string()), { + reducer: { + fn: (x, y) => x.concat(y), + }, + default: () => [], + }), +}); +``` + +::: + Let's now assume the current state of the graph is +:::python + ``` {"foo": 1, "bar": ["a"]} ``` +::: + +:::js + +```typescript +{ foo: 1, bar: ["a"] } +``` + +::: + If you update the state as below: -``` +:::python + +```python graph.update_state(config, {"foo": 2, "bar": ["b"]}) ``` +::: + +:::js + +```typescript +await graph.updateState(config, { foo: 2, bar: ["b"] }); +``` + +::: + Then the new state of the graph will be: +:::python + ``` {"foo": 2, "bar": ["a", "b"]} ``` The `foo` key (channel) is completely changed (because there is no reducer specified for that channel, so `update_state` overwrites it). However, there is a reducer specified for the `bar` key, and so it appends `"b"` to the state of `bar`. +::: + +:::js + +```typescript +{ foo: 2, bar: ["a", "b"] } +``` + +The `foo` key (channel) is completely changed (because there is no reducer specified for that channel, so `updateState` overwrites it). However, there is a reducer specified for the `bar` key, and so it appends `"b"` to the state of `bar`. +::: #### `as_node` +:::python The final thing you can optionally specify when calling `update_state` is `as_node`. If you provided it, the update will be applied as if it came from node `as_node`. If `as_node` is not provided, it will be set to the last node that updated the state, if not ambiguous. The reason this matters is that the next steps to execute depend on the last node to have given an update, so this can be used to control which node executes next. See this [how to guide on time-travel to learn more about forking state](../how-tos/human_in_the_loop/time-travel.md). +::: + +:::js +The final thing you can optionally specify when calling `updateState` is `asNode`. If you provide it, the update will be applied as if it came from node `asNode`. If `asNode` is not provided, it will be set to the last node that updated the state, if not ambiguous. The reason this matters is that the next steps to execute depend on the last node to have given an update, so this can be used to control which node executes next. See this [how to guide on time-travel to learn more about forking state](../how-tos/human_in_the_loop/time-travel.md). +::: ![Update](img/persistence/checkpoints_full_story.jpg) @@ -234,7 +639,7 @@ The final thing you can optionally specify when calling `update_state` is `as_no A [state schema](low_level.md#schema) specifies a set of keys that are populated as a graph is executed. As discussed above, state can be written by a checkpointer to a thread at each graph step, enabling state persistence. -But, what if we want to retain some information *across threads*? Consider the case of a chatbot where we want to retain specific information about the user across *all* chat conversations (e.g., threads) with that user! +But, what if we want to retain some information _across threads_? Consider the case of a chatbot where we want to retain specific information about the user across _all_ chat conversations (e.g., threads) with that user! With checkpointers alone, we cannot share information across threads. This motivates the need for the [`Store`](../reference/store.md#langgraph.store.base.BaseStore) interface. As an illustration, we can define an `InMemoryStore` to store information about a user across threads. We simply compile our graph with a checkpointer, as before, and with our new `in_memory_store` variable. @@ -246,28 +651,73 @@ With checkpointers alone, we cannot share information across threads. This motiv First, let's showcase this in isolation without using LangGraph. +:::python + ```python from langgraph.store.memory import InMemoryStore in_memory_store = InMemoryStore() ``` +::: + +:::js + +```typescript +import { MemoryStore } from "@langchain/langgraph"; + +const memoryStore = new MemoryStore(); +``` + +::: + Memories are namespaced by a `tuple`, which in this specific example will be `(<user_id>, "memories")`. The namespace can be any length and represent anything, does not have to be user specific. -```python +:::python + +```python user_id = "1" namespace_for_memory = (user_id, "memories") ``` +::: + +:::js + +```typescript +const userId = "1"; +const namespaceForMemory = [userId, "memories"]; +``` + +::: + We use the `store.put` method to save memories to our namespace in the store. When we do this, we specify the namespace, as defined above, and a key-value pair for the memory: the key is simply a unique identifier for the memory (`memory_id`) and the value (a dictionary) is the memory itself. +:::python + ```python memory_id = str(uuid.uuid4()) memory = {"food_preference" : "I like pizza"} in_memory_store.put(namespace_for_memory, memory_id, memory) ``` +::: + +:::js + +```typescript +import { v4 as uuidv4 } from "uuid"; + +const memoryId = uuidv4(); +const memory = { food_preference: "I like pizza" }; +await memoryStore.put(namespaceForMemory, memoryId, memory); +``` + +::: + We can read out memories in our namespace using the `store.search` method, which will return all memories for a given user as a list. The most recent memory is the last in the list. +:::python + ```python memories = in_memory_store.search(namespace_for_memory) memories[-1].dict() @@ -279,6 +729,7 @@ memories[-1].dict() ``` Each memory type is a Python class ([`Item`](https://langchain-ai.github.io/langgraph/reference/store/#langgraph.store.base.Item)) with certain attributes. We can access it as a dictionary by converting via `.dict` as above. + The attributes it has are: - `value`: The value (itself a dictionary) of this memory @@ -287,10 +738,39 @@ The attributes it has are: - `created_at`: Timestamp for when this memory was created - `updated_at`: Timestamp for when this memory was updated +::: + +:::js + +```typescript +const memories = await memoryStore.search(namespaceForMemory); +memories[memories.length - 1]; + +// { +// value: { food_preference: 'I like pizza' }, +// key: '07e0caf4-1631-47b7-b15f-65515d4c1843', +// namespace: ['1', 'memories'], +// createdAt: '2024-10-02T17:22:31.590602+00:00', +// updatedAt: '2024-10-02T17:22:31.590605+00:00' +// } +``` + +The attributes it has are: + +- `value`: The value of this memory +- `key`: A unique key for this memory in this namespace +- `namespace`: A list of strings, the namespace of this memory type +- `createdAt`: Timestamp for when this memory was created +- `updatedAt`: Timestamp for when this memory was updated + +::: + ### Semantic Search Beyond simple retrieval, the store also supports semantic search, allowing you to find memories based on meaning rather than exact matches. To enable this, configure the store with an embedding model: +:::python + ```python from langchain.embeddings import init_embeddings @@ -303,8 +783,28 @@ store = InMemoryStore( ) ``` +::: + +:::js + +```typescript +import { OpenAIEmbeddings } from "@langchain/openai"; + +const store = new InMemoryStore({ + index: { + embeddings: new OpenAIEmbeddings({ model: "text-embedding-3-small" }), + dims: 1536, + fields: ["food_preference", "$"], // Fields to embed + }, +}); +``` + +::: + Now when searching, you can use natural language queries to find relevant memories: +:::python + ```python # Find memories about food preferences # (This can be done after putting memories into the store) @@ -315,8 +815,25 @@ memories = store.search( ) ``` +::: + +:::js + +```typescript +// Find memories about food preferences +// (This can be done after putting memories into the store) +const memories = await store.search(namespaceForMemory, { + query: "What does the user like to eat?", + limit: 3, // Return top 3 matches +}); +``` + +::: + You can control which parts of your memories get embedded by configuring the `fields` parameter or by specifying the `index` parameter when storing memories: +:::python + ```python # Store with specific fields to embed store.put( @@ -338,9 +855,37 @@ store.put( ) ``` +::: + +:::js + +```typescript +// Store with specific fields to embed +await store.put( + namespaceForMemory, + uuidv4(), + { + food_preference: "I love Italian cuisine", + context: "Discussing dinner plans", + }, + { index: ["food_preference"] } // Only embed "food_preferences" field +); + +// Store without embedding (still retrievable, but not searchable) +await store.put( + namespaceForMemory, + uuidv4(), + { system_info: "Last updated: 2024-01-01" }, + { index: false } +); +``` + +::: + ### Using in LangGraph -With this all in place, we use the `in_memory_store` in LangGraph. The `in_memory_store` works hand-in-hand with the checkpointer: the checkpointer saves state to threads, as discussed above, and the `in_memory_store` allows us to store arbitrary information for access *across* threads. We compile the graph with both the checkpointer and the `in_memory_store` as follows. +:::python +With this all in place, we use the `in_memory_store` in LangGraph. The `in_memory_store` works hand-in-hand with the checkpointer: the checkpointer saves state to threads, as discussed above, and the `in_memory_store` allows us to store arbitrary information for access _across_ threads. We compile the graph with both the checkpointer and the `in_memory_store` as follows. ```python from langgraph.checkpoint.memory import InMemorySaver @@ -354,8 +899,29 @@ checkpointer = InMemorySaver() graph = graph.compile(checkpointer=checkpointer, store=in_memory_store) ``` +::: + +:::js +With this all in place, we use the `memoryStore` in LangGraph. The `memoryStore` works hand-in-hand with the checkpointer: the checkpointer saves state to threads, as discussed above, and the `memoryStore` allows us to store arbitrary information for access _across_ threads. We compile the graph with both the checkpointer and the `memoryStore` as follows. + +```typescript +import { MemorySaver } from "@langchain/langgraph"; + +// We need this because we want to enable threads (conversations) +const checkpointer = new MemorySaver(); + +// ... Define the graph ... + +// Compile the graph with the checkpointer and store +const graph = workflow.compile({ checkpointer, store: memoryStore }); +``` + +::: + We invoke the graph with a `thread_id`, as before, and also with a `user_id`, which we'll use to namespace our memories to this particular user as we showed above. +:::python + ```python # Invoke the graph user_id = "1" @@ -368,19 +934,40 @@ for update in graph.stream( print(update) ``` -We can access the `in_memory_store` and the `user_id` in *any node* by passing `store: BaseStore` and `config: RunnableConfig` as node arguments. Here's how we might use semantic search in a node to find relevant memories: +::: + +:::js + +```typescript +// Invoke the graph +const userId = "1"; +const config = { configurable: { thread_id: "1", user_id: userId } }; + +// First let's just say hi to the AI +for await (const update of await graph.stream( + { messages: [{ role: "user", content: "hi" }] }, + { ...config, streamMode: "updates" } +)) { + console.log(update); +} +``` + +::: + +:::python +We can access the `in_memory_store` and the `user_id` in _any node_ by passing `store: BaseStore` and `config: RunnableConfig` as node arguments. Here's how we might use semantic search in a node to find relevant memories: ```python def update_memory(state: MessagesState, config: RunnableConfig, *, store: BaseStore): - + # Get the user id from the config user_id = config["configurable"]["user_id"] - + # Namespace the memory namespace = (user_id, "memories") - + # ... Analyze conversation and create a new memory - + # Create a new memory ID memory_id = str(uuid.uuid4()) @@ -389,8 +976,46 @@ def update_memory(state: MessagesState, config: RunnableConfig, *, store: BaseSt ``` +::: + +:::js +We can access the `memoryStore` and the `user_id` in _any node_ by accessing `config` and `store` as node arguments. Here's how we might use semantic search in a node to find relevant memories: + +```typescript +import { + LangGraphRunnableConfig, + BaseStore, + MessagesZodState, +} from "@langchain/langgraph"; +import { z } from "zod"; + +const updateMemory = async ( + state: z.infer<typeof MessagesZodState>, + config: LangGraphRunnableConfig, + store: BaseStore +) => { + // Get the user id from the config + const userId = config.configurable?.user_id; + + // Namespace the memory + const namespace = [userId, "memories"]; + + // ... Analyze conversation and create a new memory + + // Create a new memory ID + const memoryId = uuidv4(); + + // We create a new memory + await store.put(namespace, memoryId, { memory }); +}; +``` + +::: + As we showed above, we can also access the store in any node and use the `store.search` method to get memories. Recall the memories are returned as a list of objects that can be converted to a dictionary. +:::python + ```python memories[-1].dict() {'value': {'food_preference': 'I like pizza'}, @@ -400,8 +1025,27 @@ memories[-1].dict() 'updated_at': '2024-10-02T17:22:31.590605+00:00'} ``` +::: + +:::js + +```typescript +memories[memories.length - 1]; +// { +// value: { food_preference: 'I like pizza' }, +// key: '07e0caf4-1631-47b7-b15f-65515d4c1843', +// namespace: ['1', 'memories'], +// createdAt: '2024-10-02T17:22:31.590602+00:00', +// updatedAt: '2024-10-02T17:22:31.590605+00:00' +// } +``` + +::: + We can access the memories and use them in our model call. +:::python + ```python def call_model(state: MessagesState, config: RunnableConfig, *, store: BaseStore): # Get the user id from the config @@ -409,7 +1053,7 @@ def call_model(state: MessagesState, config: RunnableConfig, *, store: BaseStore # Namespace the memory namespace = (user_id, "memories") - + # Search based on the most recent message memories = store.search( namespace, @@ -417,11 +1061,42 @@ def call_model(state: MessagesState, config: RunnableConfig, *, store: BaseStore limit=3 ) info = "\n".join([d.value["memory"] for d in memories]) - + # ... Use memories in the model call ``` -If we create a new thread, we can still access the same memories so long as the `user_id` is the same. +::: + +:::js + +```typescript +const callModel = async ( + state: z.infer<typeof MessagesZodState>, + config: LangGraphRunnableConfig, + store: BaseStore +) => { + // Get the user id from the config + const userId = config.configurable?.user_id; + + // Namespace the memory + const namespace = [userId, "memories"]; + + // Search based on the most recent message + const memories = await store.search(namespace, { + query: state.messages[state.messages.length - 1].content, + limit: 3, + }); + const info = memories.map((d) => d.value.memory).join("\n"); + + // ... Use memories in the model call +}; +``` + +::: + +If we create a new thread, we can still access the same memories so long as the `user_id` is the same. + +:::python ```python # Invoke the graph @@ -434,6 +1109,25 @@ for update in graph.stream( print(update) ``` +::: + +:::js + +```typescript +// Invoke the graph +const config = { configurable: { thread_id: "2", user_id: "1" } }; + +// Let's say hi again +for await (const update of await graph.stream( + { messages: [{ role: "user", content: "hi, tell me about my memories" }] }, + { ...config, streamMode: "updates" } +)) { + console.log(update); +} +``` + +::: + When we use the LangGraph Platform, either locally (e.g., in LangGraph Studio) or with LangGraph Platform, the base store is available to use by default and does not need to be specified during graph compilation. To enable semantic search, however, you **do** need to configure the indexing settings in your `langgraph.json` file. For example: ```json @@ -453,35 +1147,61 @@ See the [deployment guide](../cloud/deployment/semantic_search.md) for more deta ## Checkpointer libraries -Under the hood, checkpointing is powered by checkpointer objects that conform to [BaseCheckpointSaver][langgraph.checkpoint.base.BaseCheckpointSaver] interface. LangGraph provides several checkpointer implementations, all implemented via standalone, installable libraries: +Under the hood, checkpointing is powered by checkpointer objects that conform to @[BaseCheckpointSaver] interface. LangGraph provides several checkpointer implementations, all implemented via standalone, installable libraries: -* `langgraph-checkpoint`: The base interface for checkpointer savers ([BaseCheckpointSaver][langgraph.checkpoint.base.BaseCheckpointSaver]) and serialization/deserialization interface ([SerializerProtocol][langgraph.checkpoint.serde.base.SerializerProtocol]). Includes in-memory checkpointer implementation ([InMemorySaver][langgraph.checkpoint.memory.InMemorySaver]) for experimentation. LangGraph comes with `langgraph-checkpoint` included. -* `langgraph-checkpoint-sqlite`: An implementation of LangGraph checkpointer that uses SQLite database ([SqliteSaver][langgraph.checkpoint.sqlite.SqliteSaver] / [AsyncSqliteSaver][langgraph.checkpoint.sqlite.aio.AsyncSqliteSaver]). Ideal for experimentation and local workflows. Needs to be installed separately. -* `langgraph-checkpoint-postgres`: An advanced checkpointer that uses Postgres database ([PostgresSaver][langgraph.checkpoint.postgres.PostgresSaver] / [AsyncPostgresSaver][langgraph.checkpoint.postgres.aio.AsyncPostgresSaver]), used in LangGraph Platform. Ideal for using in production. Needs to be installed separately. +:::python +- `langgraph-checkpoint`: The base interface for checkpointer savers (@[BaseCheckpointSaver]) and serialization/deserialization interface (@[SerializerProtocol][SerializerProtocol]). Includes in-memory checkpointer implementation (@[InMemorySaver][InMemorySaver]) for experimentation. LangGraph comes with `langgraph-checkpoint` included. +- `langgraph-checkpoint-sqlite`: An implementation of LangGraph checkpointer that uses SQLite database (@[SqliteSaver][SqliteSaver] / @[AsyncSqliteSaver]). Ideal for experimentation and local workflows. Needs to be installed separately. +- `langgraph-checkpoint-postgres`: An advanced checkpointer that uses Postgres database (@[PostgresSaver][PostgresSaver] / @[AsyncPostgresSaver]), used in LangGraph Platform. Ideal for using in production. Needs to be installed separately. + +::: + +:::js + +- `@langchain/langgraph-checkpoint`: The base interface for checkpointer savers (@[BaseCheckpointSaver][BaseCheckpointSaver]) and serialization/deserialization interface (@[SerializerProtocol][SerializerProtocol]). Includes in-memory checkpointer implementation (@[InMemorySaver) for experimentation. LangGraph comes with `@langchain/langgraph-checkpoint` included. +- `@langchain/langgraph-checkpoint-sqlite`: An implementation of LangGraph checkpointer that uses SQLite database (@[SqliteSaver]). Ideal for experimentation and local workflows. Needs to be installed separately. +- `@langchain/langgraph-checkpoint-postgres`: An advanced checkpointer that uses Postgres database (@[PostgresSaver]), used in LangGraph Platform. Ideal for using in production. Needs to be installed separately. + +::: ### Checkpointer interface -Each checkpointer conforms to [BaseCheckpointSaver][langgraph.checkpoint.base.BaseCheckpointSaver] interface and implements the following methods: +:::python +Each checkpointer conforms to @[BaseCheckpointSaver] interface and implements the following methods: -* `.put` - Store a checkpoint with its configuration and metadata. -* `.put_writes` - Store intermediate writes linked to a checkpoint (i.e. [pending writes](#pending-writes)). -* `.get_tuple` - Fetch a checkpoint tuple using for a given configuration (`thread_id` and `checkpoint_id`). This is used to populate `StateSnapshot` in `graph.get_state()`. -* `.list` - List checkpoints that match a given configuration and filter criteria. This is used to populate state history in `graph.get_state_history()` +- `.put` - Store a checkpoint with its configuration and metadata. +- `.put_writes` - Store intermediate writes linked to a checkpoint (i.e. [pending writes](#pending-writes)). +- `.get_tuple` - Fetch a checkpoint tuple using for a given configuration (`thread_id` and `checkpoint_id`). This is used to populate `StateSnapshot` in `graph.get_state()`. +- `.list` - List checkpoints that match a given configuration and filter criteria. This is used to populate state history in `graph.get_state_history()` If the checkpointer is used with asynchronous graph execution (i.e. executing the graph via `.ainvoke`, `.astream`, `.abatch`), asynchronous versions of the above methods will be used (`.aput`, `.aput_writes`, `.aget_tuple`, `.alist`). -!!! note Note +!!! note + For running your graph asynchronously, you can use `InMemorySaver`, or async versions of Sqlite/Postgres checkpointers -- `AsyncSqliteSaver` / `AsyncPostgresSaver` checkpointers. +::: + +:::js +Each checkpointer conforms to the @[BaseCheckpointSaver][BaseCheckpointSaver] interface and implements the following methods: + +- `.put` - Store a checkpoint with its configuration and metadata. +- `.putWrites` - Store intermediate writes linked to a checkpoint (i.e. [pending writes](#pending-writes)). +- `.getTuple` - Fetch a checkpoint tuple using for a given configuration (`thread_id` and `checkpoint_id`). This is used to populate `StateSnapshot` in `graph.getState()`. +- `.list` - List checkpoints that match a given configuration and filter criteria. This is used to populate state history in `graph.getStateHistory()` +::: + ### Serializer When checkpointers save the graph state, they need to serialize the channel values in the state. This is done using serializer objects. -`langgraph_checkpoint` defines [protocol][langgraph.checkpoint.serde.base.SerializerProtocol] for implementing serializers provides a default implementation ([JsonPlusSerializer][langgraph.checkpoint.serde.jsonplus.JsonPlusSerializer]) that handles a wide variety of types, including LangChain and LangGraph primitives, datetimes, enums and more. + +:::python +`langgraph_checkpoint` defines @[protocol][SerializerProtocol] for implementing serializers provides a default implementation (@[JsonPlusSerializer][JsonPlusSerializer]) that handles a wide variety of types, including LangChain and LangGraph primitives, datetimes, enums and more. #### Serialization with `pickle` -The default serializer, [`JsonPlusSerializer`][langgraph.checkpoint.serde.jsonplus.JsonPlusSerializer], uses ormsgpack and JSON under the hood, which is not suitable for all types of objects. +The default serializer, @[`JsonPlusSerializer`][JsonPlusSerializer], uses ormsgpack and JSON under the hood, which is not suitable for all types of objects. If you want to fallback to pickle for objects not currently supported by our msgpack encoder (such as Pandas dataframes), you can use the `pickle_fallback` argument of the `JsonPlusSerializer`: @@ -498,7 +1218,7 @@ graph.compile( #### Encryption -Checkpointers can optionally encrypt all persisted state. To enable this, pass an instance of [`EncryptedSerializer`][langgraph.checkpoint.serde.encrypted.EncryptedSerializer] to the `serde` argument of any `BaseCheckpointSaver` implementation. The easiest way to create an encrypted serializer is via [`from_pycryptodome_aes`][langgraph.checkpoint.serde.encrypted.EncryptedSerializer.from_pycryptodome_aes], which reads the AES key from the `LANGGRAPH_AES_KEY` environment variable (or accepts a `key` argument): +Checkpointers can optionally encrypt all persisted state. To enable this, pass an instance of @[`EncryptedSerializer`][EncryptedSerializer] to the `serde` argument of any `BaseCheckpointSaver` implementation. The easiest way to create an encrypted serializer is via @[`from_pycryptodome_aes`][from_pycryptodome_aes], which reads the AES key from the `LANGGRAPH_AES_KEY` environment variable (or accepts a `key` argument): ```python import sqlite3 @@ -519,7 +1239,12 @@ checkpointer = PostgresSaver.from_conn_string("postgresql://...", serde=serde) checkpointer.setup() ``` -When running on LangGraph Platform, encryption is automatically enabled whenever `LANGGRAPH_AES_KEY` is present, so you only need to provide the environment variable. Other encryption schemes can be used by implementing [`CipherProtocol`][langgraph.checkpoint.serde.base.CipherProtocol] and supplying it to `EncryptedSerializer`. +When running on LangGraph Platform, encryption is automatically enabled whenever `LANGGRAPH_AES_KEY` is present, so you only need to provide the environment variable. Other encryption schemes can be used by implementing @[`CipherProtocol`][CipherProtocol] and supplying it to `EncryptedSerializer`. +::: + +:::js +`@langchain/langgraph-checkpoint` defines protocol for implementing serializers and provides a default implementation that handles a wide variety of types, including LangChain and LangGraph primitives, datetimes, enums and more. +::: ## Capabilities @@ -529,7 +1254,7 @@ First, checkpointers facilitate [human-in-the-loop workflows](agentic_concepts.m ### Memory -Second, checkpointers allow for ["memory"](../concepts/memory.md) between interactions. In the case of repeated human interactions (like conversations) any follow up messages can be sent to that thread, which will retain its memory of previous ones. See [Add memory](../how-tos/memory/add-memory.md) for information on how to add and manage conversation memory using checkpointers. +Second, checkpointers allow for ["memory"](../concepts/memory.md) between interactions. In the case of repeated human interactions (like conversations) any follow up messages can be sent to that thread, which will retain its memory of previous ones. See [Add memory](../how-tos/memory/add-memory.md) for information on how to add and manage conversation memory using checkpointers. ### Time Travel diff --git a/docs/docs/concepts/pregel.md b/docs/docs/concepts/pregel.md index d5a87d87a..84cf50ee5 100644 --- a/docs/docs/concepts/pregel.md +++ b/docs/docs/concepts/pregel.md @@ -5,13 +5,31 @@ search: # LangGraph runtime -[Pregel][langgraph.pregel.Pregel] implements LangGraph's runtime, managing the execution of LangGraph applications. +:::python +@[Pregel] implements LangGraph's runtime, managing the execution of LangGraph applications. -Compiling a [StateGraph][langgraph.graph.StateGraph] or creating an [entrypoint][langgraph.func.entrypoint] produces a [Pregel][langgraph.pregel.Pregel] instance that can be invoked with input. +Compiling a @[StateGraph][StateGraph] or creating an @[entrypoint][entrypoint] produces a @[Pregel] instance that can be invoked with input. +::: + +:::js +@[Pregel] implements LangGraph's runtime, managing the execution of LangGraph applications. + +Compiling a @[StateGraph][StateGraph] or creating an @[entrypoint][entrypoint] produces a @[Pregel] instance that can be invoked with input. +::: This guide explains the runtime at a high level and provides instructions for directly implementing applications with Pregel. -> **Note:** The [Pregel][langgraph.pregel.Pregel] runtime is named after [Google's Pregel algorithm](https://research.google/pubs/pub37252/), which describes an efficient method for large-scale parallel computation using graphs. +:::python + +> **Note:** The @[Pregel] runtime is named after [Google's Pregel algorithm](https://research.google/pubs/pub37252/), which describes an efficient method for large-scale parallel computation using graphs. + +::: + +:::js + +> **Note:** The @[Pregel] runtime is named after [Google's Pregel algorithm](https://research.google/pubs/pub37252/), which describes an efficient method for large-scale parallel computation using graphs. + +::: ## Overview @@ -33,21 +51,36 @@ An **actor** is a `PregelNode`. It subscribes to channels, reads data from them, Channels are used to communicate between actors (PregelNodes). Each channel has a value type, an update type, and an update function – which takes a sequence of updates and modifies the stored value. Channels can be used to send data from one chain to another, or to send data from a chain to itself in a future step. LangGraph provides a number of built-in channels: -- [LastValue][langgraph.channels.LastValue]: The default channel, stores the last value sent to the channel, useful for input and output values, or for sending data from one step to the next. -- [Topic][langgraph.channels.Topic]: A configurable PubSub Topic, useful for sending multiple values between **actors**, or for accumulating output. Can be configured to deduplicate values or to accumulate values over the course of multiple steps. -- [BinaryOperatorAggregate][langgraph.channels.BinaryOperatorAggregate]: stores a persistent value, updated by applying a binary operator to the current value and each update sent to the channel, useful for computing aggregates over multiple steps; e.g.,`total = BinaryOperatorAggregate(int, operator.add)` +:::python + +- @[LastValue][LastValue]: The default channel, stores the last value sent to the channel, useful for input and output values, or for sending data from one step to the next. +- @[Topic][Topic]: A configurable PubSub Topic, useful for sending multiple values between **actors**, or for accumulating output. Can be configured to deduplicate values or to accumulate values over the course of multiple steps. +- @[BinaryOperatorAggregate][BinaryOperatorAggregate]: stores a persistent value, updated by applying a binary operator to the current value and each update sent to the channel, useful for computing aggregates over multiple steps; e.g.,`total = BinaryOperatorAggregate(int, operator.add)` + ::: + +:::js + +- @[LastValue]: The default channel, stores the last value sent to the channel, useful for input and output values, or for sending data from one step to the next. +- @[Topic]: A configurable PubSub Topic, useful for sending multiple values between **actors**, or for accumulating output. Can be configured to deduplicate values or to accumulate values over the course of multiple steps. +- @[BinaryOperatorAggregate]: stores a persistent value, updated by applying a binary operator to the current value and each update sent to the channel, useful for computing aggregates over multiple steps; e.g.,`total = BinaryOperatorAggregate(int, operator.add)` + ::: ## Examples -While most users will interact with Pregel through the [StateGraph][langgraph.graph.StateGraph] API or -the [entrypoint][langgraph.func.entrypoint] decorator, it is possible to interact with Pregel directly. +:::python +While most users will interact with Pregel through the @[StateGraph][StateGraph] API or the @[entrypoint][entrypoint] decorator, it is possible to interact with Pregel directly. +::: + +:::js +While most users will interact with Pregel through the @[StateGraph] API or the @[entrypoint] decorator, it is possible to interact with Pregel directly. +::: Below are a few different examples to give you a sense of the Pregel API. === "Single node" + :::python ```python - from langgraph.channels import EphemeralValue from langgraph.pregel import Pregel, NodeBuilder @@ -73,9 +106,39 @@ Below are a few different examples to give you a sense of the Pregel API. ```con {'b': 'foofoo'} ``` + ::: + + :::js + ```typescript + import { EphemeralValue } from "@langchain/langgraph/channels"; + import { Pregel, NodeBuilder } from "@langchain/langgraph/pregel"; + + const node1 = new NodeBuilder() + .subscribeOnly("a") + .do((x: string) => x + x) + .writeTo("b"); + + const app = new Pregel({ + nodes: { node1 }, + channels: { + a: new EphemeralValue<string>(), + b: new EphemeralValue<string>(), + }, + inputChannels: ["a"], + outputChannels: ["b"], + }); + + await app.invoke({ a: "foo" }); + ``` + + ```console + { b: 'foofoo' } + ``` + ::: === "Multiple nodes" + :::python ```python from langgraph.channels import LastValue, EphemeralValue from langgraph.pregel import Pregel, NodeBuilder @@ -110,9 +173,45 @@ Below are a few different examples to give you a sense of the Pregel API. ```con {'b': 'foofoo', 'c': 'foofoofoofoo'} ``` + ::: + + :::js + ```typescript + import { LastValue, EphemeralValue } from "@langchain/langgraph/channels"; + import { Pregel, NodeBuilder } from "@langchain/langgraph/pregel"; + + const node1 = new NodeBuilder() + .subscribeOnly("a") + .do((x: string) => x + x) + .writeTo("b"); + + const node2 = new NodeBuilder() + .subscribeOnly("b") + .do((x: string) => x + x) + .writeTo("c"); + + const app = new Pregel({ + nodes: { node1, node2 }, + channels: { + a: new EphemeralValue<string>(), + b: new LastValue<string>(), + c: new EphemeralValue<string>(), + }, + inputChannels: ["a"], + outputChannels: ["b", "c"], + }); + + await app.invoke({ a: "foo" }); + ``` + + ```console + { b: 'foofoo', c: 'foofoofoofoo' } + ``` + ::: === "Topic" + :::python ```python from langgraph.channels import EphemeralValue, Topic from langgraph.pregel import Pregel, NodeBuilder @@ -146,11 +245,47 @@ Below are a few different examples to give you a sense of the Pregel API. ```pycon {'c': ['foofoo', 'foofoofoofoo']} ``` + ::: + + :::js + ```typescript + import { EphemeralValue, Topic } from "@langchain/langgraph/channels"; + import { Pregel, NodeBuilder } from "@langchain/langgraph/pregel"; + + const node1 = new NodeBuilder() + .subscribeOnly("a") + .do((x: string) => x + x) + .writeTo("b", "c"); + + const node2 = new NodeBuilder() + .subscribeTo("b") + .do((x: { b: string }) => x.b + x.b) + .writeTo("c"); + + const app = new Pregel({ + nodes: { node1, node2 }, + channels: { + a: new EphemeralValue<string>(), + b: new EphemeralValue<string>(), + c: new Topic<string>({ accumulate: true }), + }, + inputChannels: ["a"], + outputChannels: ["c"], + }); + + await app.invoke({ a: "foo" }); + ``` + + ```console + { c: ['foofoo', 'foofoofoofoo'] } + ``` + ::: === "BinaryOperatorAggregate" This examples demonstrates how to use the BinaryOperatorAggregate channel to implement a reducer. + :::python ```python from langgraph.channels import EphemeralValue, BinaryOperatorAggregate from langgraph.pregel import Pregel, NodeBuilder @@ -187,12 +322,53 @@ Below are a few different examples to give you a sense of the Pregel API. app.invoke({"a": "foo"}) ``` + ::: + + :::js + ```typescript + import { EphemeralValue, BinaryOperatorAggregate } from "@langchain/langgraph/channels"; + import { Pregel, NodeBuilder } from "@langchain/langgraph/pregel"; + + const node1 = new NodeBuilder() + .subscribeOnly("a") + .do((x: string) => x + x) + .writeTo("b", "c"); + + const node2 = new NodeBuilder() + .subscribeOnly("b") + .do((x: string) => x + x) + .writeTo("c"); + + const reducer = (current: string, update: string) => { + if (current) { + return current + " | " + update; + } else { + return update; + } + }; + + const app = new Pregel({ + nodes: { node1, node2 }, + channels: { + a: new EphemeralValue<string>(), + b: new EphemeralValue<string>(), + c: new BinaryOperatorAggregate<string>({ operator: reducer }), + }, + inputChannels: ["a"], + outputChannels: ["c"], + }); + + await app.invoke({ a: "foo" }); + ``` + ::: === "Cycle" + :::python + This example demonstrates how to introduce a cycle in the graph, by having a chain write to a channel it subscribes to. Execution will continue - until a None value is written to the channel. + until a `None` value is written to the channel. ```python from langgraph.channels import EphemeralValue @@ -219,6 +395,39 @@ Below are a few different examples to give you a sense of the Pregel API. ```pycon {'value': 'aaaaaaaaaaaaaaaa'} ``` + ::: + + :::js + + This example demonstrates how to introduce a cycle in the graph, by having + a chain write to a channel it subscribes to. Execution will continue + until a `null` value is written to the channel. + + ```typescript + import { EphemeralValue } from "@langchain/langgraph/channels"; + import { Pregel, NodeBuilder, ChannelWriteEntry } from "@langchain/langgraph/pregel"; + + const exampleNode = new NodeBuilder() + .subscribeOnly("value") + .do((x: string) => x.length < 10 ? x + x : null) + .writeTo(new ChannelWriteEntry("value", { skipNone: true })); + + const app = new Pregel({ + nodes: { exampleNode }, + channels: { + value: new EphemeralValue<string>(), + }, + inputChannels: ["value"], + outputChannels: ["value"], + }); + + await app.invoke({ value: "a" }); + ``` + + ```console + { value: 'aaaaaaaaaaaaaaaa' } + ``` + ::: ## High-level API @@ -226,7 +435,9 @@ LangGraph provides two high-level APIs for creating a Pregel application: the [S === "StateGraph (Graph API)" - The [StateGraph (Graph API)][langgraph.graph.StateGraph] is a higher-level abstraction that simplifies the creation of Pregel applications. It allows you to define a graph of nodes and edges. When you compile the graph, the StateGraph API automatically creates the Pregel application for you. + :::python + + The @[StateGraph (Graph API)][StateGraph] is a higher-level abstraction that simplifies the creation of Pregel applications. It allows you to define a graph of nodes and edges. When you compile the graph, the StateGraph API automatically creates the Pregel application for you. ```python from typing import TypedDict, Optional @@ -258,9 +469,53 @@ LangGraph provides two high-level APIs for creating a Pregel application: the [S # This will return a Pregel instance. graph = builder.compile() ``` + ::: + + :::js + + The @[StateGraph (Graph API)][StateGraph] is a higher-level abstraction that simplifies the creation of Pregel applications. It allows you to define a graph of nodes and edges. When you compile the graph, the StateGraph API automatically creates the Pregel application for you. + + ```typescript + import { START, StateGraph } from "@langchain/langgraph"; + + interface Essay { + topic: string; + content?: string; + score?: number; + } + + const writeEssay = (essay: Essay) => { + return { + content: `Essay about ${essay.topic}`, + }; + }; + + const scoreEssay = (essay: Essay) => { + return { + score: 10 + }; + }; + + const builder = new StateGraph<Essay>({ + channels: { + topic: null, + content: null, + score: null, + } + }) + .addNode("writeEssay", writeEssay) + .addNode("scoreEssay", scoreEssay) + .addEdge(START, "writeEssay"); + + // Compile the graph. + // This will return a Pregel instance. + const graph = builder.compile(); + ``` + ::: The compiled Pregel instance will be associated with a list of nodes and channels. You can inspect the nodes and channels by printing them. + :::python ```python print(graph.nodes) ``` @@ -294,11 +549,53 @@ LangGraph provides two high-level APIs for creating a Pregel application: the [S 'branch:score_essay:__self__:score_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d8b400>, 'start:write_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d8b280>} ``` + ::: + + :::js + ```typescript + console.log(graph.nodes); + ``` + + You will see something like this: + + ```console + { + __start__: PregelNode { ... }, + writeEssay: PregelNode { ... }, + scoreEssay: PregelNode { ... } + } + ``` + + ```typescript + console.log(graph.channels); + ``` + + You should see something like this + + ```console + { + topic: LastValue { ... }, + content: LastValue { ... }, + score: LastValue { ... }, + __start__: EphemeralValue { ... }, + writeEssay: EphemeralValue { ... }, + scoreEssay: EphemeralValue { ... }, + 'branch:__start__:__self__:writeEssay': EphemeralValue { ... }, + 'branch:__start__:__self__:scoreEssay': EphemeralValue { ... }, + 'branch:writeEssay:__self__:writeEssay': EphemeralValue { ... }, + 'branch:writeEssay:__self__:scoreEssay': EphemeralValue { ... }, + 'branch:scoreEssay:__self__:writeEssay': EphemeralValue { ... }, + 'branch:scoreEssay:__self__:scoreEssay': EphemeralValue { ... }, + 'start:writeEssay': EphemeralValue { ... } + } + ``` + ::: === "Functional API" - In the [Functional API](functional_api.md), you can use an [`entrypoint`][langgraph.func.entrypoint] to create - a Pregel application. The `entrypoint` decorator allows you to define a function that takes input and returns output. + :::python + + In the [Functional API](functional_api.md), you can use an @[`entrypoint`][entrypoint] to create a Pregel application. The `entrypoint` decorator allows you to define a function that takes input and returns output. ```python from typing import TypedDict, Optional @@ -332,3 +629,47 @@ LangGraph provides two high-level APIs for creating a Pregel application: the [S Channels: {'__start__': <langgraph.channels.ephemeral_value.EphemeralValue object at 0x7d05e2c906c0>, '__end__': <langgraph.channels.last_value.LastValue object at 0x7d05e2c90c40>, '__previous__': <langgraph.channels.last_value.LastValue object at 0x7d05e1007280>} ``` + ::: + + :::js + + In the [Functional API](functional_api.md), you can use an @[`entrypoint`][entrypoint] to create a Pregel application. The `entrypoint` decorator allows you to define a function that takes input and returns output. + + ```typescript + import { MemorySaver } from "@langchain/langgraph"; + import { entrypoint } from "@langchain/langgraph/func"; + + interface Essay { + topic: string; + content?: string; + score?: number; + } + + const checkpointer = new MemorySaver(); + + const writeEssay = entrypoint( + { checkpointer, name: "writeEssay" }, + async (essay: Essay) => { + return { + content: `Essay about ${essay.topic}`, + }; + } + ); + + console.log("Nodes: "); + console.log(writeEssay.nodes); + console.log("Channels: "); + console.log(writeEssay.channels); + ``` + + ```console + Nodes: + { writeEssay: PregelNode { ... } } + Channels: + { + __start__: EphemeralValue { ... }, + __end__: LastValue { ... }, + __previous__: LastValue { ... } + } + ``` + ::: diff --git a/docs/docs/concepts/sdk.md b/docs/docs/concepts/sdk.md index 221fcccde..e3d088d08 100644 --- a/docs/docs/concepts/sdk.md +++ b/docs/docs/concepts/sdk.md @@ -5,25 +5,20 @@ search: # LangGraph SDK -LangGraph Platform provides both a Python SDK for interacting with [LangGraph Server](./langgraph_server.md). +:::python +LangGraph Platform provides a python SDK for interacting with [LangGraph Server](./langgraph_server.md). !!! tip "Python SDK reference" - + For detailed information about the Python SDK, see [Python SDK reference docs](../cloud/reference/sdk/python_sdk_ref.md). ## Installation -You can install the packages using the appropriate package manager for your language: +You can install the LangGraph SDK using the following command: -=== "Python" - ```bash - pip install langgraph-sdk - ``` - -=== "JS" - ```bash - yarn add @langchain/langgraph-sdk - ``` +```bash +pip install langgraph-sdk +``` ## Python sync vs. async @@ -39,6 +34,7 @@ The Python SDK provides both synchronous (`get_sync_client`) and asynchronous (` ``` === "Async" + ```python from langgraph_sdk import get_client @@ -46,9 +42,24 @@ The Python SDK provides both synchronous (`get_sync_client`) and asynchronous (` await client.assistants.search() ``` - ## Learn more - [Python SDK Reference](../cloud/reference/sdk/python_sdk_ref.md) - [LangGraph CLI API Reference](../cloud/reference/cli.md) -- [JS/TS SDK Reference](../cloud/reference/sdk/js_ts_sdk_ref.md) \ No newline at end of file + ::: + +:::js +LangGraph Platform provides a JS/TS SDK for interacting with [LangGraph Server](./langgraph_server.md). + +## Installation + +You can add the LangGraph SDK to your project using the following command: + +```bash +npm install @langchain/langgraph-sdk +``` + +## Learn more + +- [LangGraph CLI API Reference](../cloud/reference/cli.md) + ::: diff --git a/docs/docs/concepts/server-mcp.md b/docs/docs/concepts/server-mcp.md index b66eaada3..ce69dd348 100644 --- a/docs/docs/concepts/server-mcp.md +++ b/docs/docs/concepts/server-mcp.md @@ -8,7 +8,7 @@ hide: # MCP endpoint in LangGraph Server -The [Model Context Protocol (MCP)](./mcp.md) is an open protocol for describing tools and data sources in a model-agnostic format, enabling LLMs to discover and use them via a structured API. +The [Model Context Protocol (MCP)](./mcp.md) is an open protocol for describing tools and data sources in a model-agnostic format, enabling LLMs to discover and use them via a structured API. [LangGraph Server](./langgraph_server.md) implements MCP using the [Streamable HTTP transport](https://spec.modelcontextprotocol.io/specification/2025-03-26/basic/transports/#streamable-http). This allows LangGraph **agents** to be exposed as **MCP tools**, making them usable with any MCP-compliant client supporting Streamable HTTP. @@ -16,6 +16,7 @@ The MCP endpoint is available at `/mcp` on [LangGraph Server](./langgraph_server ## Requirements +:::python To use MCP, ensure you have the following dependencies installed: - `langgraph-api >= 0.2.3` @@ -27,107 +28,18 @@ Install them with: pip install "langgraph-api>=0.2.3" "langgraph-sdk>=0.1.61" ``` -## Usage overview +::: -To enable MCP: +:::js +To use MCP, ensure you have both the api and sdk packages installed. -- Upgrade to use langgraph-api>=0.2.3. If you are deploying LangGraph Platform, this will be done for you automatically if you create a new revision. -- MCP tools (agents) will be automatically exposed. -- Connect with any MCP-compliant client that supports Streamable HTTP. +```bash +npm install @langchain/langgraph-api @langchain/langgraph-sdk +``` +::: -### Client - -Use an MCP-compliant client to connect to the LangGraph server. The following examples show how to connect using different programming languages. - -=== "JavaScript/TypeScript" - - ```bash - npm install @modelcontextprotocol/sdk - ``` - - > **Note** - > Replace `serverUrl` with your LangGraph server URL and configure authentication headers as needed. - - ```js - import { Client } from "@modelcontextprotocol/sdk/client/index.js"; - import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; - - // Connects to the LangGraph MCP endpoint - async function connectClient(url) { - const baseUrl = new URL(url); - const client = new Client({ - name: 'streamable-http-client', - version: '1.0.0' - }); - - const transport = new StreamableHTTPClientTransport(baseUrl); - await client.connect(transport); - - console.log("Connected using Streamable HTTP transport"); - console.log(JSON.stringify(await client.listTools(), null, 2)); - return client; - } - - const serverUrl = "http://localhost:2024/mcp"; - - connectClient(serverUrl) - .then(() => { - console.log("Client connected successfully"); - }) - .catch(error => { - console.error("Failed to connect client:", error); - }); - ``` - -=== "Python" - - - Install the adapter with: - - ```bash - pip install langchain-mcp-adapters - ``` - - Here is an example of how to connect to a remote MCP endpoint and use an agent as a tool: - - ```python - # Create server parameters for stdio connection - from mcp import ClientSession - from mcp.client.streamable_http import streamablehttp_client - import asyncio - - from langchain_mcp_adapters.tools import load_mcp_tools - from langgraph.prebuilt import create_react_agent - - server_params = { - "url": "https://mcp-finance-agent.xxx.us.langgraph.app/mcp", - "headers": { - "X-Api-Key":"lsv2_pt_your_api_key" - } - } - - async def main(): - async with streamablehttp_client(**server_params) as (read, write, _): - async with ClientSession(read, write) as session: - # Initialize the connection - await session.initialize() - - # Load the remote graph as if it was a tool - tools = await load_mcp_tools(session) - - # Create and run a react agent with the tools - agent = create_react_agent("openai:gpt-4.1", tools) - - # Invoke the agent with a message - agent_response = await agent.ainvoke({"messages": "What can the finance agent do for me?"}) - print(agent_response) - - if __name__ == "__main__": - asyncio.run(main()) - ``` - -## Expose an agent as MCP tool +## Exposing an agent as MCP tool When deployed, your agent will appear as a tool in the MCP endpoint with this configuration: @@ -136,22 +48,41 @@ with this configuration: - **Tool description**: The agent's description. - **Tool input schema**: The agent's input schema. -### Setting name and description +### Setting name and description You can set the name and description of your agent in `langgraph.json`: +:::python + ```json { - "graphs": { - "my_agent": { - "path": "./my_agent/agent.py:graph", - "description": "A description of what the agent does" - } - }, - "env": ".env" + "graphs": { + "my_agent": { + "path": "./my_agent/agent.py:graph", + "description": "A description of what the agent does" + } + }, + "env": ".env" } ``` +::: +:::js + +```json +{ + "graphs": { + "my_agent": { + "path": "./my_agent/agent.ts:graph", + "description": "A description of what the agent does" + } + }, + "env": ".env" +} +``` + +::: + After deployment, you can update the name and description using the LangGraph SDK. ### Schema @@ -198,45 +129,116 @@ print(graph.invoke({"question": "hi"})) For more details, see the [low-level concepts guide](https://langchain-ai.github.io/langgraph/concepts/low_level/#state). -## Use user-scoped MCP tools in your deployment +## Usage overview -!!! tip "Prerequisites" +To enable MCP: - You have added your own [custom auth middleware](https://langchain-ai.github.io/langgraph/how-tos/auth/custom_auth/) that populates the `langgraph_auth_user` object, making it accessible through configurable context for every node in your graph. +- Upgrade to use langgraph-api>=0.2.3. If you are deploying LangGraph Platform, this will be done for you automatically if you create a new revision. +- MCP tools (agents) will be automatically exposed. +- Connect with any MCP-compliant client that supports Streamable HTTP. -To make user-scoped tools available to your LangGraph Platform deployment, start with implementing a snippet like the following: +### Client -```python -from langchain_mcp_adapters.client import MultiServerMCPClient +:::python +Use an MCP-compliant client to connect to the LangGraph server. The following example shows how to connect using [langchain-mcp-adapters](https://github.com/langchain-ai/langchain-mcp-adapters). -def mcp_tools_node(state, config): - user = config["configurable"].get("langgraph_auth_user") - # e.g., user["github_token"], user["email"], etc. - - client = MultiServerMCPClient({ - "github": { - "transport": "streamable_http", # (1) - "url": "https://my-github-mcp-server/mcp", # (2) - "headers": { - "Authorization": f"Bearer {user['github_token']}" - } - } - }) - tools = await client.get_tools() # (3) - - # Your tool-calling logic here - - tool_messages = ... - return {"messages": tool_messages} +Install the adapter with: + +```bash +pip install langchain-mcp-adapters ``` -1. MCP only supports adding headers to requests made to `streamable_http` and `sse` `transport` servers. -2. Your MCP server URL. -3. Get available tools from your MCP server. +Here is an example of how to connect to a remote MCP endpoint and use an agent as a tool: -_This can also be done by [rebuilding your graph at runtime](https://langchain-ai.github.io/langgraph/cloud/deployment/graph_rebuild/) to have a different configuration for a new run_ +```python +# Create server parameters for stdio connection +from mcp import ClientSession +from mcp.client.streamable_http import streamablehttp_client +import asyncio -## Session behavior +from langchain_mcp_adapters.tools import load_mcp_tools +from langgraph.prebuilt import create_react_agent + +server_params = { + "url": "https://mcp-finance-agent.xxx.us.langgraph.app/mcp", + "headers": { + "X-Api-Key":"lsv2_pt_your_api_key" + } +} + +async def main(): + async with streamablehttp_client(**server_params) as (read, write, _): + async with ClientSession(read, write) as session: + # Initialize the connection + await session.initialize() + + # Load the remote graph as if it was a tool + tools = await load_mcp_tools(session) + + # Create and run a react agent with the tools + agent = create_react_agent("openai:gpt-4.1", tools) + + # Invoke the agent with a message + agent_response = await agent.ainvoke({"messages": "What can the finance agent do for me?"}) + print(agent_response) + +if __name__ == "__main__": + asyncio.run(main()) +``` + +::: + +:::js +Use an MCP-compliant client to connect to the LangGraph server. The following example shows how to connect using [`@langchain/mcp-adapters`](https://npmjs.com/package/@langchain/mcp-adapters). + +```bash +npm install @langchain/mcp-adapters +``` + +Here is an example of how to connect to a remote MCP endpoint and use an agent as a tool: + +```typescript +import { MultiServerMCPClient } from "@langchain/mcp-adapters"; +import { createReactAgent } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; + +async function main() { + const client = new MultiServerMCPClient({ + mcpServers: { + "finance-agent": { + url: "https://mcp-finance-agent.xxx.us.langgraph.app/mcp", + headers: { + "X-Api-Key": "lsv2_pt_your_api_key", + }, + }, + }, + }); + + const tools = await client.getTools(); + + const model = new ChatOpenAI({ + model: "gpt-4o-mini", + temperature: 0, + }); + + const agent = createReactAgent({ + model, + tools, + }); + + const response = await agent.invoke({ + input: "What can the finance agent do for me?", + }); + + console.log(response); +} + +main(); +``` + +::: + +## Session behavior The current LangGraph MCP implementation does not support sessions. Each `/mcp` request is stateless and independent. diff --git a/docs/docs/concepts/subgraphs.md b/docs/docs/concepts/subgraphs.md index 218bf8cac..6c5503431 100644 --- a/docs/docs/concepts/subgraphs.md +++ b/docs/docs/concepts/subgraphs.md @@ -12,71 +12,152 @@ Some reasons for using subgraphs are: The main question when adding subgraphs is how the parent graph and subgraph communicate, i.e. how they pass the [state](./low_level.md#state) between each other during the graph execution. There are two scenarios: -* parent and subgraph have **shared state keys** in their state [schemas](./low_level.md#state). In this case, you can [include the subgraph as a node in the parent graph](../how-tos/subgraph.md#shared-state-schemas) +- parent and subgraph have **shared state keys** in their state [schemas](./low_level.md#state). In this case, you can [include the subgraph as a node in the parent graph](../how-tos/subgraph.ipynb#shared-state-schemas) - ```python - from langgraph.graph import StateGraph, MessagesState, START + :::python - # Subgraph + ```python + from langgraph.graph import StateGraph, MessagesState, START - def call_model(state: MessagesState): - response = model.invoke(state["messages"]) - return {"messages": response} + # Subgraph - subgraph_builder = StateGraph(State) - subgraph_builder.add_node(call_model) - ... - # highlight-next-line - subgraph = subgraph_builder.compile() + def call_model(state: MessagesState): + response = model.invoke(state["messages"]) + return {"messages": response} - # Parent graph + subgraph_builder = StateGraph(State) + subgraph_builder.add_node(call_model) + ... + # highlight-next-line + subgraph = subgraph_builder.compile() - builder = StateGraph(State) - # highlight-next-line - builder.add_node("subgraph_node", subgraph) - builder.add_edge(START, "subgraph_node") - graph = builder.compile() - ... - graph.invoke({"messages": [{"role": "user", "content": "hi!"}]}) - ``` + # Parent graph -* parent graph and subgraph have **different schemas** (no shared state keys in their state [schemas](./low_level.md#state)). In this case, you have to [call the subgraph from inside a node in the parent graph](../how-tos/subgraph.md#different-state-schemas): this is useful when the parent graph and the subgraph have different state schemas and you need to transform state before or after calling the subgraph + builder = StateGraph(State) + # highlight-next-line + builder.add_node("subgraph_node", subgraph) + builder.add_edge(START, "subgraph_node") + graph = builder.compile() + ... + graph.invoke({"messages": [{"role": "user", "content": "hi!"}]}) + ``` - ```python - from typing_extensions import TypedDict, Annotated - from langchain_core.messages import AnyMessage - from langgraph.graph import StateGraph, MessagesState, START - from langgraph.graph.message import add_messages + ::: - class SubgraphMessagesState(TypedDict): - # highlight-next-line - subgraph_messages: Annotated[list[AnyMessage], add_messages] + :::js - # Subgraph + ```typescript + import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; - # highlight-next-line - def call_model(state: SubgraphMessagesState): - response = model.invoke(state["subgraph_messages"]) - return {"subgraph_messages": response} + // Subgraph - subgraph_builder = StateGraph(SubgraphMessagesState) - subgraph_builder.add_node("call_model_from_subgraph", call_model) - subgraph_builder.add_edge(START, "call_model_from_subgraph") - ... - # highlight-next-line - subgraph = subgraph_builder.compile() + const subgraphBuilder = new StateGraph(MessagesZodState).addNode( + "callModel", + async (state) => { + const response = await model.invoke(state.messages); + return { messages: response }; + } + ); + // ... other nodes and edges + // highlight-next-line + const subgraph = subgraphBuilder.compile(); - # Parent graph + // Parent graph - def call_subgraph(state: MessagesState): - response = subgraph.invoke({"subgraph_messages": state["messages"]}) - return {"messages": response["subgraph_messages"]} + const builder = new StateGraph(MessagesZodState) + // highlight-next-line + .addNode("subgraphNode", subgraph) + .addEdge(START, "subgraphNode"); + const graph = builder.compile(); + // ... + await graph.invoke({ messages: [{ role: "user", content: "hi!" }] }); + ``` - builder = StateGraph(State) - # highlight-next-line - builder.add_node("subgraph_node", call_subgraph) - builder.add_edge(START, "subgraph_node") - graph = builder.compile() - ... - graph.invoke({"messages": [{"role": "user", "content": "hi!"}]}) - ``` + ::: + +- parent graph and subgraph have **different schemas** (no shared state keys in their state [schemas](./low_level.md#state)). In this case, you have to [call the subgraph from inside a node in the parent graph](../how-tos/subgraph.ipynb#different-state-schemas): this is useful when the parent graph and the subgraph have different state schemas and you need to transform state before or after calling the subgraph + + :::python + + ```python + from typing_extensions import TypedDict, Annotated + from langchain_core.messages import AnyMessage + from langgraph.graph import StateGraph, MessagesState, START + from langgraph.graph.message import add_messages + + class SubgraphMessagesState(TypedDict): + # highlight-next-line + subgraph_messages: Annotated[list[AnyMessage], add_messages] + + # Subgraph + + # highlight-next-line + def call_model(state: SubgraphMessagesState): + response = model.invoke(state["subgraph_messages"]) + return {"subgraph_messages": response} + + subgraph_builder = StateGraph(SubgraphMessagesState) + subgraph_builder.add_node("call_model_from_subgraph", call_model) + subgraph_builder.add_edge(START, "call_model_from_subgraph") + ... + # highlight-next-line + subgraph = subgraph_builder.compile() + + # Parent graph + + def call_subgraph(state: MessagesState): + response = subgraph.invoke({"subgraph_messages": state["messages"]}) + return {"messages": response["subgraph_messages"]} + + builder = StateGraph(State) + # highlight-next-line + builder.add_node("subgraph_node", call_subgraph) + builder.add_edge(START, "subgraph_node") + graph = builder.compile() + ... + graph.invoke({"messages": [{"role": "user", "content": "hi!"}]}) + ``` + + ::: + + :::js + + ```typescript + import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; + import { z } from "zod"; + + const SubgraphState = z.object({ + // highlight-next-line + subgraphMessages: MessagesZodState.shape.messages, + }); + + // Subgraph + + const subgraphBuilder = new StateGraph(SubgraphState) + // highlight-next-line + .addNode("callModelFromSubgraph", async (state) => { + const response = await model.invoke(state.subgraphMessages); + return { subgraphMessages: response }; + }) + .addEdge(START, "callModelFromSubgraph"); + // ... + // highlight-next-line + const subgraph = subgraphBuilder.compile(); + + // Parent graph + + const builder = new StateGraph(MessagesZodState) + // highlight-next-line + .addNode("subgraphNode", async (state) => { + const response = await subgraph.invoke({ + subgraphMessages: state.messages, + }); + return { messages: response.subgraphMessages }; + }) + .addEdge(START, "subgraphNode"); + const graph = builder.compile(); + // ... + await graph.invoke({ messages: [{ role: "user", content: "hi!" }] }); + ``` + + ::: diff --git a/docs/docs/concepts/template_applications.md b/docs/docs/concepts/template_applications.md index 8da3229b9..9f0198b0a 100644 --- a/docs/docs/concepts/template_applications.md +++ b/docs/docs/concepts/template_applications.md @@ -9,6 +9,7 @@ Templates are open source reference applications designed to help you get starte You can create an application from a template using the LangGraph CLI. +:::python !!! info "Requirements" - Python >= 3.11 @@ -16,56 +17,74 @@ You can create an application from a template using the LangGraph CLI. ## Install the LangGraph CLI -=== "Python" +```bash +pip install "langgraph-cli[inmem]" --upgrade +``` - ```bash - pip install "langgraph-cli[inmem]" --upgrade - ``` +Or via [`uv`](https://docs.astral.sh/uv/getting-started/installation/) (recommended): - Or via [`uv`](https://docs.astral.sh/uv/getting-started/installation/) (recommended): +```bash +uvx --from "langgraph-cli[inmem]" langgraph dev --help +``` - ```bash - uvx --from "langgraph-cli[inmem]" langgraph dev --help - ``` +::: -=== "JS" +:::js - ```bash - npx @langchain/langgraph-cli --help - ``` +```bash +npx @langchain/langgraph-cli --help +``` + +::: ## Available Templates -| Template | Description | Python | JS/TS | -|---------------------------|------------------------------------------------------------------------------------------|------------------------------------------------------------------|---------------------------------------------------------------------| -| **New LangGraph Project** | A simple, minimal chatbot with memory. | [Repo](https://github.com/langchain-ai/new-langgraph-project) | [Repo](https://github.com/langchain-ai/new-langgraphjs-project) | -| **ReAct Agent** | A simple agent that can be flexibly extended to many tools. | [Repo](https://github.com/langchain-ai/react-agent) | [Repo](https://github.com/langchain-ai/react-agent-js) | -| **Memory Agent** | A ReAct-style agent with an additional tool to store memories for use across threads. | [Repo](https://github.com/langchain-ai/memory-agent) | [Repo](https://github.com/langchain-ai/memory-agent-js) | -| **Retrieval Agent** | An agent that includes a retrieval-based question-answering system. | [Repo](https://github.com/langchain-ai/retrieval-agent-template) | [Repo](https://github.com/langchain-ai/retrieval-agent-template-js) | -| **Data-Enrichment Agent** | An agent that performs web searches and organizes its findings into a structured format. | [Repo](https://github.com/langchain-ai/data-enrichment) | [Repo](https://github.com/langchain-ai/data-enrichment-js) | +:::python +| Template | Description | Link | +| -------- | ----------- | ------ | +| **New LangGraph Project** | A simple, minimal chatbot with memory. | [Repo](https://github.com/langchain-ai/new-langgraph-project) | +| **ReAct Agent** | A simple agent that can be flexibly extended to many tools. | [Repo](https://github.com/langchain-ai/react-agent) | +| **Memory Agent** | A ReAct-style agent with an additional tool to store memories for use across threads. | [Repo](https://github.com/langchain-ai/memory-agent) | +| **Retrieval Agent** | An agent that includes a retrieval-based question-answering system. | [Repo](https://github.com/langchain-ai/retrieval-agent-template) | +| **Data-Enrichment Agent** | An agent that performs web searches and organizes its findings into a structured format. | [Repo](https://github.com/langchain-ai/data-enrichment) | +::: + +:::js +| Template | Description | Link | +| -------- | ----------- | ------ | +| **New LangGraph Project** | A simple, minimal chatbot with memory. | [Repo](https://github.com/langchain-ai/new-langgraphjs-project) | +| **ReAct Agent** | A simple agent that can be flexibly extended to many tools. | [Repo](https://github.com/langchain-ai/react-agent-js) | +| **Memory Agent** | A ReAct-style agent with an additional tool to store memories for use across threads. | [Repo](https://github.com/langchain-ai/memory-agent-js) | +| **Retrieval Agent** | An agent that includes a retrieval-based question-answering system. | [Repo](https://github.com/langchain-ai/retrieval-agent-template-js) | +| **Data-Enrichment Agent** | An agent that performs web searches and organizes its findings into a structured format. | [Repo](https://github.com/langchain-ai/data-enrichment-js) | +::: ## 🌱 Create a LangGraph App To create a new app from a template, use the `langgraph new` command. -=== "Python" +:::python - ```bash - langgraph new - ``` +```bash +langgraph new +``` - Or via [`uv`](https://docs.astral.sh/uv/getting-started/installation/) (recommended): +Or via [`uv`](https://docs.astral.sh/uv/getting-started/installation/) (recommended): - ```bash - uvx --from "langgraph-cli[inmem]" langgraph new - ``` +```bash +uvx --from "langgraph-cli[inmem]" langgraph new +``` -=== "JS" +::: - ```bash - npm create langgraph@latest - ``` +:::js + +```bash +npm create langgraph +``` + +::: ## Next Steps @@ -73,26 +92,31 @@ Review the `README.md` file in the root of your new LangGraph app for more infor After configuring the app properly and adding your API keys, you can start the app using the LangGraph CLI: -=== "Python" +:::python - ```bash - langgraph dev - ``` +```bash +langgraph dev +``` - Or via [`uv`](https://docs.astral.sh/uv/getting-started/installation/) (recommended): +Or via [`uv`](https://docs.astral.sh/uv/getting-started/installation/) (recommended): - ```bash - uvx --from "langgraph-cli[inmem]" --with-editable . langgraph dev - ``` +```bash +uvx --from "langgraph-cli[inmem]" --with-editable . langgraph dev +``` - ??? info "Missing Local Package?" - If you are not using `uv` and run into a "`ModuleNotFoundError`" or "`ImportError`", even after installing the local package (`pip install -e .`), it is likely the case that you need to install the CLI into your local virtual environment to make the CLI "aware" of the local package. You can do this by running `python -m pip install "langgraph-cli[inmem]"` and re-activating your virtual environment before running `langgraph dev`. +!!! info "Missing Local Package?" -=== "JS" + If you are not using `uv` and run into a "`ModuleNotFoundError`" or "`ImportError`", even after installing the local package (`pip install -e .`), it is likely the case that you need to install the CLI into your local virtual environment to make the CLI "aware" of the local package. You can do this by running `python -m pip install "langgraph-cli[inmem]"` and re-activating your virtual environment before running `langgraph dev`. - ```bash - npx @langchain/langgraph-cli dev - ``` +::: + +:::js + +```bash +npx @langchain/langgraph-cli dev +``` + +::: See the following guides for more information on how to deploy your app: diff --git a/docs/docs/concepts/tools.md b/docs/docs/concepts/tools.md index 8a2e693af..d43cb8a9c 100644 --- a/docs/docs/concepts/tools.md +++ b/docs/docs/concepts/tools.md @@ -2,7 +2,13 @@ Many AI applications interact with users via natural language. However, some use cases require models to interface directly with external systems—such as APIs, databases, or file systems—using structured input. In these scenarios, [tool calling](../how-tos/tool-calling.md) enables models to generate requests that conform to a specified input schema. +:::python **Tools** encapsulate a callable function and its input schema. These can be passed to compatible [chat models](https://python.langchain.com/docs/concepts/chat_models), allowing the model to decide whether to invoke a tool and with what arguments. +::: + +:::js +**Tools** encapsulate a callable function and its input schema. These can be passed to compatible [chat models](https://js.langchain.com/docs/concepts/chat_models), allowing the model to decide whether to invoke a tool and with what arguments. +::: ## Tool calling @@ -10,17 +16,63 @@ Many AI applications interact with users via natural language. However, some use Tool calling is typically **conditional**. Based on the user input and available tools, the model may choose to issue a tool call request. This request is returned in an `AIMessage` object, which includes a `tool_calls` field that specifies the tool name and input arguments: +:::python + ```python llm_with_tools.invoke("What is 2 multiplied by 3?") # -> AIMessage(tool_calls=[{'name': 'multiply', 'args': {'a': 2, 'b': 3}, ...}]) ``` +``` +AIMessage( + tool_calls=[ + ToolCall(name="multiply", args={"a": 2, "b": 3}), + ... + ] +) +``` + +::: + +:::js + +```typescript +await llmWithTools.invoke("What is 2 multiplied by 3?"); +``` + +``` +AIMessage { + tool_calls: [ + ToolCall { + name: "multiply", + args: { a: 2, b: 3 }, + ... + }, + ... + ] +} +``` + +::: + If the input is unrelated to any tool, the model returns only a natural language message: +:::python + ```python llm_with_tools.invoke("Hello world!") # -> AIMessage(content="Hello!") ``` +::: + +:::js + +```typescript +await llmWithTools.invoke("Hello world!"); // { content: "Hello!" } +``` + +::: + Importantly, the model does not execute the tool—it only generates a request. A separate executor (such as a runtime or agent) is responsible for handling the tool call and returning the result. See the [tool calling guide](../how-tos/tool-calling.md) for more details. @@ -29,18 +81,25 @@ See the [tool calling guide](../how-tos/tool-calling.md) for more details. LangChain provides prebuilt tool integrations for common external systems including APIs, databases, file systems, and web data. +:::python Browse the [integrations directory](https://python.langchain.com/docs/integrations/tools/) for available tools. +::: + +:::js +Browse the [integrations directory](https://js.langchain.com/docs/integrations/tools/) for available tools. +::: Common categories: -* **Search**: Bing, SerpAPI, Tavily -* **Code execution**: Python REPL, Node.js REPL -* **Databases**: SQL, MongoDB, Redis -* **Web data**: Scraping and browsing -* **APIs**: OpenWeatherMap, NewsAPI, etc. +- **Search**: Bing, SerpAPI, Tavily +- **Code execution**: Python REPL, Node.js REPL +- **Databases**: SQL, MongoDB, Redis +- **Web data**: Scraping and browsing +- **APIs**: OpenWeatherMap, NewsAPI, etc. ## Custom tools +:::python You can define custom tools using the `@tool` decorator or plain Python functions. For example: ```python @@ -52,6 +111,32 @@ def multiply(a: int, b: int) -> int: return a * b ``` +::: + +:::js +You can define custom tools using the `tool` function. For example: + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const multiply = tool( + (input) => { + return input.a * input.b; + }, + { + name: "multiply", + description: "Multiply two numbers.", + schema: z.object({ + a: z.number(), + b: z.number(), + }), + } +); +``` + +::: + See the [tool calling guide](../how-tos/tool-calling.md) for more details. ## Tool execution @@ -60,5 +145,14 @@ While the model determines when to call a tool, execution of the tool call must LangGraph provides prebuilt components for this: -* [`ToolNode`][langgraph.prebuilt.tool_node.ToolNode]: A prebuilt node that executes tools. -* [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent]: Constructs a full agent that manages tool calling automatically. +:::python + +- @[`ToolNode`][ToolNode]: A prebuilt node that executes tools. +- @[`create_react_agent`][create_react_agent]: Constructs a full agent that manages tool calling automatically. +::: + +:::js + +- @[ToolNode]: A prebuilt node that executes tools. +- @[`createReactAgent`][create_react_agent]: Constructs a full agent that manages tool calling automatically. +::: diff --git a/docs/docs/how-tos/auth/custom_auth.md b/docs/docs/how-tos/auth/custom_auth.md index 2b1dd035c..1aa732a62 100644 --- a/docs/docs/how-tos/auth/custom_auth.md +++ b/docs/docs/how-tos/auth/custom_auth.md @@ -1,5 +1,18 @@ # Add custom authentication +!!! tip "Prerequisites" + + This guide assumes familiarity with the following concepts: + + * [**Authentication & Access Control**](../../concepts/auth.md) + * [**LangGraph Platform**](../../concepts/langgraph_platform.md) + + For a more guided walkthrough, see [**setting up custom authentication**](../../tutorials/auth/getting_started.md) tutorial. + +???+ note "Support by deployment type" + + Custom auth is supported for all deployments in the **managed LangGraph Platform**, as well as **Enterprise** self-hosted plans. It is not supported for **Lite** self-hosted plans. + This guide shows how to add custom authentication to your LangGraph Platform application. This guide applies to both LangGraph Platform and self-hosted deployments. It does not apply to isolated usage of the LangGraph open source library in your own custom server. !!! note @@ -10,7 +23,9 @@ This guide shows how to add custom authentication to your LangGraph Platform app To leverage custom authentication and access user-level metadata in your deployments, set up custom authentication to automatically populate the `config["configurable"]["langgraph_auth_user"]` object through a custom authentication handler. You can then access this object in your graph with the `langgraph_auth_user` key to [allow an agent to perform authenticated actions on behalf of the user](#enable-agent-authentication). -1. Implement authentication: +:::python + +1. Implement authentication: !!! note @@ -46,7 +61,7 @@ To leverage custom authentication and access user-level metadata in your deploym 1. This handler receives the request (headers, etc.), validates the user, and returns a dictionary with at least an identity field. 2. You can add any custom fields you want (e.g., OAuth tokens, roles, org IDs, etc.). -2. In your `langgraph.json`, add the path to your auth file: +2. In your `langgraph.json`, add the path to your auth file: ```json hl_lines="7-9" { @@ -61,7 +76,7 @@ To leverage custom authentication and access user-level metadata in your deploym } ``` -3. Once you've set up authentication in your server, requests must include the required authorization information based on your chosen scheme. Assuming you are using JWT token authentication, you could access your deployments using any of the following methods: +3. Once you've set up authentication in your server, requests must include the required authorization information based on your chosen scheme. Assuming you are using JWT token authentication, you could access your deployments using any of the following methods: === "Python Client" @@ -89,32 +104,16 @@ To leverage custom authentication and access user-level metadata in your deploym ) threads = await remote_graph.ainvoke(...) ``` + ```python + from langgraph.pregel.remote import RemoteGraph - === "JavaScript Client" - - ```javascript - import { Client } from "@langchain/langgraph-sdk"; - - const my_token = "your-token"; // In practice, you would generate a signed token with your auth provider - const client = new Client({ - apiUrl: "http://localhost:2024", - defaultHeaders: { Authorization: `Bearer ${my_token}` }, - }); - const threads = await client.threads.search(); - ``` - - === "JavaScript RemoteGraph" - - ```javascript - import { RemoteGraph } from "@langchain/langgraph/remote"; - - const my_token = "your-token"; // In practice, you would generate a signed token with your auth provider - const remoteGraph = new RemoteGraph({ - graphId: "agent", - url: "http://localhost:2024", - headers: { Authorization: `Bearer ${my_token}` }, - }); - const threads = await remoteGraph.invoke(...); + my_token = "your-token" # In practice, you would generate a signed token with your auth provider + remote_graph = RemoteGraph( + "agent", + url="http://localhost:2024", + headers={"Authorization": f"Bearer {my_token}"} + ) + threads = await remote_graph.ainvoke(...) ``` === "CURL" @@ -138,14 +137,14 @@ def my_node(state, config): ``` !!! note - Fetch user credentials from a secure secret store. Storing secrets in graph state is not recommended. +Fetch user credentials from a secure secret store. Storing secrets in graph state is not recommended. ### Authorizing a Studio user By default, if you add custom authorization on your resources, this will also apply to interactions made from the Studio. If you want, you can handle logged-in Studio users differently by checking [is_studio_user()](../../reference/functions/sdk_auth.isStudioUser.html). !!! note - `is_studio_user` was added in version 0.1.73 of the langgraph-sdk. If you're on an older version, you can still check whether `isinstance(ctx.user, StudioUser)`. +`is_studio_user` was added in version 0.1.73 of the langgraph-sdk. If you're on an older version, you can still check whether `isinstance(ctx.user, StudioUser)`. ```python from langgraph_sdk.auth import is_studio_user, Auth @@ -169,6 +168,104 @@ async def add_owner( Only use this if you want to permit developer access to a graph deployed on the managed LangGraph Platform SaaS. +::: + +:::js + +1. Implement authentication: + + !!! note + + Without a custom `authenticate` handler, LangGraph sees only the API-key owner (usually the developer), so requests aren’t scoped to individual end-users. To propagate custom tokens, you must implement your own handler. + + ```typescript + import { Auth, HTTPException } from "@langchain/langgraph-sdk/auth"; + + const auth = new Auth() + .authenticate(async (request) => { + const authorization = request.headers.get("Authorization"); + const token = authorization?.split(" ")[1]; // "Bearer <token>" + if (!token) { + throw new HTTPException(401, "No token provided"); + } + try { + const user = await verifyToken(token); + return user; + } catch (error) { + throw new HTTPException(401, "Invalid token"); + } + }) + // Add authorization rules to actually control access to resources + .on("*", async ({ user, value }) => { + const filters = { owner: user.identity }; + const metadata = value.metadata ?? {}; + metadata.update(filters); + return filters; + }) + // Assumes you organize information in store like (user_id, resource_type, resource_id) + .on("store", async ({ user, value }) => { + const namespace = value.namespace; + if (namespace[0] !== user.identity) { + throw new HTTPException(403, "Not authorized"); + } + }); + ``` + + 1. This handler receives the request (headers, etc.), validates the user, and returns an object with at least an identity field. + 2. You can add any custom fields you want (e.g., OAuth tokens, roles, org IDs, etc.). + +2. In your `langgraph.json`, add the path to your auth file: + + ```json hl_lines="7-9" + { + "dependencies": ["."], + "graphs": { + "agent": "./agent.ts:graph" + }, + "env": ".env", + "auth": { + "path": "./auth.ts:my_auth" + } + } + ``` + +3. Once you've set up authentication in your server, requests must include the required authorization information based on your chosen scheme. Assuming you are using JWT token authentication, you could access your deployments using any of the following methods: + + === "SDK Client" + + ```javascript + import { Client } from "@langchain/langgraph-sdk"; + + const my_token = "your-token"; // In practice, you would generate a signed token with your auth provider + const client = new Client({ + apiUrl: "http://localhost:2024", + defaultHeaders: { Authorization: `Bearer ${my_token}` }, + }); + const threads = await client.threads.search(); + ``` + + === "RemoteGraph" + + ```javascript + import { RemoteGraph } from "@langchain/langgraph/remote"; + + const my_token = "your-token"; // In practice, you would generate a signed token with your auth provider + const remoteGraph = new RemoteGraph({ + graphId: "agent", + url: "http://localhost:2024", + headers: { Authorization: `Bearer ${my_token}` }, + }); + const threads = await remoteGraph.invoke(...); + ``` + + === "CURL" + + ```bash + curl -H "Authorization: Bearer ${your-token}" http://localhost:2024/threads + ``` + +::: + ## Learn more - [Authentication & Access Control](../../concepts/auth.md) diff --git a/docs/docs/how-tos/auth/openapi_security.md b/docs/docs/how-tos/auth/openapi_security.md index 05215b497..70bdb395d 100644 --- a/docs/docs/how-tos/auth/openapi_security.md +++ b/docs/docs/how-tos/auth/openapi_security.md @@ -3,6 +3,7 @@ This guide shows how to customize the OpenAPI security schema for your LangGraph Platform API documentation. A well-documented security schema helps API consumers understand how to authenticate with your API and even enables automatic client generation. See the [Authentication & Access Control conceptual guide](../../concepts/auth.md) for more details about LangGraph's authentication system. !!! note "Implementation vs Documentation" + This guide only covers how to document your security requirements in OpenAPI. To implement the actual authentication logic, see [How to add custom authentication](./custom_auth.md). This guide applies to all LangGraph Platform deployments (Cloud and self-hosted). It does not apply to usage of the LangGraph open source library if you are not using LangGraph Platform. @@ -38,6 +39,7 @@ To customize the security schema in your OpenAPI documentation, add an `openapi` Note that LangGraph Platform does not provide authentication endpoints - you'll need to handle user authentication in your client application and pass the resulting credentials to the LangGraph API. +:::python === "OAuth2 with Bearer Token" ```json @@ -89,6 +91,62 @@ Note that LangGraph Platform does not provide authentication endpoints - you'll } ``` +::: + +:::js +=== "OAuth2 with Bearer Token" + + ```json + { + "auth": { + "path": "./auth.ts:my_auth", // Implement auth logic here + "openapi": { + "securitySchemes": { + "OAuth2": { + "type": "oauth2", + "flows": { + "implicit": { + "authorizationUrl": "https://your-auth-server.com/oauth/authorize", + "scopes": { + "me": "Read information about the current user", + "threads": "Access to create and manage threads" + } + } + } + } + }, + "security": [ + {"OAuth2": ["me", "threads"]} + ] + } + } + } + ``` + +=== "API Key" + + ```json + { + "auth": { + "path": "./auth.ts:my_auth", // Implement auth logic here + "openapi": { + "securitySchemes": { + "apiKeyAuth": { + "type": "apiKey", + "in": "header", + "name": "X-API-Key" + } + }, + "security": [ + {"apiKeyAuth": []} + ] + } + } + } + ``` + +::: + ## Testing After updating your configuration: diff --git a/docs/docs/how-tos/http/custom_middleware.md b/docs/docs/how-tos/http/custom_middleware.md index a351b345d..f81d6802e 100644 --- a/docs/docs/how-tos/http/custom_middleware.md +++ b/docs/docs/how-tos/http/custom_middleware.md @@ -12,7 +12,7 @@ Below is an example using FastAPI. ## Create app -Starting from an **existing** LangGraph Platform application, add the following middleware code to your `webapp.py` file. If you are starting from scratch, you can create a new app from a template using the CLI. +Starting from an **existing** LangGraph Platform application, add the following middleware code to your webapp file. If you are starting from scratch, you can create a new app from a template using the CLI. ```bash langgraph new --template=new-langgraph-project-python my_new_project @@ -72,4 +72,4 @@ You can deploy this app as-is to LangGraph Platform or to your self-hosted platf ## Next steps -Now that you've added custom middleware to your deployment, you can use similar techniques to add [custom routes](./custom_routes.md) or define [custom lifespan events](./custom_lifespan.md) to further customize your server's behavior. \ No newline at end of file +Now that you've added custom middleware to your deployment, you can use similar techniques to add [custom routes](./custom_routes.md) or define [custom lifespan events](./custom_lifespan.md) to further customize your server's behavior. diff --git a/docs/docs/how-tos/http/custom_routes.md b/docs/docs/how-tos/http/custom_routes.md index b385a8aa0..76f715abc 100644 --- a/docs/docs/how-tos/http/custom_routes.md +++ b/docs/docs/how-tos/http/custom_routes.md @@ -10,7 +10,7 @@ Below is an example using FastAPI. ## Create app -Starting from an **existing** LangGraph Platform application, add the following custom route code to your `webapp.py` file. If you are starting from scratch, you can create a new app from a template using the CLI. +Starting from an **existing** LangGraph Platform application, add the following custom route code to your webapp file. If you are starting from scratch, you can create a new app from a template using the CLI. ```bash langgraph new --template=new-langgraph-project-python my_new_project @@ -60,7 +60,6 @@ langgraph dev --no-browser If you navigate to `localhost:2024/hello` in your browser (`2024` is the default development port), you should see the `/hello` endpoint returning `{"Hello": "World"}`. - !!! note "Shadowing default endpoints" The routes you create in the app are given priority over the system defaults, meaning you can shadow and redefine the behavior of any default endpoint. @@ -71,4 +70,4 @@ You can deploy this app as-is to LangGraph Platform or to your self-hosted platf ## Next steps -Now that you've added a custom route to your deployment, you can use this same technique to further customize how your server behaves, such as defining custom [custom middleware](./custom_middleware.md) and [custom lifespan events](./custom_lifespan.md). +Now that you've added a custom route to your deployment, you can use this same technique to further customize how your server behaves, such as defining custom [custom middleware](./custom_middleware.md) and [custom lifespan events](./custom_lifespan.md). diff --git a/docs/docs/how-tos/human_in_the_loop/add-human-in-the-loop.md b/docs/docs/how-tos/human_in_the_loop/add-human-in-the-loop.md index 3445f710b..6e7330f2f 100644 --- a/docs/docs/how-tos/human_in_the_loop/add-human-in-the-loop.md +++ b/docs/docs/how-tos/human_in_the_loop/add-human-in-the-loop.md @@ -19,18 +19,27 @@ To review, edit, and approve tool calls in an agent or workflow, use interrupts ## Pause using `interrupt` -[Dynamic interrupts](../../concepts/human_in_the_loop.md#key-capabilities) (also known as dynamic breakpoints) are triggered based on the current state of the graph. You can set dynamic interrupts by calling [`interrupt` function][langgraph.types.interrupt] in the appropriate place. The graph will pause, which allows for human intervention, and then resumes the graph with their input. It's useful for tasks like approvals, edits, or gathering additional context. +:::python +[Dynamic interrupts](../../concepts/human_in_the_loop.md#key-capabilities) (also known as dynamic breakpoints) are triggered based on the current state of the graph. You can set dynamic interrupts by calling @[`interrupt` function][interrupt] in the appropriate place. The graph will pause, which allows for human intervention, and then resumes the graph with their input. It's useful for tasks like approvals, edits, or gathering additional context. !!! note As of v1.0, `interrupt` is the recommended way to pause a graph. `NodeInterrupt` is deprecated and will be removed in v2.0. +::: + +:::js +[Dynamic interrupts](../../concepts/human_in_the_loop.md#key-capabilities) (also known as dynamic breakpoints) are triggered based on the current state of the graph. You can set dynamic interrupts by calling @[`interrupt` function][interrupt] in the appropriate place. The graph will pause, which allows for human intervention, and then resumes the graph with their input. It's useful for tasks like approvals, edits, or gathering additional context. +::: + To use `interrupt` in your graph, you need to: 1. [**Specify a checkpointer**](../../concepts/persistence.md#checkpoints) to save the graph state after each step. 2. **Call `interrupt()`** in the appropriate place. See the [Common Patterns](#common-patterns) section for examples. 3. **Run the graph** with a [**thread ID**](../../concepts/persistence.md#threads) until the `interrupt` is hit. -4. **Resume execution** using `invoke`/`ainvoke`/`stream`/`astream` (see [**The `Command` primitive**](#resume-using-the-command-primitive)). +4. **Resume execution** using `invoke`/`stream` (see [**The `Command` primitive**](#resume-using-the-command-primitive)). + +:::python ```python # highlight-next-line @@ -54,7 +63,13 @@ graph = graph_builder.compile(checkpointer=checkpointer) # (4)! config = {"configurable": {"thread_id": "some_id"}} result = graph.invoke({"some_text": "original text"}, config=config) # (5)! print(result['__interrupt__']) # (6)! -# > [Interrupt(value={'text_to_revise': 'original text'}, id='a0d9dd40440ac7be2720dc5c20858627')] +# > [ +# > Interrupt( +# > value={'text_to_revise': 'original text'}, +# > resumable=True, +# > ns=['human_node:6ce9e64f-edef-fe5d-f7dc-511fa9526960'] +# > ) +# > ] # highlight-next-line print(graph.invoke(Command(resume="Edited text"), config=config)) # (7)! @@ -68,9 +83,60 @@ print(graph.invoke(Command(resume="Edited text"), config=config)) # (7)! 5. The graph is invoked with some initial state. 6. When the graph hits the interrupt, it returns an `Interrupt` object with the payload and metadata. 7. The graph is resumed with a `Command(resume=...)`, injecting the human's input and continuing execution. + ::: + +:::js + +```typescript +// highlight-next-line +import { interrupt, Command } from "@langchain/langgraph"; + +const graph = graphBuilder + .addNode("humanNode", (state) => { + // highlight-next-line + const value = interrupt( + // (1)! + { + textToRevise: state.someText, // (2)! + } + ); + return { + someText: value, // (3)! + }; + }) + .addEdge(START, "humanNode") + .compile({ checkpointer }); // (4)! + +// Run the graph until the interrupt is hit. +const config = { configurable: { thread_id: "some_id" } }; +const result = await graph.invoke({ someText: "original text" }, config); // (5)! +console.log(result.__interrupt__); // (6)! +// > [ +// > { +// > value: { textToRevise: 'original text' }, +// > resumable: true, +// > ns: ['humanNode:6ce9e64f-edef-fe5d-f7dc-511fa9526960'], +// > when: 'during' +// > } +// > ] + +// highlight-next-line +console.log(await graph.invoke(new Command({ resume: "Edited text" }), config)); // (7)! +// > { someText: 'Edited text' } +``` + +1. `interrupt(...)` pauses execution at `humanNode`, surfacing the given payload to a human. +2. Any JSON serializable value can be passed to the `interrupt` function. Here, an object containing the text to revise. +3. Once resumed, the return value of `interrupt(...)` is the human-provided input, which is used to update the state. +4. A checkpointer is required to persist graph state. In production, this should be durable (e.g., backed by a database). +5. The graph is invoked with some initial state. +6. When the graph hits the interrupt, it returns an object with `__interrupt__` containing the payload and metadata. +7. The graph is resumed with a `Command({ resume: ... })`, injecting the human's input and continuing execution. + ::: ??? example "Extended example: using `interrupt`" + :::python ```python from typing import TypedDict import uuid @@ -109,6 +175,14 @@ print(graph.invoke(Command(resume="Edited text"), config=config)) # (7)! # Run the graph until the interrupt is hit. result = graph.invoke({"some_text": "original text"}, config=config) # (5)! + print(result['__interrupt__']) # (6)! + # > [ + # > Interrupt( + # > value={'text_to_revise': 'original text'}, + # > resumable=True, + # > ns=['human_node:6ce9e64f-edef-fe5d-f7dc-511fa9526960'] + # > ) + # > ] print(result["__interrupt__"]) # (6)! # > [Interrupt(value={'text_to_revise': 'original text'}, id='6d7c4048049254c83195429a3659661d')] @@ -124,28 +198,119 @@ print(graph.invoke(Command(resume="Edited text"), config=config)) # (7)! 5. The graph is invoked with some initial state. 6. When the graph hits the interrupt, it returns an `Interrupt` object with the payload and metadata. 7. The graph is resumed with a `Command(resume=...)`, injecting the human's input and continuing execution. + ::: + :::js + ```typescript + import { z } from "zod"; + import { v4 as uuidv4 } from "uuid"; + import { MemorySaver, StateGraph, START, interrupt, Command } from "@langchain/langgraph"; + + const StateAnnotation = z.object({ + someText: z.string(), + }); + + // Build the graph + const graphBuilder = new StateGraph(StateAnnotation) + .addNode("humanNode", (state) => { + // highlight-next-line + const value = interrupt( // (1)! + { + textToRevise: state.someText // (2)! + } + ); + return { + someText: value // (3)! + }; + }) + .addEdge(START, "humanNode"); + + const checkpointer = new MemorySaver(); // (4)! + + const graph = graphBuilder.compile({ checkpointer }); + + // Pass a thread ID to the graph to run it. + const config = { configurable: { thread_id: uuidv4() } }; + + // Run the graph until the interrupt is hit. + const result = await graph.invoke({ someText: "original text" }, config); // (5)! + + console.log(result.__interrupt__); // (6)! + // > [ + // > { + // > value: { textToRevise: 'original text' }, + // > resumable: true, + // > ns: ['humanNode:6ce9e64f-edef-fe5d-f7dc-511fa9526960'], + // > when: 'during' + // > } + // > ] + + // highlight-next-line + console.log(await graph.invoke(new Command({ resume: "Edited text" }), config)); // (7)! + // > { someText: 'Edited text' } + ``` + + 1. `interrupt(...)` pauses execution at `humanNode`, surfacing the given payload to a human. + 2. Any JSON serializable value can be passed to the `interrupt` function. Here, an object containing the text to revise. + 3. Once resumed, the return value of `interrupt(...)` is the human-provided input, which is used to update the state. + 4. A checkpointer is required to persist graph state. In production, this should be durable (e.g., backed by a database). + 5. The graph is invoked with some initial state. + 6. When the graph hits the interrupt, it returns an object with `__interrupt__` containing the payload and metadata. + 7. The graph is resumed with a `Command({ resume: ... })`, injecting the human's input and continuing execution. + ::: !!! tip "New in 0.4.0" - `__interrupt__` is a special key that will be returned when running the graph if the graph is interrupted. Support for `__interrupt__` in `invoke` and `ainvoke` has been added in version 0.4.0. If you're on an older version, you will only see `__interrupt__` in the result if you use `stream` or `astream`. You can also use `graph.get_state(thread_id)` to get the interrupt value(s). + :::python + `__interrupt__` is a special key that will be returned when running the graph if the graph is interrupted. Support for `__interrupt__` in `invoke` and `ainvoke` has been added in version 0.4.0. If you're on an older version, you will only see `__interrupt__` in the result if you use `stream` or `astream`. You can also use `graph.get_state(thread_id)` to get the interrupt value(s). + ::: + + :::js + `__interrupt__` is a special key that will be returned when running the graph if the graph is interrupted. Support for `__interrupt__` in `invoke` has been added in version 0.4.0. If you're on an older version, you will only see `__interrupt__` in the result if you use `stream`. You can also use `graph.getState(config)` to get the interrupt value(s). + ::: !!! warning - Interrupts resemble Python's input() function in terms of developer experience, but they do not automatically resume execution from the interruption point. Instead, they rerun the entire node where the interrupt was used. For this reason, interrupts are typically best placed at the start of a node or in a dedicated node. + :::python + Interrupts resemble Python's input() function in terms of developer experience, but they do not automatically resume execution from the interruption point. Instead, they rerun the entire node where the interrupt was used. For this reason, interrupts are typically best placed at the start of a node or in a dedicated node. + ::: + + :::js + Interrupts are both powerful and ergonomic, but it's important to note that they do not automatically resume execution from the interrupt point. Instead, they rerun the entire where the interrupt was used. For this reason, interrupts are typically best placed at the state of a node or in a dedicated node. + ::: ## Resume using the `Command` primitive +:::python +!!! warning + + Resuming from an `interrupt` is different from Python's `input()` function, where execution resumes from the exact point where the `input()` function was called. + +::: + When the `interrupt` function is used within a graph, execution pauses at that point and awaits user input. -To resume execution, use the [`Command`][langgraph.types.Command] primitive, which can be supplied via the `invoke`, `ainvoke`, `stream`, or `astream` methods. The graph resumes execution from the beginning of the node where `interrupt(...)` was initially called. This time, the `interrupt` function will return the value provided in `Command(resume=value)` rather than pausing again. All code from the beginning of the node to the `interrupt` will be re-executed. +:::python +To resume execution, use the @[`Command`][Command] primitive, which can be supplied via the `invoke` or `stream` methods. The graph resumes execution from the beginning of the node where `interrupt(...)` was initially called. This time, the `interrupt` function will return the value provided in `Command(resume=value)` rather than pausing again. All code from the beginning of the node to the `interrupt` will be re-executed. ```python # Resume graph execution by providing the user's input. graph.invoke(Command(resume={"age": "25"}), thread_config) ``` -## Resuming Multiple interrupts +::: + +:::js +To resume execution, use the @[`Command`][Command] primitive, which can be supplied via the `invoke` or `stream` methods. The graph resumes execution from the beginning of the node where `interrupt(...)` was initially called. This time, the `interrupt` function will return the value provided in `Command(resume=value)` rather than pausing again. All code from the beginning of the node to the `interrupt` will be re-executed. + +```typescript +// Resume graph execution by providing the user's input. +await graph.invoke(new Command({ resume: { age: "25" } }), threadConfig); +``` + +::: + +### Resume multiple interrupts with one invocation When nodes with interrupt conditions are run in parallel, it's possible to have multiple interrupts in the task queue. For example, the following graph has two nodes run in parallel that require human input: @@ -154,9 +319,9 @@ For example, the following graph has two nodes run in parallel that require huma ![image](../assets/human_in_loop_parallel.png){: style="max-height:400px"} </figure> +:::python Once your graph has been interrupted and is stalled, you can resume all the interrupts at once with `Command.resume`, passing a dictionary mapping of interrupt ids to resume values. - ```python from typing import TypedDict import uuid @@ -201,13 +366,31 @@ result = graph.invoke( # Resume with mapping of interrupt IDs to values resume_map = { - i.id: f"edited text for {i.value['text_to_revise']}" - for i in result["__interrupt__"] + i.interrupt_id: f"human input for prompt {i.value}" + for i in parent.get_state(thread_config).interrupts } print(graph.invoke(Command(resume=resume_map), config=config)) # > {'text_1': 'edited text for original text 1', 'text_2': 'edited text for original text 2'} ``` +::: + +:::js + +```typescript +const state = await parentGraph.getState(threadConfig); +const resumeMap = Object.fromEntries( + state.interrupts.map((i) => [ + i.interruptId, + `human input for prompt ${i.value}`, + ]) +); + +await parentGraph.invoke(new Command({ resume: resumeMap }), threadConfig); +``` + +::: + ## Common patterns Below we show different design patterns that can be implemented using `interrupt` and `Command`. @@ -221,6 +404,8 @@ Below we show different design patterns that can be implemented using `interrupt Pause the graph before a critical step, such as an API call, to review and approve the action. If the action is rejected, you can prevent the graph from executing the step, and potentially take an alternative action. +:::python + ```python from typing import Literal from langgraph.types import interrupt, Command @@ -251,8 +436,42 @@ thread_config = {"configurable": {"thread_id": "some_id"}} graph.invoke(Command(resume=True), config=thread_config) ``` +::: + +:::js + +```typescript +import { interrupt, Command } from "@langchain/langgraph"; + +// Add the node to the graph in an appropriate location +// and connect it to the relevant nodes. +graphBuilder.addNode("humanApproval", (state) => { + const isApproved = interrupt({ + question: "Is this correct?", + // Surface the output that should be + // reviewed and approved by the human. + llmOutput: state.llmOutput, + }); + + if (isApproved) { + return new Command({ goto: "someNode" }); + } else { + return new Command({ goto: "anotherNode" }); + } +}); +const graph = graphBuilder.compile({ checkpointer }); + +// After running the graph and hitting the interrupt, the graph will pause. +// Resume it with either an approval or rejection. +const threadConfig = { configurable: { thread_id: "some_id" } }; +await graph.invoke(new Command({ resume: true }), threadConfig); +``` + +::: + ??? example "Extended example: approve or reject with interrupt" + :::python ```python from typing import Literal, TypedDict import uuid @@ -320,6 +539,102 @@ graph.invoke(Command(resume=True), config=thread_config) final_result = graph.invoke(Command(resume="approve"), config=config) print(final_result) ``` + ::: + + :::js + ```typescript + import { z } from "zod"; + import { v4 as uuidv4 } from "uuid"; + import { + StateGraph, + START, + END, + interrupt, + Command, + MemorySaver + } from "@langchain/langgraph"; + + // Define the shared graph state + const StateAnnotation = z.object({ + llmOutput: z.string(), + decision: z.string(), + }); + + // Simulate an LLM output node + function generateLlmOutput(state: z.infer<typeof StateAnnotation>) { + return { llmOutput: "This is the generated output." }; + } + + // Human approval node + function humanApproval(state: z.infer<typeof StateAnnotation>): Command { + const decision = interrupt({ + question: "Do you approve the following output?", + llmOutput: state.llmOutput + }); + + if (decision === "approve") { + return new Command({ + goto: "approvedPath", + update: { decision: "approved" } + }); + } else { + return new Command({ + goto: "rejectedPath", + update: { decision: "rejected" } + }); + } + } + + // Next steps after approval + function approvedNode(state: z.infer<typeof StateAnnotation>) { + console.log("✅ Approved path taken."); + return state; + } + + // Alternative path after rejection + function rejectedNode(state: z.infer<typeof StateAnnotation>) { + console.log("❌ Rejected path taken."); + return state; + } + + // Build the graph + const builder = new StateGraph(StateAnnotation) + .addNode("generateLlmOutput", generateLlmOutput) + .addNode("humanApproval", humanApproval, { + ends: ["approvedPath", "rejectedPath"] + }) + .addNode("approvedPath", approvedNode) + .addNode("rejectedPath", rejectedNode) + .addEdge(START, "generateLlmOutput") + .addEdge("generateLlmOutput", "humanApproval") + .addEdge("approvedPath", END) + .addEdge("rejectedPath", END); + + const checkpointer = new MemorySaver(); + const graph = builder.compile({ checkpointer }); + + // Run until interrupt + const config = { configurable: { thread_id: uuidv4() } }; + const result = await graph.invoke({}, config); + console.log(result.__interrupt__); + // Output: + // [{ + // value: { + // question: 'Do you approve the following output?', + // llmOutput: 'This is the generated output.' + // }, + // ... + // }] + + // Simulate resuming with human input + // To test rejection, replace resume: "approve" with resume: "reject" + const finalResult = await graph.invoke( + new Command({ resume: "approve" }), + config + ); + console.log(finalResult); + ``` + ::: ### Review and edit state @@ -329,6 +644,8 @@ graph.invoke(Command(resume=True), config=thread_config) </figcaption> </figure> +:::python + ```python from langgraph.types import interrupt @@ -345,7 +662,7 @@ def human_editing(state: State): # Update the state with the edited text return { - "llm_generated_summary": result["edited_text"] + "llm_generated_summary": result["edited_text"] } # Add the node to the graph in an appropriate location @@ -359,13 +676,51 @@ graph = graph_builder.compile(checkpointer=checkpointer) # Resume it with the edited text. thread_config = {"configurable": {"thread_id": "some_id"}} graph.invoke( - Command(resume={"edited_text": "The edited text"}), + Command(resume={"edited_text": "The edited text"}), config=thread_config ) ``` +::: + +:::js + +```typescript +import { interrupt } from "@langchain/langgraph"; + +function humanEditing(state: z.infer<typeof StateAnnotation>) { + const result = interrupt({ + // Interrupt information to surface to the client. + // Can be any JSON serializable value. + task: "Review the output from the LLM and make any necessary edits.", + llmGeneratedSummary: state.llmGeneratedSummary, + }); + + // Update the state with the edited text + return { + llmGeneratedSummary: result.editedText, + }; +} + +// Add the node to the graph in an appropriate location +// and connect it to the relevant nodes. +graphBuilder.addNode("humanEditing", humanEditing); +const graph = graphBuilder.compile({ checkpointer }); + +// After running the graph and hitting the interrupt, the graph will pause. +// Resume it with the edited text. +const threadConfig = { configurable: { thread_id: "some_id" } }; +await graph.invoke( + new Command({ resume: { editedText: "The edited text" } }), + threadConfig +); +``` + +::: + ??? example "Extended example: edit state with interrupt" + :::python ```python from typing import TypedDict import uuid @@ -427,7 +782,7 @@ graph.invoke( # > value={ # > 'task': 'Please review and edit the generated summary if necessary.', # > 'generated_summary': 'The cat sat on the mat and looked at the stars.' - # > }, + # > }, # > id='...' # > ) # > ] @@ -440,6 +795,89 @@ graph.invoke( ) print(resumed_result) ``` + ::: + + :::js + ```typescript + import { z } from "zod"; + import { v4 as uuidv4 } from "uuid"; + import { + StateGraph, + START, + END, + interrupt, + Command, + MemorySaver + } from "@langchain/langgraph"; + + // Define the graph state + const StateAnnotation = z.object({ + summary: z.string(), + }); + + // Simulate an LLM summary generation + function generateSummary(state: z.infer<typeof StateAnnotation>) { + return { + summary: "The cat sat on the mat and looked at the stars." + }; + } + + // Human editing node + function humanReviewEdit(state: z.infer<typeof StateAnnotation>) { + const result = interrupt({ + task: "Please review and edit the generated summary if necessary.", + generatedSummary: state.summary + }); + return { + summary: result.editedSummary + }; + } + + // Simulate downstream use of the edited summary + function downstreamUse(state: z.infer<typeof StateAnnotation>) { + console.log(`✅ Using edited summary: ${state.summary}`); + return state; + } + + // Build the graph + const builder = new StateGraph(StateAnnotation) + .addNode("generateSummary", generateSummary) + .addNode("humanReviewEdit", humanReviewEdit) + .addNode("downstreamUse", downstreamUse) + .addEdge(START, "generateSummary") + .addEdge("generateSummary", "humanReviewEdit") + .addEdge("humanReviewEdit", "downstreamUse") + .addEdge("downstreamUse", END); + + // Set up in-memory checkpointing for interrupt support + const checkpointer = new MemorySaver(); + const graph = builder.compile({ checkpointer }); + + // Invoke the graph until it hits the interrupt + const config = { configurable: { thread_id: uuidv4() } }; + const result = await graph.invoke({}, config); + + // Output interrupt payload + console.log(result.__interrupt__); + // Example output: + // [{ + // value: { + // task: 'Please review and edit the generated summary if necessary.', + // generatedSummary: 'The cat sat on the mat and looked at the stars.' + // }, + // resumable: true, + // ... + // }] + + // Resume the graph with human-edited input + const editedSummary = "The cat lay on the rug, gazing peacefully at the night sky."; + const resumedResult = await graph.invoke( + new Command({ resume: { editedSummary } }), + config + ); + console.log(resumedResult); + ``` + ::: ### Review tool calls @@ -453,7 +891,9 @@ critical in applications where the tool calls requested by the LLM may be sensit To add a human approval step to a tool: 1. Use `interrupt()` in the tool to pause execution. -2. Resume with a `Command(resume=...)` to continue based on human input. +2. Resume with a `Command` to continue based on human input. + +:::python ```python from langgraph.checkpoint.memory import InMemorySaver @@ -487,12 +927,67 @@ agent = create_react_agent( ) ``` -1. The [`interrupt` function][langgraph.types.interrupt] pauses the agent graph at a specific node. In this case, we call `interrupt()` at the beginning of the tool function, which pauses the graph at the node that executes the tool. The information inside `interrupt()` (e.g., tool calls) can be presented to a human, and the graph can be resumed with the user input (tool call approval, edit or feedback). +1. The @[`interrupt` function][interrupt] pauses the agent graph at a specific node. In this case, we call `interrupt()` at the beginning of the tool function, which pauses the graph at the node that executes the tool. The information inside `interrupt()` (e.g., tool calls) can be presented to a human, and the graph can be resumed with the user input (tool call approval, edit or feedback). 2. The `InMemorySaver` is used to store the agent state at every step in the tool calling loop. This enables [short-term memory](../memory/add-memory.md#add-short-term-memory) and [human-in-the-loop](../../concepts/human_in_the_loop.md) capabilities. In this example, we use `InMemorySaver` to store the agent state in memory. In a production application, the agent state will be stored in a database. 3. Initialize the agent with the `checkpointer`. + ::: + +:::js + +```typescript +import { MemorySaver } from "@langchain/langgraph"; +import { interrupt } from "@langchain/langgraph"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +// An example of a sensitive tool that requires human review / approval +const bookHotel = tool( + async ({ hotelName }) => { + // highlight-next-line + const response = interrupt( + // (1)! + `Trying to call \`bookHotel\` with args {"hotelName": "${hotelName}"}. ` + + "Please approve or suggest edits." + ); + if (response.type === "accept") { + // Continue with original args + } else if (response.type === "edit") { + hotelName = response.args.hotelName; + } else { + throw new Error(`Unknown response type: ${response.type}`); + } + return `Successfully booked a stay at ${hotelName}.`; + }, + { + name: "bookHotel", + description: "Book a hotel", + schema: z.object({ + hotelName: z.string(), + }), + } +); + +// highlight-next-line +const checkpointer = new MemorySaver(); // (2)! + +const agent = createReactAgent({ + llm: model, + tools: [bookHotel], + // highlight-next-line + checkpointSaver: checkpointer, // (3)! +}); +``` + +1. The @[`interrupt` function][interrupt] pauses the agent graph at a specific node. In this case, we call `interrupt()` at the beginning of the tool function, which pauses the graph at the node that executes the tool. The information inside `interrupt()` (e.g., tool calls) can be presented to a human, and the graph can be resumed with the user input (tool call approval, edit or feedback). +2. The `MemorySaver` is used to store the agent state at every step in the tool calling loop. This enables [short-term memory](../memory/add-memory.md#add-short-term-memory) and [human-in-the-loop](../../concepts/human_in_the_loop.md) capabilities. In this example, we use `MemorySaver` to store the agent state in memory. In a production application, the agent state will be stored in a database. +3. Initialize the agent with the `checkpointSaver`. + ::: Run the agent with the `stream()` method, passing the `config` object to specify the thread ID. This allows the agent to resume the same conversation on future invocations. +:::python + ```python config = { "configurable": { @@ -510,9 +1005,37 @@ for chunk in agent.stream( print("\n") ``` +::: + +:::js + +```typescript +const config = { + configurable: { + // highlight-next-line + thread_id: "1", + }, +}; + +const stream = await agent.stream( + { messages: [{ role: "user", content: "book a stay at McKittrick hotel" }] }, + // highlight-next-line + config +); + +for await (const chunk of stream) { + console.log(chunk); + console.log("\n"); +} +``` + +::: + > You should see that the agent runs until it reaches the `interrupt()` call, at which point it pauses and waits for human input. -Resume the agent with a `Command(resume=...)` to continue based on human input. +Resume the agent with a `Command` to continue based on human input. + +:::python ```python from langgraph.types import Command @@ -527,17 +1050,41 @@ for chunk in agent.stream( print("\n") ``` -1. The [`interrupt` function][langgraph.types.interrupt] is used in conjunction with the [`Command`][langgraph.types.Command] object to resume the graph with a value provided by the human. +1. The @[`interrupt` function][interrupt] is used in conjunction with the @[`Command`][Command] object to resume the graph with a value provided by the human. + ::: + +:::js + +```typescript +import { Command } from "@langchain/langgraph"; + +const resumeStream = await agent.stream( + // highlight-next-line + new Command({ resume: { type: "accept" } }), // (1)! + // new Command({ resume: { type: "edit", args: { hotelName: "McKittrick Hotel" } } }), + config +); + +for await (const chunk of resumeStream) { + console.log(chunk); + console.log("\n"); +} +``` + +1. The @[`interrupt` function][interrupt] is used in conjunction with the @[`Command`][Command] object to resume the graph with a value provided by the human. + ::: ### Add interrupts to any tool -You can create a wrapper to add interrupts to *any* tool. The example below provides a reference implementation compatible with [Agent Inbox UI](https://github.com/langchain-ai/agent-inbox) and [Agent Chat UI](https://github.com/langchain-ai/agent-chat-ui). +You can create a wrapper to add interrupts to _any_ tool. The example below provides a reference implementation compatible with [Agent Inbox UI](https://github.com/langchain-ai/agent-inbox) and [Agent Chat UI](https://github.com/langchain-ai/agent-chat-ui). + +:::python ```python title="Wrapper that adds human-in-the-loop to any tool" from typing import Callable from langchain_core.tools import BaseTool, tool as create_tool from langchain_core.runnables import RunnableConfig -from langgraph.types import interrupt +from langgraph.types import interrupt from langgraph.prebuilt.interrupt import HumanInterruptConfig, HumanInterrupt def add_human_in_the_loop( @@ -545,7 +1092,7 @@ def add_human_in_the_loop( *, interrupt_config: HumanInterruptConfig = None, ) -> BaseTool: - """Wrap a tool to support human-in-the-loop review.""" + """Wrap a tool to support human-in-the-loop review.""" if not isinstance(tool, BaseTool): tool = create_tool(tool) @@ -592,11 +1139,89 @@ def add_human_in_the_loop( ``` 1. This wrapper creates a new tool that calls `interrupt()` **before** executing the wrapped tool. -2. `interrupt()` is using special input and output format that's expected by [Agent Inbox UI](https://github.com/langchain-ai/agent-inbox): - - a list of [`HumanInterrupt`][langgraph.prebuilt.interrupt.HumanInterrupt] objects is sent to `AgentInbox` render interrupt information to the end user - - resume value is provided by `AgentInbox` as a list (i.e., `Command(resume=[...])`) +2. `interrupt()` is using special input and output format that's expected by [Agent Inbox UI](https://github.com/langchain-ai/agent-inbox): - a list of @[`HumanInterrupt`][HumanInterrupt] objects is sent to `AgentInbox` render interrupt information to the end user - resume value is provided by `AgentInbox` as a list (i.e., `Command(resume=[...])`) + ::: -You can use the `add_human_in_the_loop` wrapper to add `interrupt()` to any tool without having to add it *inside* the tool: +:::js + +```typescript title="Wrapper that adds human-in-the-loop to any tool" +import { StructuredTool, tool } from "@langchain/core/tools"; +import { RunnableConfig } from "@langchain/core/runnables"; +import { interrupt } from "@langchain/langgraph"; + +interface HumanInterruptConfig { + allowAccept?: boolean; + allowEdit?: boolean; + allowRespond?: boolean; +} + +interface HumanInterrupt { + actionRequest: { + action: string; + args: Record<string, any>; + }; + config: HumanInterruptConfig; + description: string; +} + +function addHumanInTheLoop( + originalTool: StructuredTool, + interruptConfig: HumanInterruptConfig = { + allowAccept: true, + allowEdit: true, + allowRespond: true, + } +): StructuredTool { + // Wrap the original tool to support human-in-the-loop review + return tool( + // (1)! + async (toolInput: Record<string, any>, config?: RunnableConfig) => { + const request: HumanInterrupt = { + actionRequest: { + action: originalTool.name, + args: toolInput, + }, + config: interruptConfig, + description: "Please review the tool call", + }; + + // highlight-next-line + const response = interrupt([request])[0]; // (2)! + + // approve the tool call + if (response.type === "accept") { + return await originalTool.invoke(toolInput, config); + } + // update tool call args + else if (response.type === "edit") { + const updatedArgs = response.args.args; + return await originalTool.invoke(updatedArgs, config); + } + // respond to the LLM with user feedback + else if (response.type === "response") { + return response.args; + } else { + throw new Error( + `Unsupported interrupt response type: ${response.type}` + ); + } + }, + { + name: originalTool.name, + description: originalTool.description, + schema: originalTool.schema, + } + ); +} +``` + +1. This wrapper creates a new tool that calls `interrupt()` **before** executing the wrapped tool. +2. `interrupt()` is using special input and output format that's expected by [Agent Inbox UI](https://github.com/langchain-ai/agent-inbox): - a list of [`HumanInterrupt`] objects is sent to `AgentInbox` render interrupt information to the end user - resume value is provided by `AgentInbox` as a list (i.e., `Command({ resume: [...] })`) + ::: + +You can use the wrapper to add `interrupt()` to any tool without having to add it _inside_ the tool: + +:::python ```python from langgraph.checkpoint.memory import InMemorySaver @@ -633,14 +1258,69 @@ for chunk in agent.stream( ``` 1. The `add_human_in_the_loop` wrapper is used to add `interrupt()` to the tool. This allows the agent to pause execution and wait for human input before proceeding with the tool call. + ::: -> You should see that the agent runs until it reaches the `interrupt()` call, -> at which point it pauses and waits for human input. +:::js -Resume the agent with a `Command(resume=...)` to continue based on human input. +```typescript +import { MemorySaver } from "@langchain/langgraph"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +// highlight-next-line +const checkpointer = new MemorySaver(); + +const bookHotel = tool( + async ({ hotelName }) => { + return `Successfully booked a stay at ${hotelName}.`; + }, + { + name: "bookHotel", + description: "Book a hotel", + schema: z.object({ + hotelName: z.string(), + }), + } +); + +const agent = createReactAgent({ + llm: model, + tools: [ + // highlight-next-line + addHumanInTheLoop(bookHotel), // (1)! + ], + // highlight-next-line + checkpointSaver: checkpointer, +}); + +const config = { configurable: { thread_id: "1" } }; + +// Run the agent +const stream = await agent.stream( + { messages: [{ role: "user", content: "book a stay at McKittrick hotel" }] }, + // highlight-next-line + config +); + +for await (const chunk of stream) { + console.log(chunk); + console.log("\n"); +} +``` + +1. The `addHumanInTheLoop` wrapper is used to add `interrupt()` to the tool. This allows the agent to pause execution and wait for human input before proceeding with the tool call. + ::: + +> You should see that the agent runs until it reaches the `interrupt()` call, +> at which point it pauses and waits for human input. + +Resume the agent with a `Command` to continue based on human input. + +:::python ```python -from langgraph.types import Command +from langgraph.types import Command for chunk in agent.stream( # highlight-next-line @@ -652,10 +1332,34 @@ for chunk in agent.stream( print("\n") ``` +::: + +:::js + +```typescript +import { Command } from "@langchain/langgraph"; + +const resumeStream = await agent.stream( + // highlight-next-line + new Command({ resume: [{ type: "accept" }] }), + // new Command({ resume: [{ type: "edit", args: { args: { hotelName: "McKittrick Hotel" } } }] }), + config +); + +for await (const chunk of resumeStream) { + console.log(chunk); + console.log("\n"); +} +``` + +::: + ### Validate human input If you need to validate the input provided by the human within the graph itself (rather than on the client side), you can achieve this by using multiple interrupt calls within a single node. +:::python + ```python from langgraph.types import interrupt @@ -674,15 +1378,49 @@ def human_node(state: State): else: # If the answer is valid, we can proceed. break - + print(f"The human in the loop is {answer} years old.") return { "age": answer } ``` +::: + +:::js + +```typescript +import { interrupt } from "@langchain/langgraph"; + +graphBuilder.addNode("humanNode", (state) => { + // Human node with validation. + let question = "What is your age?"; + + while (true) { + const answer = interrupt(question); + + // Validate answer, if the answer isn't valid ask for input again. + if (typeof answer !== "number" || answer < 0) { + question = `'${answer}' is not a valid age. What is your age?`; + continue; + } else { + // If the answer is valid, we can proceed. + break; + } + } + + console.log(`The human in the loop is ${answer} years old.`); + return { + age: answer, + }; +}); +``` + +::: + ??? example "Extended example: validating user input" + :::python ```python from typing import TypedDict import uuid @@ -749,6 +1487,84 @@ def human_node(state: State): final_result = graph.invoke(Command(resume="25"), config=config) print(final_result) # Should include the valid age ``` + ::: + + :::js + ```typescript + import { z } from "zod"; + import { v4 as uuidv4 } from "uuid"; + import { + StateGraph, + START, + END, + interrupt, + Command, + MemorySaver + } from "@langchain/langgraph"; + + // Define graph state + const StateAnnotation = z.object({ + age: z.number(), + }); + + // Node that asks for human input and validates it + function getValidAge(state: z.infer<typeof StateAnnotation>) { + let prompt = "Please enter your age (must be a non-negative integer)."; + + while (true) { + const userInput = interrupt(prompt); + + // Validate the input + try { + const age = parseInt(userInput as string); + if (isNaN(age) || age < 0) { + throw new Error("Age must be non-negative."); + } + return { age }; + } catch (error) { + prompt = `'${userInput}' is not valid. Please enter a non-negative integer for age.`; + } + } + } + + // Node that uses the valid input + function reportAge(state: z.infer<typeof StateAnnotation>) { + console.log(`✅ Human is ${state.age} years old.`); + return state; + } + + // Build the graph + const builder = new StateGraph(StateAnnotation) + .addNode("getValidAge", getValidAge) + .addNode("reportAge", reportAge) + .addEdge(START, "getValidAge") + .addEdge("getValidAge", "reportAge") + .addEdge("reportAge", END); + + // Create the graph with a memory checkpointer + const checkpointer = new MemorySaver(); + const graph = builder.compile({ checkpointer }); + + // Run the graph until the first interrupt + const config = { configurable: { thread_id: uuidv4() } }; + let result = await graph.invoke({}, config); + console.log(result.__interrupt__); // First prompt: "Please enter your age..." + + // Simulate an invalid input (e.g., string instead of integer) + result = await graph.invoke(new Command({ resume: "not a number" }), config); + console.log(result.__interrupt__); // Follow-up prompt with validation message + + // Simulate a second invalid input (e.g., negative number) + result = await graph.invoke(new Command({ resume: "-10" }), config); + console.log(result.__interrupt__); // Another retry + + // Provide valid input + const finalResult = await graph.invoke(new Command({ resume: "25" }), config); + console.log(finalResult); // Should include the valid age + ``` + ::: + +:::python ## Debug with interrupts @@ -795,14 +1611,14 @@ To debug and test a graph, use [static interrupts](../../concepts/human_in_the_l ```python # highlight-next-line graph.invoke( # (1)! - inputs, + inputs, # highlight-next-line interrupt_before=["node_a"], # (2)! # highlight-next-line interrupt_after=["node_b", "node_c"] # (3)! config={ "configurable": {"thread_id": "some_thread"} - }, + }, ) config = { @@ -834,30 +1650,30 @@ To debug and test a graph, use [static interrupts](../../concepts/human_in_the_l ```python from IPython.display import Image, display from typing_extensions import TypedDict - - from langgraph.checkpoint.memory import InMemorySaver + + from langgraph.checkpoint.memory import InMemorySaver from langgraph.graph import StateGraph, START, END - - + + class State(TypedDict): input: str - - + + def step_1(state): print("---Step 1---") pass - - + + def step_2(state): print("---Step 2---") pass - - + + def step_3(state): print("---Step 3---") pass - - + + builder = StateGraph(State) builder.add_node("step_1", step_1) builder.add_node("step_2", step_2) @@ -866,33 +1682,33 @@ To debug and test a graph, use [static interrupts](../../concepts/human_in_the_l builder.add_edge("step_1", "step_2") builder.add_edge("step_2", "step_3") builder.add_edge("step_3", END) - - # Set up a checkpointer + + # Set up a checkpointer checkpointer = InMemorySaver() # (1)! - + graph = builder.compile( checkpointer=checkpointer, # (2)! interrupt_before=["step_3"] # (3)! ) - + # View display(Image(graph.get_graph().draw_mermaid_png())) - - + + # Input initial_input = {"input": "hello world"} - + # Thread thread = {"configurable": {"thread_id": "1"}} - + # Run the graph until the first interruption for event in graph.stream(initial_input, thread, stream_mode="values"): print(event) - + # This will run until the breakpoint # You can get the state of the graph at this point print(graph.get_state(config)) - + # You can continue the graph execution by passing in `None` for the input for event in graph.stream(None, thread, stream_mode="values"): print(event) @@ -904,7 +1720,165 @@ You can use [LangGraph Studio](../../concepts/langgraph_studio.md) to debug your ![image](../../concepts/img/human_in_the_loop/static-interrupt.png){: style="max-height:400px"} -LangGraph Studio is free with [locally deployed applications](../../tutorials/langgraph-platform/local-server.md) using `langgraph dev`. +LangGraph Studio is free with [locally deployed applications](../../tutorials/langgraph-platform/local-server.md) using `langgraph dev`. + +::: + +## Debug with interrupts + +To debug and test a graph, use [static interrupts](../../concepts/human_in_the_loop.md#key-capabilities) (also known as static breakpoints) to step through the graph execution one node at a time or to pause the graph execution at specific nodes. Static interrupts are triggered at defined points either before or after a node executes. You can set static interrupts by specifying `interrupt_before` and `interrupt_after` at compile time or run time. + +!!! warning + + Static interrupts are **not** recommended for human-in-the-loop workflows. Use [dynamic interrupts](#pause-using-interrupt) instead. + +=== "Compile time" + + ```python + # highlight-next-line + graph = graph_builder.compile( # (1)! + # highlight-next-line + interrupt_before=["node_a"], # (2)! + # highlight-next-line + interrupt_after=["node_b", "node_c"], # (3)! + checkpointer=checkpointer, # (4)! + ) + + config = { + "configurable": { + "thread_id": "some_thread" + } + } + + # Run the graph until the breakpoint + graph.invoke(inputs, config=thread_config) # (5)! + + # Resume the graph + graph.invoke(None, config=thread_config) # (6)! + ``` + + 1. The breakpoints are set during `compile` time. + 2. `interrupt_before` specifies the nodes where execution should pause before the node is executed. + 3. `interrupt_after` specifies the nodes where execution should pause after the node is executed. + 4. A checkpointer is required to enable breakpoints. + 5. The graph is run until the first breakpoint is hit. + 6. The graph is resumed by passing in `None` for the input. This will run the graph until the next breakpoint is hit. + +=== "Run time" + + ```python + # highlight-next-line + graph.invoke( # (1)! + inputs, + # highlight-next-line + interrupt_before=["node_a"], # (2)! + # highlight-next-line + interrupt_after=["node_b", "node_c"] # (3)! + config={ + "configurable": {"thread_id": "some_thread"} + }, + ) + + config = { + "configurable": { + "thread_id": "some_thread" + } + } + + # Run the graph until the breakpoint + graph.invoke(inputs, config=config) # (4)! + + # Resume the graph + graph.invoke(None, config=config) # (5)! + ``` + + 1. `graph.invoke` is called with the `interrupt_before` and `interrupt_after` parameters. This is a run-time configuration and can be changed for every invocation. + 2. `interrupt_before` specifies the nodes where execution should pause before the node is executed. + 3. `interrupt_after` specifies the nodes where execution should pause after the node is executed. + 4. The graph is run until the first breakpoint is hit. + 5. The graph is resumed by passing in `None` for the input. This will run the graph until the next breakpoint is hit. + + !!! note + + You cannot set static breakpoints at runtime for **sub-graphs**. + If you have a sub-graph, you must set the breakpoints at compilation time. + +??? example "Setting static breakpoints" + + ```python + from IPython.display import Image, display + from typing_extensions import TypedDict + + from langgraph.checkpoint.memory import InMemorySaver + from langgraph.graph import StateGraph, START, END + + + class State(TypedDict): + input: str + + + def step_1(state): + print("---Step 1---") + pass + + + def step_2(state): + print("---Step 2---") + pass + + + def step_3(state): + print("---Step 3---") + pass + + + builder = StateGraph(State) + builder.add_node("step_1", step_1) + builder.add_node("step_2", step_2) + builder.add_node("step_3", step_3) + builder.add_edge(START, "step_1") + builder.add_edge("step_1", "step_2") + builder.add_edge("step_2", "step_3") + builder.add_edge("step_3", END) + + # Set up a checkpointer + checkpointer = InMemorySaver() # (1)! + + graph = builder.compile( + checkpointer=checkpointer, # (2)! + interrupt_before=["step_3"] # (3)! + ) + + # View + display(Image(graph.get_graph().draw_mermaid_png())) + + + # Input + initial_input = {"input": "hello world"} + + # Thread + thread = {"configurable": {"thread_id": "1"}} + + # Run the graph until the first interruption + for event in graph.stream(initial_input, thread, stream_mode="values"): + print(event) + + # This will run until the breakpoint + # You can get the state of the graph at this point + print(graph.get_state(config)) + + # You can continue the graph execution by passing in `None` for the input + for event in graph.stream(None, thread, stream_mode="values"): + print(event) + ``` + +### Use static interrupts in LangGraph Studio + +You can use [LangGraph Studio](../../concepts/langgraph_studio.md) to debug your graph. You can set static breakpoints in the UI and then run the graph. You can also use the UI to inspect the graph state at any point in the execution. + +![image](../../concepts/img/human_in_the_loop/static-interrupt.png){: style="max-height:400px"} + +LangGraph Studio is free with [locally deployed applications](../../tutorials/langgraph-platform/local-server.md) using `langgraph dev`. ## Considerations @@ -916,27 +1890,44 @@ Place code with side effects, such as API calls, after the `interrupt` or in a s === "Side effects after interrupt" + :::python ```python from langgraph.types import interrupt def human_node(state: State): """Human node with validation.""" - + answer = interrupt(question) - + api_call(answer) # OK as it's after the interrupt ``` + ::: + + :::js + ```typescript + import { interrupt } from "@langchain/langgraph"; + + function humanNode(state: z.infer<typeof StateAnnotation>) { + // Human node with validation. + + const answer = interrupt(question); + + apiCall(answer); // OK as it's after the interrupt + } + ``` + ::: === "Side effects in a separate node" + :::python ```python from langgraph.types import interrupt def human_node(state: State): """Human node with validation.""" - + answer = interrupt(question) - + return { "answer": answer } @@ -944,11 +1935,34 @@ Place code with side effects, such as API calls, after the `interrupt` or in a s def api_call_node(state: State): api_call(...) # OK as it's in a separate node ``` + ::: + + :::js + ```typescript + import { interrupt } from "@langchain/langgraph"; + + function humanNode(state: z.infer<typeof StateAnnotation>) { + // Human node with validation. + + const answer = interrupt(question); + + return { + answer + }; + } + + function apiCallNode(state: z.infer<typeof StateAnnotation>) { + apiCall(state.answer); // OK as it's in a separate node + } + ``` + ::: ### Using with subgraphs called as functions When invoking a subgraph as a function, the parent graph will resume execution from the **beginning of the node** where the subgraph was invoked where the `interrupt` was triggered. Similarly, the **subgraph** will resume from the **beginning of the node** where the `interrupt()` function was called. +:::python + ```python def node_in_parent_graph(state: State): some_code() # <-- This will re-execute when the subgraph is resumed. @@ -958,6 +1972,22 @@ def node_in_parent_graph(state: State): ... ``` +::: + +:::js + +```typescript +async function nodeInParentGraph(state: z.infer<typeof StateAnnotation>) { + someCode(); // <-- This will re-execute when the subgraph is resumed. + // Invoke a subgraph as a function. + // The subgraph contains an `interrupt` call. + const subgraphResult = await subgraph.invoke(someInput); + // ... +} +``` + +::: + ??? example "Extended example: parent and subgraph execution flow" Say we have a parent graph with 3 nodes: @@ -979,6 +2009,7 @@ def node_in_parent_graph(state: State): Here is abbreviated example code that you can use to understand how subgraphs work with interrupts. It counts the number of times each node is entered and prints the count. + :::python ```python import uuid from typing import TypedDict @@ -1074,6 +2105,107 @@ def node_in_parent_graph(state: State): Got an answer of 35 {'parent_node': {'state_counter': 1}} ``` + ::: + + :::js + ```typescript + import { v4 as uuidv4 } from "uuid"; + import { + StateGraph, + START, + interrupt, + Command, + MemorySaver + } from "@langchain/langgraph"; + import { z } from "zod"; + + const StateAnnotation = z.object({ + stateCounter: z.number(), + }); + + // Global variable to track the number of attempts + let counterNodeInSubgraph = 0; + + function nodeInSubgraph(state: z.infer<typeof StateAnnotation>) { + // A node in the sub-graph. + counterNodeInSubgraph += 1; // This code will **NOT** run again! + console.log(`Entered 'nodeInSubgraph' a total of ${counterNodeInSubgraph} times`); + return {}; + } + + let counterHumanNode = 0; + + function humanNode(state: z.infer<typeof StateAnnotation>) { + counterHumanNode += 1; // This code will run again! + console.log(`Entered humanNode in sub-graph a total of ${counterHumanNode} times`); + const answer = interrupt("what is your name?"); + console.log(`Got an answer of ${answer}`); + return {}; + } + + const checkpointer = new MemorySaver(); + + const subgraphBuilder = new StateGraph(StateAnnotation) + .addNode("someNode", nodeInSubgraph) + .addNode("humanNode", humanNode) + .addEdge(START, "someNode") + .addEdge("someNode", "humanNode"); + const subgraph = subgraphBuilder.compile({ checkpointer }); + + let counterParentNode = 0; + + async function parentNode(state: z.infer<typeof StateAnnotation>) { + // This parent node will invoke the subgraph. + counterParentNode += 1; // This code will run again on resuming! + console.log(`Entered 'parentNode' a total of ${counterParentNode} times`); + + // Please note that we're intentionally incrementing the state counter + // in the graph state as well to demonstrate that the subgraph update + // of the same key will not conflict with the parent graph (until + const subgraphState = await subgraph.invoke(state); + return subgraphState; + } + + const builder = new StateGraph(StateAnnotation) + .addNode("parentNode", parentNode) + .addEdge(START, "parentNode"); + + // A checkpointer must be enabled for interrupts to work! + const graph = builder.compile({ checkpointer }); + + const config = { + configurable: { + thread_id: uuidv4(), + } + }; + + const stream = await graph.stream({ stateCounter: 1 }, config); + for await (const chunk of stream) { + console.log(chunk); + } + + console.log('--- Resuming ---'); + + const resumeStream = await graph.stream(new Command({ resume: "35" }), config); + for await (const chunk of resumeStream) { + console.log(chunk); + } + ``` + + This will print out + + ``` + Entered 'parentNode' a total of 1 times + Entered 'nodeInSubgraph' a total of 1 times + Entered humanNode in sub-graph a total of 1 times + { __interrupt__: [{ value: 'what is your name?', resumable: true, ns: ['parentNode:4c3a0248-21f0-1287-eacf-3002bc304db4', 'humanNode:2fe86d52-6f70-2a3f-6b2f-b1eededd6348'], when: 'during' }] } + --- Resuming --- + Entered 'parentNode' a total of 2 times + Entered humanNode in sub-graph a total of 2 times + Got an answer of 35 + { parentNode: null } + ``` + ::: ### Using multiple interrupts in a single node @@ -1081,8 +2213,9 @@ Using multiple interrupts within a **single** node can be helpful for patterns l When a node contains multiple interrupt calls, LangGraph keeps a list of resume values specific to the task executing the node. Whenever execution resumes, it starts at the beginning of the node. For each interrupt encountered, LangGraph checks if a matching value exists in the task's resume list. Matching is **strictly index-based**, so the order of interrupt calls within the node is critical. -To avoid issues, refrain from dynamically changing the node's structure between executions. This includes adding, removing, or reordering interrupt calls, as such changes can result in mismatched indices. These problems often arise from unconventional patterns, such as mutating state via `Command(resume=..., update=SOME_STATE_MUTATION)` or relying on global variables to modify the node’s structure dynamically. +To avoid issues, refrain from dynamically changing the node's structure between executions. This includes adding, removing, or reordering interrupt calls, as such changes can result in mismatched indices. These problems often arise from unconventional patterns, such as mutating state via `Command(resume=..., update=SOME_STATE_MUTATION)` or relying on global variables to modify the node's structure dynamically. +:::python ??? example "Extended example: incorrect code that introduces non-determinism" ```python @@ -1090,7 +2223,7 @@ To avoid issues, refrain from dynamically changing the node's structure between from typing import TypedDict, Optional from langgraph.graph import StateGraph - from langgraph.constants import START + from langgraph.constants import START from langgraph.types import interrupt, Command from langgraph.checkpoint.memory import InMemorySaver @@ -1112,9 +2245,9 @@ To avoid issues, refrain from dynamically changing the node's structure between age = interrupt("what is your age?") else: age = "N/A" - + print(f"Name: {name}. Age: {age}") - + return { "age": age, "name": name, @@ -1147,3 +2280,5 @@ To avoid issues, refrain from dynamically changing the node's structure between Name: N/A. Age: John {'human_node': {'age': 'John', 'name': 'N/A'}} ``` + +::: diff --git a/docs/docs/how-tos/human_in_the_loop/time-travel.md b/docs/docs/how-tos/human_in_the_loop/time-travel.md index 84ded8410..6e98e1c05 100644 --- a/docs/docs/how-tos/human_in_the_loop/time-travel.md +++ b/docs/docs/how-tos/human_in_the_loop/time-travel.md @@ -2,11 +2,23 @@ To use [time-travel](../../concepts/time-travel.md) in LangGraph: -1. [Run the graph](#1-run-the-graph) with initial inputs using [`invoke`][langgraph.graph.state.CompiledStateGraph.invoke] or [`stream`][langgraph.graph.state.CompiledStateGraph.stream] methods. -2. [Identify a checkpoint in an existing thread](#2-identify-a-checkpoint): Use the [`get_state_history()`][langgraph.graph.state.CompiledStateGraph.get_state_history] method to retrieve the execution history for a specific `thread_id` and locate the desired `checkpoint_id`. +:::python + +1. [Run the graph](#1-run-the-graph) with initial inputs using @[`invoke`][CompiledStateGraph.invoke] or @[`stream`][CompiledStateGraph.stream] methods. +2. [Identify a checkpoint in an existing thread](#2-identify-a-checkpoint): Use the @[`get_state_history()`][get_state_history] method to retrieve the execution history for a specific `thread_id` and locate the desired `checkpoint_id`. Alternatively, set an [interrupt](../../how-tos/human_in_the_loop/add-human-in-the-loop.md) before the node(s) where you want execution to pause. You can then find the most recent checkpoint recorded up to that interrupt. -3. [Update the graph state (optional)](#3-update-the-state-optional): Use the [`update_state`][langgraph.graph.state.CompiledStateGraph.update_state] method to modify the graph's state at the checkpoint and resume execution from alternative state. +3. [Update the graph state (optional)](#3-update-the-state-optional): Use the @[`update_state`][update_state] method to modify the graph's state at the checkpoint and resume execution from alternative state. 4. [Resume execution from the checkpoint](#4-resume-execution-from-the-checkpoint): Use the `invoke` or `stream` methods with an input of `None` and a configuration containing the appropriate `thread_id` and `checkpoint_id`. + ::: + +:::js + +1. [Run the graph](#1-run-the-graph) with initial inputs using @[`invoke`][CompiledStateGraph.invoke] or @[`stream`][CompiledStateGraph.stream] methods. +2. [Identify a checkpoint in an existing thread](#2-identify-a-checkpoint): Use the @[`getStateHistory()`][get_state_history] method to retrieve the execution history for a specific `thread_id` and locate the desired `checkpoint_id`. + Alternatively, set a [breakpoint](../../concepts/breakpoints.md) before the node(s) where you want execution to pause. You can then find the most recent checkpoint recorded up to that breakpoint. +3. [Update the graph state (optional)](#3-update-the-state-optional): Use the @[`updateState`][update_state] method to modify the graph's state at the checkpoint and resume execution from alternative state. +4. [Resume execution from the checkpoint](#4-resume-execution-from-the-checkpoint): Use the `invoke` or `stream` methods with an input of `null` and a configuration containing the appropriate `thread_id` and `checkpoint_id`. + ::: !!! tip @@ -20,13 +32,27 @@ This example builds a simple LangGraph workflow that generates a joke topic and First we need to install the packages required +:::python + ```python %%capture --no-stderr %pip install --quiet -U langgraph langchain_anthropic ``` +::: + +:::js + +```bash +npm install @langchain/langgraph @langchain/anthropic +``` + +::: + Next, we need to set API keys for Anthropic (the LLM we will use) +:::python + ```python import getpass import os @@ -40,6 +66,16 @@ def _set_env(var: str): _set_env("ANTHROPIC_API_KEY") ``` +::: + +:::js + +```typescript +process.env.ANTHROPIC_API_KEY = "YOUR_API_KEY"; +``` + +::: + <div class="admonition tip"> <p class="admonition-title">Set up <a href="https://smith.langchain.com">LangSmith</a> for LangGraph development</p> <p style="padding-top: 5px;"> @@ -47,6 +83,8 @@ _set_env("ANTHROPIC_API_KEY") </p> </div> +:::python + ```python import uuid @@ -97,8 +135,56 @@ graph = workflow.compile(checkpointer=checkpointer) graph ``` +::: + +:::js + +```typescript +import { v4 as uuidv4 } from "uuid"; +import { z } from "zod"; +import { StateGraph, START, END } from "@langchain/langgraph"; +import { ChatAnthropic } from "@langchain/anthropic"; +import { MemorySaver } from "@langchain/langgraph"; + +const State = z.object({ + topic: z.string().optional(), + joke: z.string().optional(), +}); + +const llm = new ChatAnthropic({ + model: "claude-3-5-sonnet-latest", + temperature: 0, +}); + +// Build workflow +const workflow = new StateGraph(State) + // Add nodes + .addNode("generateTopic", async (state) => { + // LLM call to generate a topic for the joke + const msg = await llm.invoke("Give me a funny topic for a joke"); + return { topic: msg.content }; + }) + .addNode("writeJoke", async (state) => { + // LLM call to write a joke based on the topic + const msg = await llm.invoke(`Write a short joke about ${state.topic}`); + return { joke: msg.content }; + }) + // Add edges to connect nodes + .addEdge(START, "generateTopic") + .addEdge("generateTopic", "writeJoke") + .addEdge("writeJoke", END); + +// Compile +const checkpointer = new MemorySaver(); +const graph = workflow.compile({ checkpointer }); +``` + +::: + ### 1. Run the graph +:::python + ```python config = { "configurable": { @@ -112,7 +198,28 @@ print() print(state["joke"]) ``` +::: + +:::js + +```typescript +const config = { + configurable: { + thread_id: uuidv4(), + }, +}; + +const state = await graph.invoke({}, config); + +console.log(state.topic); +console.log(); +console.log(state.joke); +``` + +::: + **Output:** + ``` How about "The Secret Life of Socks in the Dryer"? You know, exploring the mysterious phenomenon of how socks go into the laundry as pairs but come out as singles. Where do they go? Are they starting new lives elsewhere? Is there a sock paradise we don't know about? There's a lot of comedic potential in the everyday mystery that unites us all! @@ -125,6 +232,8 @@ My blue argyle is now living in Bermuda with a red polka dot, posting vacation p ### 2. Identify a checkpoint +:::python + ```python # The states are returned in reverse chronological order. states = list(graph.get_state_history(config)) @@ -136,6 +245,7 @@ for state in states: ``` **Output:** + ``` () 1f02ac4a-ec9f-6524-8002-8f7b0bbeed0e @@ -150,6 +260,44 @@ for state in states: 1f02ac4a-a4dd-665e-bfff-e6c8c44315d9 ``` +::: + +:::js + +```typescript +// The states are returned in reverse chronological order. +const states = []; +for await (const state of graph.getStateHistory(config)) { + states.push(state); +} + +for (const state of states) { + console.log(state.next); + console.log(state.config.configurable?.checkpoint_id); + console.log(); +} +``` + +**Output:** + +``` +[] +1f02ac4a-ec9f-6524-8002-8f7b0bbeed0e + +['writeJoke'] +1f02ac4a-ce2a-6494-8001-cb2e2d651227 + +['generateTopic'] +1f02ac4a-a4e0-630d-8000-b73c254ba748 + +['__start__'] +1f02ac4a-a4dd-665e-bfff-e6c8c44315d9 +``` + +::: + +:::python + ```python # This is the state before last (states are listed in chronological order) selected_state = states[1] @@ -158,13 +306,35 @@ print(selected_state.values) ``` **Output:** + ``` ('write_joke',) {'topic': 'How about "The Secret Life of Socks in the Dryer"? You know, exploring the mysterious phenomenon of how socks go into the laundry as pairs but come out as singles. Where do they go? Are they starting new lives elsewhere? Is there a sock paradise we don\\'t know about? There\\'s a lot of comedic potential in the everyday mystery that unites us all!'} ``` +::: + +:::js + +```typescript +// This is the state before last (states are listed in chronological order) +const selectedState = states[1]; +console.log(selectedState.next); +console.log(selectedState.values); +``` + +**Output:** + +``` +['writeJoke'] +{'topic': 'How about "The Secret Life of Socks in the Dryer"? You know, exploring the mysterious phenomenon of how socks go into the laundry as pairs but come out as singles. Where do they go? Are they starting new lives elsewhere? Is there a sock paradise we don\\'t know about? There\\'s a lot of comedic potential in the everyday mystery that unites us all!'} +``` + +::: + ### 3. Update the state (optional) +:::python `update_state` will create a new checkpoint. The new checkpoint will be associated with the same thread, but a new checkpoint ID. ```python @@ -173,18 +343,61 @@ print(new_config) ``` **Output:** + ``` {'configurable': {'thread_id': 'c62e2e03-c27b-4cb6-8cea-ea9bfedae006', 'checkpoint_ns': '', 'checkpoint_id': '1f02ac4a-ecee-600b-8002-a1d21df32e4c'}} ``` +::: + +:::js +`updateState` will create a new checkpoint. The new checkpoint will be associated with the same thread, but a new checkpoint ID. + +```typescript +const newConfig = await graph.updateState(selectedState.config, { + topic: "chickens", +}); +console.log(newConfig); +``` + +**Output:** + +``` +{'configurable': {'thread_id': 'c62e2e03-c27b-4cb6-8cea-ea9bfedae006', 'checkpoint_ns': '', 'checkpoint_id': '1f02ac4a-ecee-600b-8002-a1d21df32e4c'}} +``` + +::: + ### 4. Resume execution from the checkpoint +:::python + ```python graph.invoke(None, new_config) ``` **Output:** + ```python {'topic': 'chickens', 'joke': 'Why did the chicken join a band?\n\nBecause it had excellent drumsticks!'} -``` \ No newline at end of file +``` + +::: + +:::js + +```typescript +await graph.invoke(null, newConfig); +``` + +**Output:** + +```typescript +{ + 'topic': 'chickens', + 'joke': 'Why did the chicken join a band?\n\nBecause it had excellent drumsticks!' +} +``` + +::: diff --git a/docs/docs/how-tos/memory/add-memory.md b/docs/docs/how-tos/memory/add-memory.md index 408c260aa..55df5cd48 100644 --- a/docs/docs/how-tos/memory/add-memory.md +++ b/docs/docs/how-tos/memory/add-memory.md @@ -9,6 +9,8 @@ AI applications need [memory](../../concepts/memory.md) to share context across **Short-term** memory (thread-level [persistence](../../concepts/persistence.md)) enables agents to track multi-turn conversations. To add short-term memory: +:::python + ```python # highlight-next-line from langgraph.checkpoint.memory import InMemorySaver @@ -28,10 +30,32 @@ graph.invoke( ) ``` +::: + +:::js + +```typescript +import { MemorySaver, StateGraph } from "@langchain/langgraph"; + +const checkpointer = new MemorySaver(); + +const builder = new StateGraph(...); +const graph = builder.compile({ checkpointer }); + +await graph.invoke( + { messages: [{ role: "user", content: "hi! i am Bob" }] }, + { configurable: { thread_id: "1" } } +); +``` + +::: + ### Use in production In production, use a checkpointer backed by a database: +:::python + ```python from langgraph.checkpoint.postgres import PostgresSaver @@ -43,8 +67,25 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: graph = builder.compile(checkpointer=checkpointer) ``` -??? example "Example: using [Postgres](https://pypi.org/project/langgraph-checkpoint-postgres/) checkpointer" +::: +:::js + +```typescript +import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres"; + +const DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable"; +const checkpointer = PostgresSaver.fromConnString(DB_URI); + +const builder = new StateGraph(...); +const graph = builder.compile({ checkpointer }); +``` + +::: + +??? example "Example: using Postgres checkpointer" + + :::python ``` pip install -U "psycopg[binary,pool]" langgraph langgraph-checkpoint-postgres ``` @@ -59,32 +100,32 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: from langgraph.graph import StateGraph, MessagesState, START # highlight-next-line from langgraph.checkpoint.postgres import PostgresSaver - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable" # highlight-next-line with PostgresSaver.from_conn_string(DB_URI) as checkpointer: # checkpointer.setup() - + def call_model(state: MessagesState): response = model.invoke(state["messages"]) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + # highlight-next-line graph = builder.compile(checkpointer=checkpointer) - + config = { "configurable": { # highlight-next-line "thread_id": "1" } } - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, # highlight-next-line @@ -92,7 +133,7 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: stream_mode="values" ): chunk["messages"][-1].pretty_print() - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "what's my name?"}]}, # highlight-next-line @@ -109,32 +150,32 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: from langgraph.graph import StateGraph, MessagesState, START # highlight-next-line from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable" # highlight-next-line async with AsyncPostgresSaver.from_conn_string(DB_URI) as checkpointer: # await checkpointer.setup() - + async def call_model(state: MessagesState): response = await model.ainvoke(state["messages"]) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + # highlight-next-line graph = builder.compile(checkpointer=checkpointer) - + config = { "configurable": { # highlight-next-line "thread_id": "1" } } - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, # highlight-next-line @@ -142,7 +183,7 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: stream_mode="values" ): chunk["messages"][-1].pretty_print() - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "what's my name?"}]}, # highlight-next-line @@ -151,9 +192,59 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: ): chunk["messages"][-1].pretty_print() ``` + ::: - + :::js + ``` + npm install @langchain/langgraph-checkpoint-postgres + ``` + !!! Setup + You need to call `checkpointer.setup()` the first time you're using Postgres checkpointer + + ```typescript + import { ChatAnthropic } from "@langchain/anthropic"; + import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; + import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres"; + + const model = new ChatAnthropic({ model: "claude-3-5-haiku-20241022" }); + + const DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable"; + const checkpointer = PostgresSaver.fromConnString(DB_URI); + // await checkpointer.setup(); + + const builder = new StateGraph(MessagesZodState) + .addNode("call_model", async (state) => { + const response = await model.invoke(state.messages); + return { messages: [response] }; + }) + .addEdge(START, "call_model"); + + const graph = builder.compile({ checkpointer }); + + const config = { + configurable: { + thread_id: "1" + } + }; + + for await (const chunk of await graph.stream( + { messages: [{ role: "user", content: "hi! I'm bob" }] }, + { ...config, streamMode: "values" } + )) { + console.log(chunk.messages.at(-1)?.content); + } + + for await (const chunk of await graph.stream( + { messages: [{ role: "user", content: "what's my name?" }] }, + { ...config, streamMode: "values" } + )) { + console.log(chunk.messages.at(-1)?.content); + } + ``` + ::: + +:::python ??? example "Example: using [MongoDB](https://pypi.org/project/langgraph-checkpoint-mongodb/) checkpointer" ``` @@ -171,31 +262,31 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: from langgraph.graph import StateGraph, MessagesState, START # highlight-next-line from langgraph.checkpoint.mongodb import MongoDBSaver - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "localhost:27017" # highlight-next-line with MongoDBSaver.from_conn_string(DB_URI) as checkpointer: - + def call_model(state: MessagesState): response = model.invoke(state["messages"]) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + # highlight-next-line graph = builder.compile(checkpointer=checkpointer) - + config = { "configurable": { # highlight-next-line "thread_id": "1" } } - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, # highlight-next-line @@ -203,7 +294,7 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: stream_mode="values" ): chunk["messages"][-1].pretty_print() - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "what's my name?"}]}, # highlight-next-line @@ -220,31 +311,31 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: from langgraph.graph import StateGraph, MessagesState, START # highlight-next-line from langgraph.checkpoint.mongodb.aio import AsyncMongoDBSaver - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "localhost:27017" # highlight-next-line async with AsyncMongoDBSaver.from_conn_string(DB_URI) as checkpointer: - + async def call_model(state: MessagesState): response = await model.ainvoke(state["messages"]) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + # highlight-next-line graph = builder.compile(checkpointer=checkpointer) - + config = { "configurable": { # highlight-next-line "thread_id": "1" } } - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, # highlight-next-line @@ -252,7 +343,7 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: stream_mode="values" ): chunk["messages"][-1].pretty_print() - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "what's my name?"}]}, # highlight-next-line @@ -260,7 +351,7 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: stream_mode="values" ): chunk["messages"][-1].pretty_print() - ``` + ``` ??? example "Example: using [Redis](https://pypi.org/project/langgraph-checkpoint-redis/) checkpointer" @@ -279,32 +370,32 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: from langgraph.graph import StateGraph, MessagesState, START # highlight-next-line from langgraph.checkpoint.redis import RedisSaver - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "redis://localhost:6379" # highlight-next-line with RedisSaver.from_conn_string(DB_URI) as checkpointer: # checkpointer.setup() - + def call_model(state: MessagesState): response = model.invoke(state["messages"]) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + # highlight-next-line graph = builder.compile(checkpointer=checkpointer) - + config = { "configurable": { # highlight-next-line "thread_id": "1" } } - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, # highlight-next-line @@ -312,7 +403,7 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: stream_mode="values" ): chunk["messages"][-1].pretty_print() - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "what's my name?"}]}, # highlight-next-line @@ -329,32 +420,32 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: from langgraph.graph import StateGraph, MessagesState, START # highlight-next-line from langgraph.checkpoint.redis.aio import AsyncRedisSaver - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "redis://localhost:6379" # highlight-next-line async with AsyncRedisSaver.from_conn_string(DB_URI) as checkpointer: # await checkpointer.asetup() - + async def call_model(state: MessagesState): response = await model.ainvoke(state["messages"]) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + # highlight-next-line graph = builder.compile(checkpointer=checkpointer) - + config = { "configurable": { # highlight-next-line "thread_id": "1" } } - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, # highlight-next-line @@ -362,20 +453,24 @@ with PostgresSaver.from_conn_string(DB_URI) as checkpointer: stream_mode="values" ): chunk["messages"][-1].pretty_print() - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "what's my name?"}]}, # highlight-next-line config, stream_mode="values" ): - chunk["messages"][-1].pretty_print() + chunk["messages"][-1].pretty_print() ``` +::: + ### Use in subgraphs If your graph contains [subgraphs](../../concepts/subgraphs.md), you only need to provide the checkpointer when compiling the parent graph. LangGraph will automatically propagate the checkpointer to the child subgraphs. +:::python + ```python from langgraph.graph import START, StateGraph from langgraph.checkpoint.memory import InMemorySaver @@ -397,9 +492,6 @@ subgraph = subgraph_builder.compile() # Parent graph -def node_1(state: State): - return {"foo": "hi! " + state["foo"]} - builder = StateGraph(State) # highlight-next-line builder.add_node("node_1", subgraph) @@ -408,9 +500,38 @@ builder.add_edge(START, "node_1") checkpointer = InMemorySaver() # highlight-next-line graph = builder.compile(checkpointer=checkpointer) -``` +``` -If you want the subgraph to have its own memory, you can compile it `with checkpointer=True`. This is useful in [multi-agent](../../concepts/multi_agent.md) systems, if you want agents to keep track of their internal message histories. +::: + +:::js + +```typescript +import { StateGraph, START, MemorySaver } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ foo: z.string() }); + +const subgraphBuilder = new StateGraph(State) + .addNode("subgraph_node_1", (state) => { + return { foo: state.foo + "bar" }; + }) + .addEdge(START, "subgraph_node_1"); +const subgraph = subgraphBuilder.compile(); + +const builder = new StateGraph(State) + .addNode("node_1", subgraph) + .addEdge(START, "node_1"); + +const checkpointer = new MemorySaver(); +const graph = builder.compile({ checkpointer }); +``` + +::: + +If you want the subgraph to have its own memory, you can compile it with the appropriate checkpointer option. This is useful in [multi-agent](../../concepts/multi_agent.md) systems, if you want agents to keep track of their internal message histories. + +:::python ```python subgraph_builder = StateGraph(...) @@ -418,10 +539,24 @@ subgraph_builder = StateGraph(...) subgraph = subgraph_builder.compile(checkpointer=True) ``` +::: + +:::js + +```typescript +const subgraphBuilder = new StateGraph(...); +// highlight-next-line +const subgraph = subgraphBuilder.compile({ checkpointer: true }); +``` + +::: + ### Read short-term memory in tools { #read-short-term } LangGraph allows agents to access their short-term memory (state) inside the tools. +:::python + ```python from typing import Annotated from langgraph.prebuilt import InjectedState, create_react_agent @@ -453,12 +588,58 @@ agent.invoke({ }) ``` +::: + +:::js + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import { + MessagesZodState, + LangGraphRunnableConfig, +} from "@langchain/langgraph"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const CustomState = z.object({ + messages: MessagesZodState.shape.messages, + userId: z.string(), +}); + +const getUserInfo = tool( + async (_, config: LangGraphRunnableConfig) => { + const userId = config.configurable?.userId; + return userId === "user_123" ? "User is John Smith" : "Unknown user"; + }, + { + name: "get_user_info", + description: "Look up user info.", + schema: z.object({}), + } +); + +const agent = createReactAgent({ + llm: model, + tools: [getUserInfo], + stateSchema: CustomState, +}); + +await agent.invoke({ + messages: [{ role: "user", content: "look up user information" }], + userId: "user_123", +}); +``` + +::: + See the [Context](../../agents/context.md) guide for more information. ### Write short-term memory from tools { #write-short-term } To modify the agent's short-term memory (state) during execution, you can return state updates directly from the tools. This is useful for persisting intermediate results or making information accessible to subsequent tools or prompts. +:::python + ```python from typing import Annotated from langchain_core.tools import InjectedToolCallId @@ -514,10 +695,82 @@ agent.invoke( ) ``` +::: + +:::js + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import { + MessagesZodState, + LangGraphRunnableConfig, + Command, +} from "@langchain/langgraph"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const CustomState = z.object({ + messages: MessagesZodState.shape.messages, + userName: z.string().optional(), +}); + +const updateUserInfo = tool( + async (_, config: LangGraphRunnableConfig) => { + const userId = config.configurable?.userId; + const name = userId === "user_123" ? "John Smith" : "Unknown user"; + return new Command({ + update: { + userName: name, + // update the message history + messages: [ + { + role: "tool", + content: "Successfully looked up user information", + tool_call_id: config.toolCall?.id, + }, + ], + }, + }); + }, + { + name: "update_user_info", + description: "Look up and update user info.", + schema: z.object({}), + } +); + +const greet = tool( + async (_, config: LangGraphRunnableConfig) => { + const userName = config.configurable?.userName; + return `Hello ${userName}!`; + }, + { + name: "greet", + description: "Use this to greet the user once you found their info.", + schema: z.object({}), + } +); + +const agent = createReactAgent({ + llm: model, + tools: [updateUserInfo, greet], + stateSchema: CustomState, +}); + +await agent.invoke( + { messages: [{ role: "user", content: "greet the user" }] }, + { configurable: { userId: "user_123" } } +); +``` + +::: + ## Add long-term memory Use long-term memory to store user-specific or application-specific data across conversations. +:::python + ```python # highlight-next-line from langgraph.store.memory import InMemoryStore @@ -531,10 +784,27 @@ builder = StateGraph(...) graph = builder.compile(store=store) ``` +::: + +:::js + +```typescript +import { InMemoryStore, StateGraph } from "@langchain/langgraph"; + +const store = new InMemoryStore(); + +const builder = new StateGraph(...); +const graph = builder.compile({ store }); +``` + +::: + ### Use in production In production, use a store backed by a database: +:::python + ```python from langgraph.store.postgres import PostgresStore @@ -546,8 +816,25 @@ with PostgresStore.from_conn_string(DB_URI) as store: graph = builder.compile(store=store) ``` -??? example "Example: using [Postgres](https://pypi.org/project/langgraph-checkpoint-postgres/) store" +::: +:::js + +```typescript +import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres"; + +const DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable"; +const store = PostgresStore.fromConnString(DB_URI); + +const builder = new StateGraph(...); +const graph = builder.compile({ store }); +``` + +::: + +??? example "Example: using Postgres store" + + :::python ``` pip install -U "psycopg[binary,pool]" langgraph langgraph-checkpoint-postgres ``` @@ -565,11 +852,11 @@ with PostgresStore.from_conn_string(DB_URI) as store: # highlight-next-line from langgraph.store.postgres import PostgresStore from langgraph.store.base import BaseStore - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable" - + with ( # highlight-next-line PostgresStore.from_conn_string(DB_URI) as store, @@ -577,7 +864,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: ): # store.setup() # checkpointer.setup() - + def call_model( state: MessagesState, config: RunnableConfig, @@ -591,29 +878,29 @@ with PostgresStore.from_conn_string(DB_URI) as store: memories = store.search(namespace, query=str(state["messages"][-1].content)) info = "\n".join([d.value["data"] for d in memories]) system_msg = f"You are a helpful assistant talking to the user. User info: {info}" - + # Store new memories if the user asks the model to remember last_message = state["messages"][-1] if "remember" in last_message.content.lower(): memory = "User name is Bob" # highlight-next-line store.put(namespace, str(uuid.uuid4()), {"data": memory}) - + response = model.invoke( [{"role": "system", "content": system_msg}] + state["messages"] ) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + graph = builder.compile( checkpointer=checkpointer, # highlight-next-line store=store, ) - + config = { "configurable": { # highlight-next-line @@ -629,7 +916,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: stream_mode="values", ): chunk["messages"][-1].pretty_print() - + config = { "configurable": { # highlight-next-line @@ -637,7 +924,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: "user_id": "1", } } - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "what is my name?"}]}, # highlight-next-line @@ -657,11 +944,11 @@ with PostgresStore.from_conn_string(DB_URI) as store: # highlight-next-line from langgraph.store.postgres.aio import AsyncPostgresStore from langgraph.store.base import BaseStore - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable" - + async with ( # highlight-next-line AsyncPostgresStore.from_conn_string(DB_URI) as store, @@ -669,7 +956,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: ): # await store.setup() # await checkpointer.setup() - + async def call_model( state: MessagesState, config: RunnableConfig, @@ -683,29 +970,29 @@ with PostgresStore.from_conn_string(DB_URI) as store: memories = await store.asearch(namespace, query=str(state["messages"][-1].content)) info = "\n".join([d.value["data"] for d in memories]) system_msg = f"You are a helpful assistant talking to the user. User info: {info}" - + # Store new memories if the user asks the model to remember last_message = state["messages"][-1] if "remember" in last_message.content.lower(): memory = "User name is Bob" # highlight-next-line await store.aput(namespace, str(uuid.uuid4()), {"data": memory}) - + response = await model.ainvoke( [{"role": "system", "content": system_msg}] + state["messages"] ) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + graph = builder.compile( checkpointer=checkpointer, # highlight-next-line store=store, ) - + config = { "configurable": { # highlight-next-line @@ -721,7 +1008,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: stream_mode="values", ): chunk["messages"][-1].pretty_print() - + config = { "configurable": { # highlight-next-line @@ -729,7 +1016,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: "user_id": "1", } } - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "what is my name?"}]}, # highlight-next-line @@ -738,7 +1025,96 @@ with PostgresStore.from_conn_string(DB_URI) as store: ): chunk["messages"][-1].pretty_print() ``` + ::: + :::js + ``` + npm install @langchain/langgraph-checkpoint-postgres + ``` + + !!! Setup + You need to call `store.setup()` the first time you're using Postgres store + + ```typescript + import { ChatAnthropic } from "@langchain/anthropic"; + import { StateGraph, MessagesZodState, START, LangGraphRunnableConfig } from "@langchain/langgraph"; + import { PostgresSaver, PostgresStore } from "@langchain/langgraph-checkpoint-postgres"; + import { z } from "zod"; + import { v4 as uuidv4 } from "uuid"; + + const model = new ChatAnthropic({ model: "claude-3-5-haiku-20241022" }); + + const DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable"; + + const store = PostgresStore.fromConnString(DB_URI); + const checkpointer = PostgresSaver.fromConnString(DB_URI); + // await store.setup(); + // await checkpointer.setup(); + + const callModel = async ( + state: z.infer<typeof MessagesZodState>, + config: LangGraphRunnableConfig, + ) => { + const userId = config.configurable?.userId; + const namespace = ["memories", userId]; + const memories = await config.store?.search(namespace, { query: state.messages.at(-1)?.content }); + const info = memories?.map(d => d.value.data).join("\n") || ""; + const systemMsg = `You are a helpful assistant talking to the user. User info: ${info}`; + + // Store new memories if the user asks the model to remember + const lastMessage = state.messages.at(-1); + if (lastMessage?.content?.toLowerCase().includes("remember")) { + const memory = "User name is Bob"; + await config.store?.put(namespace, uuidv4(), { data: memory }); + } + + const response = await model.invoke([ + { role: "system", content: systemMsg }, + ...state.messages + ]); + return { messages: [response] }; + }; + + const builder = new StateGraph(MessagesZodState) + .addNode("call_model", callModel) + .addEdge(START, "call_model"); + + const graph = builder.compile({ + checkpointer, + store, + }); + + const config = { + configurable: { + thread_id: "1", + userId: "1", + } + }; + + for await (const chunk of await graph.stream( + { messages: [{ role: "user", content: "Hi! Remember: my name is Bob" }] }, + { ...config, streamMode: "values" } + )) { + console.log(chunk.messages.at(-1)?.content); + } + + const config2 = { + configurable: { + thread_id: "2", + userId: "1", + } + }; + + for await (const chunk of await graph.stream( + { messages: [{ role: "user", content: "what is my name?" }] }, + { ...config2, streamMode: "values" } + )) { + console.log(chunk.messages.at(-1)?.content); + } + ``` + ::: + +:::python ??? example "Example: using [Redis](https://pypi.org/project/langgraph-checkpoint-redis/) store" ``` @@ -759,11 +1135,11 @@ with PostgresStore.from_conn_string(DB_URI) as store: # highlight-next-line from langgraph.store.redis import RedisStore from langgraph.store.base import BaseStore - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "redis://localhost:6379" - + with ( # highlight-next-line RedisStore.from_conn_string(DB_URI) as store, @@ -771,7 +1147,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: ): store.setup() checkpointer.setup() - + def call_model( state: MessagesState, config: RunnableConfig, @@ -785,29 +1161,29 @@ with PostgresStore.from_conn_string(DB_URI) as store: memories = store.search(namespace, query=str(state["messages"][-1].content)) info = "\n".join([d.value["data"] for d in memories]) system_msg = f"You are a helpful assistant talking to the user. User info: {info}" - + # Store new memories if the user asks the model to remember last_message = state["messages"][-1] if "remember" in last_message.content.lower(): memory = "User name is Bob" # highlight-next-line store.put(namespace, str(uuid.uuid4()), {"data": memory}) - + response = model.invoke( [{"role": "system", "content": system_msg}] + state["messages"] ) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + graph = builder.compile( checkpointer=checkpointer, # highlight-next-line store=store, ) - + config = { "configurable": { # highlight-next-line @@ -823,7 +1199,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: stream_mode="values", ): chunk["messages"][-1].pretty_print() - + config = { "configurable": { # highlight-next-line @@ -831,7 +1207,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: "user_id": "1", } } - + for chunk in graph.stream( {"messages": [{"role": "user", "content": "what is my name?"}]}, # highlight-next-line @@ -851,11 +1227,11 @@ with PostgresStore.from_conn_string(DB_URI) as store: # highlight-next-line from langgraph.store.redis.aio import AsyncRedisStore from langgraph.store.base import BaseStore - + model = init_chat_model(model="anthropic:claude-3-5-haiku-latest") - + DB_URI = "redis://localhost:6379" - + async with ( # highlight-next-line AsyncRedisStore.from_conn_string(DB_URI) as store, @@ -863,7 +1239,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: ): # await store.setup() # await checkpointer.asetup() - + async def call_model( state: MessagesState, config: RunnableConfig, @@ -877,29 +1253,29 @@ with PostgresStore.from_conn_string(DB_URI) as store: memories = await store.asearch(namespace, query=str(state["messages"][-1].content)) info = "\n".join([d.value["data"] for d in memories]) system_msg = f"You are a helpful assistant talking to the user. User info: {info}" - + # Store new memories if the user asks the model to remember last_message = state["messages"][-1] if "remember" in last_message.content.lower(): memory = "User name is Bob" # highlight-next-line await store.aput(namespace, str(uuid.uuid4()), {"data": memory}) - + response = await model.ainvoke( [{"role": "system", "content": system_msg}] + state["messages"] ) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") - + graph = builder.compile( checkpointer=checkpointer, # highlight-next-line store=store, ) - + config = { "configurable": { # highlight-next-line @@ -915,7 +1291,7 @@ with PostgresStore.from_conn_string(DB_URI) as store: stream_mode="values", ): chunk["messages"][-1].pretty_print() - + config = { "configurable": { # highlight-next-line @@ -923,18 +1299,22 @@ with PostgresStore.from_conn_string(DB_URI) as store: "user_id": "1", } } - + async for chunk in graph.astream( {"messages": [{"role": "user", "content": "what is my name?"}]}, # highlight-next-line config, stream_mode="values", ): - chunk["messages"][-1].pretty_print() + chunk["messages"][-1].pretty_print() ``` +::: + ### Read long-term memory in tools { #read-long-term } +:::python + ```python title="A tool the agent can use to look up user information" from langchain_core.runnables import RunnableConfig from langgraph.config import get_store @@ -980,16 +1360,78 @@ agent.invoke( ``` 1. The `InMemoryStore` is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the [store documentation](../../reference/store.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready store for you. -2. For this example, we write some sample data to the store using the `put` method. Please see the [BaseStore.put][langgraph.store.base.BaseStore.put] API reference for more details. +2. For this example, we write some sample data to the store using the `put` method. Please see the @[BaseStore.put] API reference for more details. 3. The first argument is the namespace. This is used to group related data together. In this case, we are using the `users` namespace to group user data. 4. A key within the namespace. This example uses a user ID for the key. 5. The data that we want to store for the given user. 6. The `get_store` function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. 7. The `get` method is used to retrieve data from the store. The first argument is the namespace, and the second argument is the key. This will return a `StoreValue` object, which contains the value and metadata about the value. 8. The `store` is passed to the agent. This enables the agent to access the store when running tools. You can also use the `get_store` function to access the store from anywhere in your code. + ::: + +:::js + +```typescript title="A tool the agent can use to look up user information" +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import { LangGraphRunnableConfig, InMemoryStore } from "@langchain/langgraph"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const store = new InMemoryStore(); // (1)! + +await store.put( + // (2)! + ["users"], // (3)! + "user_123", // (4)! + { + name: "John Smith", + language: "English", + } // (5)! +); + +const getUserInfo = tool( + async (_, config: LangGraphRunnableConfig) => { + /**Look up user info.*/ + // Same as that provided to `createReactAgent` + const store = config.store; // (6)! + const userId = config.configurable?.userId; + const userInfo = await store?.get(["users"], userId); // (7)! + return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user"; + }, + { + name: "get_user_info", + description: "Look up user info.", + schema: z.object({}), + } +); + +const agent = createReactAgent({ + llm: model, + tools: [getUserInfo], + store, // (8)! +}); + +// Run the agent +await agent.invoke( + { messages: [{ role: "user", content: "look up user information" }] }, + { configurable: { userId: "user_123" } } +); +``` + +1. The `InMemoryStore` is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the [store documentation](../../reference/store.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready store for you. +2. For this example, we write some sample data to the store using the `put` method. Please see the @[BaseStore.put] API reference for more details. +3. The first argument is the namespace. This is used to group related data together. In this case, we are using the `users` namespace to group user data. +4. A key within the namespace. This example uses a user ID for the key. +5. The data that we want to store for the given user. +6. The store is accessible through the config. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. +7. The `get` method is used to retrieve data from the store. The first argument is the namespace, and the second argument is the key. This will return a `StoreValue` object, which contains the value and metadata about the value. +8. The `store` is passed to the agent. This enables the agent to access the store when running tools. You can also use the store from the config to access it from anywhere in your code. + ::: ### Write long-term memory from tools { #write-long-term } +:::python + ```python title="Example of a tool that updates user information" from typing_extensions import TypedDict @@ -1036,11 +1478,74 @@ store.get(("users",), "user_123").value 4. The `get_store` function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. 5. The `put` method is used to store data in the store. The first argument is the namespace, and the second argument is the key. This will store the user information in the store. 6. The `user_id` is passed in the config. This is used to identify the user whose information is being updated. + ::: + +:::js + +```typescript title="Example of a tool that updates user information" +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import { LangGraphRunnableConfig, InMemoryStore } from "@langchain/langgraph"; +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const store = new InMemoryStore(); // (1)! + +const UserInfo = z.object({ + // (2)! + name: z.string(), +}); + +const saveUserInfo = tool( + async ( + userInfo: z.infer<typeof UserInfo>, + config: LangGraphRunnableConfig + ) => { + // (3)! + /**Save user info.*/ + // Same as that provided to `createReactAgent` + const store = config.store; // (4)! + const userId = config.configurable?.userId; + await store?.put(["users"], userId, userInfo); // (5)! + return "Successfully saved user info."; + }, + { + name: "save_user_info", + description: "Save user info.", + schema: UserInfo, + } +); + +const agent = createReactAgent({ + llm: model, + tools: [saveUserInfo], + store, +}); + +// Run the agent +await agent.invoke( + { messages: [{ role: "user", content: "My name is John Smith" }] }, + { configurable: { userId: "user_123" } } // (6)! +); + +// You can access the store directly to get the value +const result = await store.get(["users"], "user_123"); +console.log(result?.value); +``` + +1. The `InMemoryStore` is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the [store documentation](../../reference/store.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready store for you. +2. The `UserInfo` schema defines the structure of the user information. The LLM will use this to format the response according to the schema. +3. The `saveUserInfo` function is a tool that allows an agent to update user information. This could be useful for a chat application where the user wants to update their profile information. +4. The store is accessible through the config. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. +5. The `put` method is used to store data in the store. The first argument is the namespace, and the second argument is the key. This will store the user information in the store. +6. The `userId` is passed in the config. This is used to identify the user whose information is being updated. + ::: ### Use semantic search Enable semantic search in your graph's memory store to let graph agents search for items in the store by semantic similarity. +:::python + ```python from langchain.embeddings import init_embeddings from langgraph.store.memory import InMemoryStore @@ -1062,19 +1567,48 @@ items = store.search( ) ``` +::: + +:::js + +```typescript +import { OpenAIEmbeddings } from "@langchain/openai"; +import { InMemoryStore } from "@langchain/langgraph"; + +// Create store with semantic search enabled +const embeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small" }); +const store = new InMemoryStore({ + index: { + embeddings, + dims: 1536, + }, +}); + +await store.put(["user_123", "memories"], "1", { text: "I love pizza" }); +await store.put(["user_123", "memories"], "2", { text: "I am a plumber" }); + +const items = await store.search(["user_123", "memories"], { + query: "I'm hungry", + limit: 1, +}); +``` + +::: + ??? example "Long-term memory with semantic search" + :::python ```python from typing import Optional - + from langchain.embeddings import init_embeddings from langchain.chat_models import init_chat_model from langgraph.store.base import BaseStore from langgraph.store.memory import InMemoryStore from langgraph.graph import START, MessagesState, StateGraph - + llm = init_chat_model("openai:gpt-4o-mini") - + # Create store with semantic search enabled embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( @@ -1083,10 +1617,10 @@ items = store.search( "dims": 1536, } ) - + store.put(("user_123", "memories"), "1", {"text": "I love pizza"}) store.put(("user_123", "memories"), "2", {"text": "I am a plumber"}) - + def chat(state, *, store: BaseStore): # Search based on user's last message items = store.search( @@ -1101,19 +1635,73 @@ items = store.search( ] ) return {"messages": [response]} - - + + builder = StateGraph(MessagesState) builder.add_node(chat) builder.add_edge(START, "chat") graph = builder.compile(store=store) - + for message, metadata in graph.stream( input={"messages": [{"role": "user", "content": "I'm hungry"}]}, stream_mode="messages", ): print(message.content, end="") ``` + ::: + + :::js + ```typescript + import { OpenAIEmbeddings, ChatOpenAI } from "@langchain/openai"; + import { StateGraph, START, MessagesZodState, InMemoryStore } from "@langchain/langgraph"; + import { z } from "zod"; + + const llm = new ChatOpenAI({ model: "gpt-4o-mini" }); + + // Create store with semantic search enabled + const embeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small" }); + const store = new InMemoryStore({ + index: { + embeddings, + dims: 1536, + } + }); + + await store.put(["user_123", "memories"], "1", { text: "I love pizza" }); + await store.put(["user_123", "memories"], "2", { text: "I am a plumber" }); + + const chat = async (state: z.infer<typeof MessagesZodState>, config) => { + // Search based on user's last message + const items = await config.store.search( + ["user_123", "memories"], + { query: state.messages.at(-1)?.content, limit: 2 } + ); + const memories = items.map(item => item.value.text).join("\n"); + const memoriesText = memories ? `## Memories of user\n${memories}` : ""; + + const response = await llm.invoke([ + { role: "system", content: `You are a helpful assistant.\n${memoriesText}` }, + ...state.messages, + ]); + + return { messages: [response] }; + }; + + const builder = new StateGraph(MessagesZodState) + .addNode("chat", chat) + .addEdge(START, "chat"); + const graph = builder.compile({ store }); + + for await (const [message, metadata] of await graph.stream( + { messages: [{ role: "user", content: "I'm hungry" }] }, + { streamMode: "messages" } + )) { + if (message.content) { + console.log(message.content); + } + } + ``` + ::: See [this guide](../../cloud/deployment/semantic_search.md) for more information on how to use semantic search with LangGraph memory store. @@ -1121,21 +1709,22 @@ See [this guide](../../cloud/deployment/semantic_search.md) for more information With [short-term memory](#add-short-term-memory) enabled, long conversations can exceed the LLM's context window. Common solutions are: -* [Trim messages](#trim-messages): Remove first or last N messages (before calling LLM) -* [Delete messages](#delete-messages) from LangGraph state permanently -* [Summarize messages](#summarize-messages): Summarize earlier messages in the history and replace them with a summary -* [Manage checkpoints](#manage-checkpoints) to store and retrieve message history -* Custom strategies (e.g., message filtering, etc.) +- [Trim messages](#trim-messages): Remove first or last N messages (before calling LLM) +- [Delete messages](#delete-messages) from LangGraph state permanently +- [Summarize messages](#summarize-messages): Summarize earlier messages in the history and replace them with a summary +- [Manage checkpoints](#manage-checkpoints) to store and retrieve message history +- Custom strategies (e.g., message filtering, etc.) This allows the agent to keep track of the conversation without exceeding the LLM's context window. ### Trim messages -Most LLMs have a maximum supported context window (denominated in tokens). One way to decide when to truncate messages is to count the tokens in the message history and truncate whenever it approaches that limit. If you're using LangChain, you can use the `trim_messages` utility and specify the number of tokens to keep from the list, as well as the `strategy` (e.g., keep the last `max_tokens`) to use for handling the boundary. +Most LLMs have a maximum supported context window (denominated in tokens). One way to decide when to truncate messages is to count the tokens in the message history and truncate whenever it approaches that limit. If you're using LangChain, you can use the trim messages utility and specify the number of tokens to keep from the list, as well as the `strategy` (e.g., keep the last `maxTokens`) to use for handling the boundary. === "In an agent" - To trim message history in an agent, use [`pre_model_hook`][langgraph.prebuilt.chat_agent_executor.create_react_agent] with the [`trim_messages`](https://python.langchain.com/api_reference/core/messages/langchain_core.messages.utils.trim_messages.html) function: + :::python + To trim message history in an agent, use @[`pre_model_hook`][create_react_agent] with the [`trim_messages`](https://python.langchain.com/api_reference/core/messages/langchain_core.messages.utils.trim_messages.html) function: ```python # highlight-next-line @@ -1170,9 +1759,38 @@ Most LLMs have a maximum supported context window (denominated in tokens). One w checkpointer=checkpointer, ) ``` + ::: + + :::js + To trim message history in an agent, use `stateModifier` with the [`trimMessages`](https://js.langchain.com/docs/how_to/trim_messages/) function: + + ```typescript + import { trimMessages } from "@langchain/core/messages"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + + // This function will be called every time before the node that calls LLM + const stateModifier = async (state) => { + return trimMessages(state.messages, { + strategy: "last", + maxTokens: 384, + startOn: "human", + endOn: ["human", "tool"], + }); + }; + + const checkpointer = new MemorySaver(); + const agent = createReactAgent({ + llm: model, + tools, + stateModifier, + checkpointer, + }); + ``` + ::: === "In a workflow" + :::python To trim message history, use the [`trim_messages`](https://python.langchain.com/api_reference/core/messages/langchain_core.messages.utils.trim_messages.html) function: ```python @@ -1202,9 +1820,34 @@ Most LLMs have a maximum supported context window (denominated in tokens). One w builder.add_node(call_model) ... ``` + ::: + + :::js + To trim message history, use the [`trimMessages`](https://js.langchain.com/docs/how_to/trim_messages/) function: + + ```typescript + import { trimMessages } from "@langchain/core/messages"; + + const callModel = async (state: z.infer<typeof MessagesZodState>) => { + const messages = trimMessages(state.messages, { + strategy: "last", + maxTokens: 128, + startOn: "human", + endOn: ["human", "tool"], + }); + const response = await model.invoke(messages); + return { messages: [response] }; + }; + + const builder = new StateGraph(MessagesZodState) + .addNode("call_model", callModel); + // ... + ``` + ::: ??? example "Full example: trim messages" + :::python ```python # highlight-next-line from langchain_core.messages.utils import ( @@ -1216,10 +1859,10 @@ Most LLMs have a maximum supported context window (denominated in tokens). One w ) from langchain.chat_models import init_chat_model from langgraph.graph import StateGraph, START, MessagesState - + model = init_chat_model("anthropic:claude-3-7-sonnet-latest") summarization_model = model.bind(max_tokens=128) - + def call_model(state: MessagesState): # highlight-next-line messages = trim_messages( @@ -1232,13 +1875,13 @@ Most LLMs have a maximum supported context window (denominated in tokens). One w ) response = model.invoke(messages) return {"messages": [response]} - + checkpointer = InMemorySaver() builder = StateGraph(MessagesState) builder.add_node(call_model) builder.add_edge(START, "call_model") graph = builder.compile(checkpointer=checkpointer) - + config = {"configurable": {"thread_id": "1"}} graph.invoke({"messages": "hi, my name is bob"}, config) graph.invoke({"messages": "write a short poem about cats"}, config) @@ -1250,15 +1893,57 @@ Most LLMs have a maximum supported context window (denominated in tokens). One w ``` ================================== Ai Message ================================== - + Your name is Bob, as you mentioned when you first introduced yourself. ``` + ::: + + :::js + ```typescript + import { trimMessages } from "@langchain/core/messages"; + import { ChatAnthropic } from "@langchain/anthropic"; + import { StateGraph, START, MessagesZodState, MemorySaver } from "@langchain/langgraph"; + import { z } from "zod"; + + const model = new ChatAnthropic({ model: "claude-3-5-sonnet-20241022" }); + + const callModel = async (state: z.infer<typeof MessagesZodState>) => { + const messages = trimMessages(state.messages, { + strategy: "last", + maxTokens: 128, + startOn: "human", + endOn: ["human", "tool"], + }); + const response = await model.invoke(messages); + return { messages: [response] }; + }; + + const checkpointer = new MemorySaver(); + const builder = new StateGraph(MessagesZodState) + .addNode("call_model", callModel) + .addEdge(START, "call_model"); + const graph = builder.compile({ checkpointer }); + + const config = { configurable: { thread_id: "1" } }; + await graph.invoke({ messages: [{ role: "user", content: "hi, my name is bob" }] }, config); + await graph.invoke({ messages: [{ role: "user", content: "write a short poem about cats" }] }, config); + await graph.invoke({ messages: [{ role: "user", content: "now do the same but for dogs" }] }, config); + const finalResponse = await graph.invoke({ messages: [{ role: "user", content: "what's my name?" }] }, config); + + console.log(finalResponse.messages.at(-1)?.content); + ``` + + ``` + Your name is Bob, as you mentioned when you first introduced yourself. + ``` + ::: ### Delete messages You can delete messages from the graph state to manage the message history. This is useful when you want to remove specific messages or clear the entire message history. -To delete messages from the graph state, you can use the `RemoveMessage`. For `RemoveMessage` to work, you need to use a state key with [`add_messages`][langgraph.graph.message.add_messages] [reducer](../../concepts/low_level.md#reducers), like [`MessagesState`](../../concepts/low_level.md#messagesstate). +:::python +To delete messages from the graph state, you can use the `RemoveMessage`. For `RemoveMessage` to work, you need to use a state key with @[`add_messages`][add_messages] [reducer](../../concepts/low_level.md#reducers), like [`MessagesState`](../../concepts/low_level.md#messagesstate). To remove specific messages: @@ -1275,7 +1960,7 @@ def delete_messages(state): ``` To remove **all** messages: - + ```python # highlight-next-line from langgraph.graph.message import REMOVE_ALL_MESSAGES @@ -1285,44 +1970,70 @@ def delete_messages(state): return {"messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES)]} ``` +::: + +:::js +To delete messages from the graph state, you can use the `RemoveMessage`. For `RemoveMessage` to work, you need to use a state key with @[`messagesStateReducer`][messagesStateReducer] [reducer](../../concepts/low_level.md#reducers), like [`MessagesZodState`](../../concepts/low_level.md#messageszodstate). + +To remove specific messages: + +```typescript +import { RemoveMessage } from "@langchain/core/messages"; + +const deleteMessages = (state) => { + const messages = state.messages; + if (messages.length > 2) { + // remove the earliest two messages + return { + messages: messages + .slice(0, 2) + .map((m) => new RemoveMessage({ id: m.id })), + }; + } +}; +``` + +::: + !!! warning When deleting messages, **make sure** that the resulting message history is valid. Check the limitations of the LLM provider you're using. For example: - + * some providers expect message history to start with a `user` message * most providers require `assistant` messages with tool calls to be followed by corresponding `tool` result messages. ??? example "Full example: delete messages" + :::python ```python # highlight-next-line from langchain_core.messages import RemoveMessage - + def delete_messages(state): messages = state["messages"] if len(messages) > 2: # remove the earliest two messages # highlight-next-line return {"messages": [RemoveMessage(id=m.id) for m in messages[:2]]} - + def call_model(state: MessagesState): response = model.invoke(state["messages"]) return {"messages": response} - + builder = StateGraph(MessagesState) builder.add_sequence([call_model, delete_messages]) builder.add_edge(START, "call_model") - + checkpointer = InMemorySaver() app = builder.compile(checkpointer=checkpointer) - + for event in app.stream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, config, stream_mode="values" ): print([(message.type, message.content) for message in event["messages"]]) - + for event in app.stream( {"messages": [{"role": "user", "content": "what's my name?"}]}, config, @@ -1338,6 +2049,65 @@ def delete_messages(state): [('human', "hi! I'm bob"), ('ai', 'Hi Bob! How are you doing today? Is there anything I can help you with?'), ('human', "what's my name?"), ('ai', 'Your name is Bob.')] [('human', "what's my name?"), ('ai', 'Your name is Bob.')] ``` + ::: + + :::js + ```typescript + import { RemoveMessage } from "@langchain/core/messages"; + import { ChatAnthropic } from "@langchain/anthropic"; + import { StateGraph, START, MessagesZodState, MemorySaver } from "@langchain/langgraph"; + import { z } from "zod"; + + const model = new ChatAnthropic({ model: "claude-3-5-sonnet-20241022" }); + + const deleteMessages = (state: z.infer<typeof MessagesZodState>) => { + const messages = state.messages; + if (messages.length > 2) { + // remove the earliest two messages + return { messages: messages.slice(0, 2).map(m => new RemoveMessage({ id: m.id })) }; + } + return {}; + }; + + const callModel = async (state: z.infer<typeof MessagesZodState>) => { + const response = await model.invoke(state.messages); + return { messages: [response] }; + }; + + const builder = new StateGraph(MessagesZodState) + .addNode("call_model", callModel) + .addNode("delete_messages", deleteMessages) + .addEdge(START, "call_model") + .addEdge("call_model", "delete_messages"); + + const checkpointer = new MemorySaver(); + const app = builder.compile({ checkpointer }); + + const config = { configurable: { thread_id: "1" } }; + + for await (const event of await app.stream( + { messages: [{ role: "user", content: "hi! I'm bob" }] }, + { ...config, streamMode: "values" } + )) { + console.log(event.messages.map(message => [message.getType(), message.content])); + } + + for await (const event of await app.stream( + { messages: [{ role: "user", content: "what's my name?" }] }, + { ...config, streamMode: "values" } + )) { + console.log(event.messages.map(message => [message.getType(), message.content])); + } + ``` + + ``` + [['human', "hi! I'm bob"]] + [['human', "hi! I'm bob"], ['ai', 'Hi Bob! How are you doing today? Is there anything I can help you with?']] + [['human', "hi! I'm bob"], ['ai', 'Hi Bob! How are you doing today? Is there anything I can help you with?'], ['human', "what's my name?"]] + [['human', "hi! I'm bob"], ['ai', 'Hi Bob! How are you doing today? Is there anything I can help you with?'], ['human', "what's my name?"], ['ai', 'Your name is Bob.']] + [['human', "what's my name?"], ['ai', 'Your name is Bob.']] + ``` + ::: ### Summarize messages @@ -1347,7 +2117,8 @@ The problem with trimming or removing messages, as shown above, is that you may === "In an agent" - To summarize message history in an agent, use [`pre_model_hook`][langgraph.prebuilt.chat_agent_executor.create_react_agent] with a prebuilt [`SummarizationNode`](https://langchain-ai.github.io/langmem/reference/short_term/#langmem.short_term.SummarizationNode) abstraction: + :::python + To summarize message history in an agent, use @[`pre_model_hook`][create_react_agent] with a prebuilt [`SummarizationNode`](https://langchain-ai.github.io/langmem/reference/short_term/#langmem.short_term.SummarizationNode) abstraction: ```python from langchain_anthropic import ChatAnthropic @@ -1391,12 +2162,13 @@ The problem with trimming or removing messages, as shown above, is that you may 1. The `InMemorySaver` is a checkpointer that stores the agent's state in memory. In a production setting, you would typically use a database or other persistent storage. Please review the [checkpointer documentation](../../reference/checkpoints.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready checkpointer for you. 2. The `context` key is added to the agent's state. The key contains book-keeping information for the summarization node. It is used to keep track of the last summary information and ensure that the agent doesn't summarize on every LLM call, which can be inefficient. 3. The `checkpointer` is passed to the agent. This enables the agent to persist its state across invocations. - 4. The `pre_model_hook` is set to the `SummarizationNode`. This node will summarize the message history before sending it to the LLM. The summarization node will automatically handle the summarization process and update the agent's state with the new summary. You can replace this with a custom implementation if you prefer. Please see the [create_react_agent][langgraph.prebuilt.chat_agent_executor.create_react_agent] API reference for more details. + 4. The `pre_model_hook` is set to the `SummarizationNode`. This node will summarize the message history before sending it to the LLM. The summarization node will automatically handle the summarization process and update the agent's state with the new summary. You can replace this with a custom implementation if you prefer. Please see the @[create_react_agent][create_react_agent] API reference for more details. 5. The `state_schema` is set to the `State` class, which is the custom state that contains an extra `context` key. - + ::: === "In a workflow" + :::python Prompting and orchestration logic can be used to summarize the message history. For example, in LangGraph you can extend the [`MessagesState`](../../concepts/low_level.md#working-with-messages-in-graph-state) to include a `summary` key: ```python @@ -1433,14 +2205,66 @@ The problem with trimming or removing messages, as shown above, is that you may delete_messages = [RemoveMessage(id=m.id) for m in state["messages"][:-2]] return {"summary": response.content, "messages": delete_messages} ``` + ::: + :::js + Prompting and orchestration logic can be used to summarize the message history. For example, in LangGraph you can extend the [`MessagesZodState`](../../concepts/low_level.md#working-with-messages-in-graph-state) to include a `summary` key: + ```typescript + import { MessagesZodState } from "@langchain/langgraph"; + import { z } from "zod"; + + const State = MessagesZodState.merge(z.object({ + summary: z.string().optional(), + })); + ``` + + Then, you can generate a summary of the chat history, using any existing summary as context for the next summary. This `summarizeConversation` node can be called after some number of messages have accumulated in the `messages` state key. + + ```typescript + import { RemoveMessage, HumanMessage } from "@langchain/core/messages"; + + const summarizeConversation = async (state: z.infer<typeof State>) => { + // First, we get any existing summary + const summary = state.summary || ""; + + // Create our summarization prompt + let summaryMessage: string; + if (summary) { + // A summary already exists + summaryMessage = + `This is a summary of the conversation to date: ${summary}\n\n` + + "Extend the summary by taking into account the new messages above:"; + } else { + summaryMessage = "Create a summary of the conversation above:"; + } + + // Add prompt to our history + const messages = [ + ...state.messages, + new HumanMessage({ content: summaryMessage }) + ]; + const response = await model.invoke(messages); + + // Delete all but the 2 most recent messages + const deleteMessages = state.messages + .slice(0, -2) + .map(m => new RemoveMessage({ id: m.id })); + + return { + summary: response.content, + messages: deleteMessages + }; + }; + ``` + ::: ??? example "Full example: summarize messages" + :::python ```python from typing import Any, TypedDict - + from langchain.chat_models import init_chat_model from langchain_core.messages import AnyMessage from langchain_core.messages.utils import count_tokens_approximately @@ -1448,18 +2272,18 @@ The problem with trimming or removing messages, as shown above, is that you may from langgraph.checkpoint.memory import InMemorySaver # highlight-next-line from langmem.short_term import SummarizationNode, RunningSummary - + model = init_chat_model("anthropic:claude-3-7-sonnet-latest") summarization_model = model.bind(max_tokens=128) - + class State(MessagesState): # highlight-next-line context: dict[str, RunningSummary] # (1)! - + class LLMInputState(TypedDict): # (2)! summarized_messages: list[AnyMessage] context: dict[str, RunningSummary] - + # highlight-next-line summarization_node = SummarizationNode( token_counter=count_tokens_approximately, @@ -1473,7 +2297,7 @@ The problem with trimming or removing messages, as shown above, is that you may def call_model(state: LLMInputState): # (3)! response = model.invoke(state["summarized_messages"]) return {"messages": [response]} - + checkpointer = InMemorySaver() builder = StateGraph(State) builder.add_node(call_model) @@ -1482,7 +2306,7 @@ The problem with trimming or removing messages, as shown above, is that you may builder.add_edge(START, "summarize") builder.add_edge("summarize", "call_model") graph = builder.compile(checkpointer=checkpointer) - + # Invoke the graph config = {"configurable": {"thread_id": "1"}} graph.invoke({"messages": "hi, my name is bob"}, config) @@ -1504,11 +2328,127 @@ The problem with trimming or removing messages, as shown above, is that you may ================================== Ai Message ================================== From our conversation, I can see that you introduced yourself as Bob. That's the name you shared with me when we began talking. - + Summary: In this conversation, I was introduced to Bob, who then asked me to write a poem about cats. I composed a poem titled "The Mystery of Cats" that captured cats' graceful movements, independent nature, and their special relationship with humans. Bob then requested a similar poem about dogs, so I wrote "The Joy of Dogs," which highlighted dogs' loyalty, enthusiasm, and loving companionship. Both poems were written in a similar style but emphasized the distinct characteristics that make each pet special. ``` + ::: + :::js + ```typescript + import { ChatAnthropic } from "@langchain/anthropic"; + import { + SystemMessage, + HumanMessage, + RemoveMessage, + type BaseMessage + } from "@langchain/core/messages"; + import { + MessagesZodState, + StateGraph, + START, + END, + MemorySaver, + } from "@langchain/langgraph"; + import { z } from "zod"; + import { v4 as uuidv4 } from "uuid"; + const memory = new MemorySaver(); + + // We will add a `summary` attribute (in addition to `messages` key, + // which MessagesZodState already has) + const GraphState = z.object({ + messages: MessagesZodState.shape.messages, + summary: z.string().default(""), + }); + + // We will use this model for both the conversation and the summarization + const model = new ChatAnthropic({ model: "claude-3-haiku-20240307" }); + + // Define the logic to call the model + const callModel = async (state: z.infer<typeof GraphState>) => { + // If a summary exists, we add this in as a system message + const { summary } = state; + let { messages } = state; + if (summary) { + const systemMessage = new SystemMessage({ + id: uuidv4(), + content: `Summary of conversation earlier: ${summary}`, + }); + messages = [systemMessage, ...messages]; + } + const response = await model.invoke(messages); + // We return an object, because this will get added to the existing state + return { messages: [response] }; + }; + + // We now define the logic for determining whether to end or summarize the conversation + const shouldContinue = (state: z.infer<typeof GraphState>) => { + const messages = state.messages; + // If there are more than six messages, then we summarize the conversation + if (messages.length > 6) { + return "summarize_conversation"; + } + // Otherwise we can just end + return END; + }; + + const summarizeConversation = async (state: z.infer<typeof GraphState>) => { + // First, we summarize the conversation + const { summary, messages } = state; + let summaryMessage: string; + if (summary) { + // If a summary already exists, we use a different system prompt + // to summarize it than if one didn't + summaryMessage = + `This is summary of the conversation to date: ${summary}\n\n` + + "Extend the summary by taking into account the new messages above:"; + } else { + summaryMessage = "Create a summary of the conversation above:"; + } + + const allMessages = [ + ...messages, + new HumanMessage({ id: uuidv4(), content: summaryMessage }), + ]; + + const response = await model.invoke(allMessages); + + // We now need to delete messages that we no longer want to show up + // I will delete all but the last two messages, but you can change this + const deleteMessages = messages + .slice(0, -2) + .map((m) => new RemoveMessage({ id: m.id! })); + + if (typeof response.content !== "string") { + throw new Error("Expected a string response from the model"); + } + + return { summary: response.content, messages: deleteMessages }; + }; + + // Define a new graph + const workflow = new StateGraph(GraphState) + // Define the conversation node and the summarize node + .addNode("conversation", callModel) + .addNode("summarize_conversation", summarizeConversation) + // Set the entrypoint as conversation + .addEdge(START, "conversation") + // We now add a conditional edge + .addConditionalEdges( + // First, we define the start node. We use `conversation`. + // This means these are the edges taken after the `conversation` node is called. + "conversation", + // Next, we pass in the function that will determine which node is called next. + shouldContinue, + ) + // We now add a normal edge from `summarize_conversation` to END. + // This means that after `summarize_conversation` is called, we end. + .addEdge("summarize_conversation", END); + + // Finally, we compile it! + const app = workflow.compile({ checkpointer: memory }); + ``` + ::: ### Manage checkpoints @@ -1516,6 +2456,7 @@ You can view and delete the information stored by the checkpointer. #### View thread state (checkpoint) +:::python === "Graph/Functional API" ```python @@ -1527,7 +2468,7 @@ You can view and delete the information stored by the checkpointer. # otherwise the latest checkpoint is shown # highlight-next-line # "checkpoint_id": "1f029ca3-1f5b-6704-8004-820c16b69a5a" - + } } # highlight-next-line @@ -1536,7 +2477,7 @@ You can view and delete the information stored by the checkpointer. ``` StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today?), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, next=(), + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today?), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, next=(), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, metadata={ 'source': 'loop', @@ -1546,7 +2487,7 @@ You can view and delete the information stored by the checkpointer. 'thread_id': '1' }, created_at='2025-05-05T16:01:24.680462+00:00', - parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, + parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, tasks=(), interrupts=() ) @@ -1563,7 +2504,7 @@ You can view and delete the information stored by the checkpointer. # otherwise the latest checkpoint is shown # highlight-next-line # "checkpoint_id": "1f029ca3-1f5b-6704-8004-820c16b69a5a" - + } } # highlight-next-line @@ -1592,8 +2533,46 @@ You can view and delete the information stored by the checkpointer. ) ``` +::: + +:::js + +```typescript +const config = { + configurable: { + thread_id: "1", + // optionally provide an ID for a specific checkpoint, + // otherwise the latest checkpoint is shown + // checkpoint_id: "1f029ca3-1f5b-6704-8004-820c16b69a5a" + }, +}; +await graph.getState(config); +``` + +``` +{ + values: { messages: [HumanMessage(...), AIMessage(...), HumanMessage(...), AIMessage(...)] }, + next: [], + config: { configurable: { thread_id: '1', checkpoint_ns: '', checkpoint_id: '1f029ca3-1f5b-6704-8004-820c16b69a5a' } }, + metadata: { + source: 'loop', + writes: { call_model: { messages: AIMessage(...) } }, + step: 4, + parents: {}, + thread_id: '1' + }, + createdAt: '2025-05-05T16:01:24.680462+00:00', + parentConfig: { configurable: { thread_id: '1', checkpoint_ns: '', checkpoint_id: '1f029ca3-1790-6b0a-8003-baf965b6a38f' } }, + tasks: [], + interrupts: [] +} +``` + +::: + #### View the history of the thread (checkpoints) +:::python === "Graph/Functional API" ```python @@ -1610,9 +2589,9 @@ You can view and delete the information stored by the checkpointer. ``` [ StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, - next=(), - config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, + next=(), + config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Your name is Bob.')}}, 'step': 4, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:24.680462+00:00', parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, @@ -1620,8 +2599,8 @@ You can view and delete the information stored by the checkpointer. interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?")]}, - next=('call_model',), + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?")]}, + next=('call_model',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, metadata={'source': 'loop', 'writes': None, 'step': 3, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:23.863421+00:00', @@ -1630,9 +2609,9 @@ You can view and delete the information stored by the checkpointer. interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, - next=('__start__',), - config={...}, + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, + next=('__start__',), + config={...}, metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "what's my name?"}]}}, 'step': 2, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:23.863173+00:00', parent_config={...} @@ -1640,9 +2619,9 @@ You can view and delete the information stored by the checkpointer. interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, - next=(), - config={...}, + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, + next=(), + config={...}, metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}}, 'step': 1, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:23.862295+00:00', parent_config={...} @@ -1650,26 +2629,26 @@ You can view and delete the information stored by the checkpointer. interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob")]}, - next=('call_model',), - config={...}, - metadata={'source': 'loop', 'writes': None, 'step': 0, 'parents': {}, 'thread_id': '1'}, - created_at='2025-05-05T16:01:22.278960+00:00', + values={'messages': [HumanMessage(content="hi! I'm bob")]}, + next=('call_model',), + config={...}, + metadata={'source': 'loop', 'writes': None, 'step': 0, 'parents': {}, 'thread_id': '1'}, + created_at='2025-05-05T16:01:22.278960+00:00', parent_config={...} - tasks=(PregelTask(id='8cbd75e0-3720-b056-04f7-71ac805140a0', name='call_model', path=('__pregel_pull', 'call_model'), error=None, interrupts=(), state=None, result={'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}),), + tasks=(PregelTask(id='8cbd75e0-3720-b056-04f7-71ac805140a0', name='call_model', path=('__pregel_pull', 'call_model'), error=None, interrupts=(), state=None, result={'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}),), interrupts=() ), StateSnapshot( - values={'messages': []}, - next=('__start__',), + values={'messages': []}, + next=('__start__',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-0870-6ce2-bfff-1f3f14c3e565'}}, - metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}}, 'step': -1, 'parents': {}, 'thread_id': '1'}, - created_at='2025-05-05T16:01:22.277497+00:00', + metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}}, 'step': -1, 'parents': {}, 'thread_id': '1'}, + created_at='2025-05-05T16:01:22.277497+00:00', parent_config=None, - tasks=(PregelTask(id='d458367b-8265-812c-18e2-33001d199ce6', name='__start__', path=('__pregel_pull', '__start__'), error=None, interrupts=(), state=None, result={'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}),), + tasks=(PregelTask(id='d458367b-8265-812c-18e2-33001d199ce6', name='__start__', path=('__pregel_pull', '__start__'), error=None, interrupts=(), state=None, result={'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}),), interrupts=() ) - ] + ] ``` === "Checkpointer API" @@ -1688,100 +2667,134 @@ You can view and delete the information stored by the checkpointer. ``` [ CheckpointTuple( - config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, + config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, checkpoint={ - 'v': 3, - 'ts': '2025-05-05T16:01:24.680462+00:00', - 'id': '1f029ca3-1f5b-6704-8004-820c16b69a5a', - 'channel_versions': {'__start__': '00000000000000000000000000000005.0.5290678567601859', 'messages': '00000000000000000000000000000006.0.3205149138784782', 'branch:to:call_model': '00000000000000000000000000000006.0.14611156755133758'}, + 'v': 3, + 'ts': '2025-05-05T16:01:24.680462+00:00', + 'id': '1f029ca3-1f5b-6704-8004-820c16b69a5a', + 'channel_versions': {'__start__': '00000000000000000000000000000005.0.5290678567601859', 'messages': '00000000000000000000000000000006.0.3205149138784782', 'branch:to:call_model': '00000000000000000000000000000006.0.14611156755133758'}, 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000004.0.5736472536395331'}, 'call_model': {'branch:to:call_model': '00000000000000000000000000000005.0.1410174088651449'}}, 'channel_values': {'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, }, - metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Your name is Bob.')}}, 'step': 4, 'parents': {}, 'thread_id': '1'}, - parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, + metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Your name is Bob.')}}, 'step': 4, 'parents': {}, 'thread_id': '1'}, + parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, pending_writes=[] ), CheckpointTuple( config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, checkpoint={ - 'v': 3, - 'ts': '2025-05-05T16:01:23.863421+00:00', - 'id': '1f029ca3-1790-6b0a-8003-baf965b6a38f', - 'channel_versions': {'__start__': '00000000000000000000000000000005.0.5290678567601859', 'messages': '00000000000000000000000000000006.0.3205149138784782', 'branch:to:call_model': '00000000000000000000000000000006.0.14611156755133758'}, + 'v': 3, + 'ts': '2025-05-05T16:01:23.863421+00:00', + 'id': '1f029ca3-1790-6b0a-8003-baf965b6a38f', + 'channel_versions': {'__start__': '00000000000000000000000000000005.0.5290678567601859', 'messages': '00000000000000000000000000000006.0.3205149138784782', 'branch:to:call_model': '00000000000000000000000000000006.0.14611156755133758'}, 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000004.0.5736472536395331'}, 'call_model': {'branch:to:call_model': '00000000000000000000000000000005.0.1410174088651449'}}, 'channel_values': {'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?")], 'branch:to:call_model': None} - }, - metadata={'source': 'loop', 'writes': None, 'step': 3, 'parents': {}, 'thread_id': '1'}, - parent_config={...}, + }, + metadata={'source': 'loop', 'writes': None, 'step': 3, 'parents': {}, 'thread_id': '1'}, + parent_config={...}, pending_writes=[('8ab4155e-6b15-b885-9ce5-bed69a2c305c', 'messages', AIMessage(content='Your name is Bob.'))] ), CheckpointTuple( - config={...}, + config={...}, checkpoint={ - 'v': 3, - 'ts': '2025-05-05T16:01:23.863173+00:00', - 'id': '1f029ca3-1790-616e-8002-9e021694a0cd', - 'channel_versions': {'__start__': '00000000000000000000000000000004.0.5736472536395331', 'messages': '00000000000000000000000000000003.0.7056767754077798', 'branch:to:call_model': '00000000000000000000000000000003.0.22059023329132854'}, - 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}, 'call_model': {'branch:to:call_model': '00000000000000000000000000000002.0.9300422176788571'}}, + 'v': 3, + 'ts': '2025-05-05T16:01:23.863173+00:00', + 'id': '1f029ca3-1790-616e-8002-9e021694a0cd', + 'channel_versions': {'__start__': '00000000000000000000000000000004.0.5736472536395331', 'messages': '00000000000000000000000000000003.0.7056767754077798', 'branch:to:call_model': '00000000000000000000000000000003.0.22059023329132854'}, + 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}, 'call_model': {'branch:to:call_model': '00000000000000000000000000000002.0.9300422176788571'}}, 'channel_values': {'__start__': {'messages': [{'role': 'user', 'content': "what's my name?"}]}, 'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]} - }, - metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "what's my name?"}]}}, 'step': 2, 'parents': {}, 'thread_id': '1'}, - parent_config={...}, + }, + metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "what's my name?"}]}}, 'step': 2, 'parents': {}, 'thread_id': '1'}, + parent_config={...}, pending_writes=[('24ba39d6-6db1-4c9b-f4c5-682aeaf38dcd', 'messages', [{'role': 'user', 'content': "what's my name?"}]), ('24ba39d6-6db1-4c9b-f4c5-682aeaf38dcd', 'branch:to:call_model', None)] ), CheckpointTuple( - config={...}, + config={...}, checkpoint={ - 'v': 3, - 'ts': '2025-05-05T16:01:23.862295+00:00', - 'id': '1f029ca3-178d-6f54-8001-d7b180db0c89', - 'channel_versions': {'__start__': '00000000000000000000000000000002.0.18673090920108737', 'messages': '00000000000000000000000000000003.0.7056767754077798', 'branch:to:call_model': '00000000000000000000000000000003.0.22059023329132854'}, - 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}, 'call_model': {'branch:to:call_model': '00000000000000000000000000000002.0.9300422176788571'}}, + 'v': 3, + 'ts': '2025-05-05T16:01:23.862295+00:00', + 'id': '1f029ca3-178d-6f54-8001-d7b180db0c89', + 'channel_versions': {'__start__': '00000000000000000000000000000002.0.18673090920108737', 'messages': '00000000000000000000000000000003.0.7056767754077798', 'branch:to:call_model': '00000000000000000000000000000003.0.22059023329132854'}, + 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}, 'call_model': {'branch:to:call_model': '00000000000000000000000000000002.0.9300422176788571'}}, 'channel_values': {'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]} - }, - metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}}, 'step': 1, 'parents': {}, 'thread_id': '1'}, - parent_config={...}, + }, + metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}}, 'step': 1, 'parents': {}, 'thread_id': '1'}, + parent_config={...}, pending_writes=[] ), CheckpointTuple( - config={...}, + config={...}, checkpoint={ - 'v': 3, - 'ts': '2025-05-05T16:01:22.278960+00:00', - 'id': '1f029ca3-0874-6612-8000-339f2abc83b1', - 'channel_versions': {'__start__': '00000000000000000000000000000002.0.18673090920108737', 'messages': '00000000000000000000000000000002.0.30296526818059655', 'branch:to:call_model': '00000000000000000000000000000002.0.9300422176788571'}, - 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}}, + 'v': 3, + 'ts': '2025-05-05T16:01:22.278960+00:00', + 'id': '1f029ca3-0874-6612-8000-339f2abc83b1', + 'channel_versions': {'__start__': '00000000000000000000000000000002.0.18673090920108737', 'messages': '00000000000000000000000000000002.0.30296526818059655', 'branch:to:call_model': '00000000000000000000000000000002.0.9300422176788571'}, + 'versions_seen': {'__input__': {}, '__start__': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}}, 'channel_values': {'messages': [HumanMessage(content="hi! I'm bob")], 'branch:to:call_model': None} - }, - metadata={'source': 'loop', 'writes': None, 'step': 0, 'parents': {}, 'thread_id': '1'}, - parent_config={...}, + }, + metadata={'source': 'loop', 'writes': None, 'step': 0, 'parents': {}, 'thread_id': '1'}, + parent_config={...}, pending_writes=[('8cbd75e0-3720-b056-04f7-71ac805140a0', 'messages', AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'))] ), CheckpointTuple( - config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-0870-6ce2-bfff-1f3f14c3e565'}}, + config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-0870-6ce2-bfff-1f3f14c3e565'}}, checkpoint={ - 'v': 3, - 'ts': '2025-05-05T16:01:22.277497+00:00', - 'id': '1f029ca3-0870-6ce2-bfff-1f3f14c3e565', - 'channel_versions': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}, - 'versions_seen': {'__input__': {}}, + 'v': 3, + 'ts': '2025-05-05T16:01:22.277497+00:00', + 'id': '1f029ca3-0870-6ce2-bfff-1f3f14c3e565', + 'channel_versions': {'__start__': '00000000000000000000000000000001.0.7040775356287469'}, + 'versions_seen': {'__input__': {}}, 'channel_values': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}} - }, - metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}}, 'step': -1, 'parents': {}, 'thread_id': '1'}, - parent_config=None, + }, + metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}}, 'step': -1, 'parents': {}, 'thread_id': '1'}, + parent_config=None, pending_writes=[('d458367b-8265-812c-18e2-33001d199ce6', 'messages', [{'role': 'user', 'content': "hi! I'm bob"}]), ('d458367b-8265-812c-18e2-33001d199ce6', 'branch:to:call_model', None)] ) ] ``` +::: + +:::js + +```typescript +const config = { + configurable: { + thread_id: "1", + }, +}; + +const history = []; +for await (const state of graph.getStateHistory(config)) { + history.push(state); +} +``` + +::: #### Delete all checkpoints for a thread +:::python + ```python thread_id = "1" checkpointer.delete_thread(thread_id) ``` +::: + +:::js + +```typescript +const threadId = "1"; +await checkpointer.deleteThread(threadId); +``` + +::: + +:::python + ## Prebuilt memory tools -**LangMem** is a LangChain-maintained library that offers tools for managing long-term memories in your agent. See the [LangMem documentation](https://langchain-ai.github.io/langmem/) for usage examples. \ No newline at end of file +**LangMem** is a LangChain-maintained library that offers tools for managing long-term memories in your agent. See the [LangMem documentation](https://langchain-ai.github.io/langmem/) for usage examples. +::: diff --git a/docs/docs/how-tos/multi_agent.md b/docs/docs/how-tos/multi_agent.md index eca36301c..bc29502cc 100644 --- a/docs/docs/how-tos/multi_agent.md +++ b/docs/docs/how-tos/multi_agent.md @@ -57,7 +57,7 @@ def create_handoff_tool(*, agent_name: str, description: str | None = None): return handoff_tool ``` -1. Access the [state](../concepts/low_level.md#state) of the agent that is calling the handoff tool using the [InjectedState][langgraph.prebuilt.InjectedState] annotation. +1. Access the [state](../concepts/low_level.md#state) of the agent that is calling the handoff tool using the @[InjectedState][InjectedState] annotation. 2. The `Command` primitive allows specifying a state update and a node transition as a single operation, making it useful for implementing handoffs. 3. Name of the agent or node to hand off to. 4. Take the agent's messages and **add** them to the parent's **state** as part of the handoff. The next agent will see the parent state. @@ -65,7 +65,7 @@ def create_handoff_tool(*, agent_name: str, description: str | None = None): !!! tip - If you want to use tools that return `Command`, you can either use prebuilt [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] / [`ToolNode`][langgraph.prebuilt.tool_node.ToolNode] components, or implement your own tool-executing node that collects `Command` objects returned by the tools and returns a list of them, e.g.: + If you want to use tools that return `Command`, you can either use prebuilt @[`create_react_agent`][create_react_agent] / @[`ToolNode`][ToolNode] components, or implement your own tool-executing node that collects `Command` objects returned by the tools and returns a list of them, e.g.: ```python def call_tools(state): @@ -92,7 +92,7 @@ def create_handoff_tool(*, agent_name: str, description: str | None = None): ### Control agent inputs -You can use the [`Send()`][langgraph.types.Send] primitive to directly send data to the worker agents during the handoff. For example, you can request that the calling agent populate a task description for the next agent: +You can use the @[`Send()`][Send] primitive to directly send data to the worker agents during the handoff. For example, you can request that the calling agent populate a task description for the next agent: ```python @@ -130,7 +130,7 @@ def create_task_description_handoff_tool( return handoff_tool ``` -See the multi-agent [supervisor](../tutorials/multi_agent/agent_supervisor.md#4-create-delegation-tasks) example for a full example of using [`Send()`][langgraph.types.Send] in handoffs. +See the multi-agent [supervisor](../tutorials/multi_agent/agent_supervisor.md#4-create-delegation-tasks) example for a full example of using @[`Send()`][Send] in handoffs. ## Build a multi-agent system @@ -326,7 +326,7 @@ multi_agent_graph = ( ## Multi-turn conversation -Users might want to engage in a *multi-turn conversation* with one or more agents. To build a system that can handle this, you can create a node that uses an [`interrupt`][langgraph.types.interrupt] to collect user input and routes back to the **active** agent. +Users might want to engage in a *multi-turn conversation* with one or more agents. To build a system that can handle this, you can create a node that uses an @[`interrupt`][interrupt] to collect user input and routes back to the **active** agent. The agents can then be implemented as nodes in a graph that executes agent steps and determines the next action: diff --git a/docs/docs/how-tos/streaming.md b/docs/docs/how-tos/streaming.md index bf25419c4..20a61a8fc 100644 --- a/docs/docs/how-tos/streaming.md +++ b/docs/docs/how-tos/streaming.md @@ -4,28 +4,41 @@ You can [stream outputs](../concepts/streaming.md) from a LangGraph agent or wor ## Supported stream modes -Pass one or more of the following stream modes as a list to the [`stream()`][langgraph.graph.state.CompiledStateGraph.stream] or [`astream()`][langgraph.graph.state.CompiledStateGraph.astream] methods: +:::python +Pass one or more of the following stream modes as a list to the @[`stream()`][CompiledStateGraph.stream] or @[`astream()`][CompiledStateGraph.astream] methods: +::: -| Mode | Description | -|------|-------------| -| `values` | Streams the full value of the state after each step of the graph. | -| `updates` | Streams the updates to the state after each step of the graph. If multiple updates are made in the same step (e.g., multiple nodes are run), those updates are streamed separately. | -| `custom` | Streams custom data from inside your graph nodes. | -| `messages` | Streams 2-tuples (LLM token, metadata) from any graph nodes where an LLM is invoked. | -| `debug` | Streams as much information as possible throughout the execution of the graph. +:::js +Pass one or more of the following stream modes as a list to the @[`stream()`][CompiledStateGraph.stream] method: +::: + +| Mode | Description | +| ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `values` | Streams the full value of the state after each step of the graph. | +| `updates` | Streams the updates to the state after each step of the graph. If multiple updates are made in the same step (e.g., multiple nodes are run), those updates are streamed separately. | +| `custom` | Streams custom data from inside your graph nodes. | +| `messages` | Streams 2-tuples (LLM token, metadata) from any graph nodes where an LLM is invoked. | +| `debug` | Streams as much information as possible throughout the execution of the graph. | ## Stream from an agent ### Agent progress -To stream agent progress, use the [`stream()`][langgraph.graph.state.CompiledStateGraph.stream] or [`astream()`][langgraph.graph.state.CompiledStateGraph.astream] methods with `stream_mode="updates"`. This emits an event after every agent step. +:::python +To stream agent progress, use the @[`stream()`][CompiledStateGraph.stream] or @[`astream()`][CompiledStateGraph.astream] methods with `stream_mode="updates"`. This emits an event after every agent step. +::: + +:::js +To stream agent progress, use the @[`stream()`][CompiledStateGraph.stream] method with `streamMode: "updates"`. This emits an event after every agent step. +::: For example, if you have an agent that calls a tool once, you should see the following updates: -* **LLM node**: AI message with tool call requests -* **Tool node**: Tool message with execution result -* **LLM node**: Final AI response +- **LLM node**: AI message with tool call requests +- **Tool node**: Tool message with execution result +- **LLM node**: Final AI response +:::python === "Sync" ```python @@ -60,8 +73,30 @@ For example, if you have an agent that calls a tool once, you should see the fol print("\n") ``` +::: + +:::js + +```typescript +const agent = createReactAgent({ + llm: model, + tools: [getWeather], +}); + +for await (const chunk of await agent.stream( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + { streamMode: "updates" } +)) { + console.log(chunk); + console.log("\n"); +} +``` + +::: + ### LLM tokens +:::python To stream tokens as they are produced by the LLM, use `stream_mode="messages"`: === "Sync" @@ -100,9 +135,33 @@ To stream tokens as they are produced by the LLM, use `stream_mode="messages"`: print("\n") ``` +::: + +:::js +To stream tokens as they are produced by the LLM, use `streamMode: "messages"`: + +```typescript +const agent = createReactAgent({ + llm: model, + tools: [getWeather], +}); + +for await (const [token, metadata] of await agent.stream( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + { streamMode: "messages" } +)) { + console.log("Token", token); + console.log("Metadata", metadata); + console.log("\n"); +} +``` + +::: + ### Tool updates -To stream updates from tools as they are executed, you can use [get_stream_writer][langgraph.config.get_stream_writer]. +:::python +To stream updates from tools as they are executed, you can use @[get_stream_writer][get_stream_writer]. === "Sync" @@ -163,10 +222,53 @@ To stream updates from tools as they are executed, you can use [get_stream_write ``` !!! Note - If you add `get_stream_writer` inside your tool, you won't be able to invoke the tool outside of a LangGraph execution context. + + If you add `get_stream_writer` inside your tool, you won't be able to invoke the tool outside of a LangGraph execution context. + +::: + +:::js +To stream updates from tools as they are executed, you can use the `writer` parameter from the configuration. + +```typescript +import { LangGraphRunnableConfig } from "@langchain/langgraph"; + +const getWeather = tool( + async (input, config: LangGraphRunnableConfig) => { + // Stream any arbitrary data + config.writer?.("Looking up data for city: " + input.city); + return `It's always sunny in ${input.city}!`; + }, + { + name: "get_weather", + description: "Get weather for a given city.", + schema: z.object({ + city: z.string().describe("The city to get weather for."), + }), + } +); + +const agent = createReactAgent({ + llm: model, + tools: [getWeather], +}); + +for await (const chunk of await agent.stream( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + { streamMode: "custom" } +)) { + console.log(chunk); + console.log("\n"); +} +``` + +!!! Note + If you add the `writer` parameter to your tool, you won't be able to invoke the tool outside of a LangGraph execution context without providing a writer function. +::: ### Stream multiple modes +:::python You can specify multiple streaming modes by passing stream mode as a list: `stream_mode=["updates", "messages", "custom"]`: === "Sync" @@ -203,17 +305,40 @@ You can specify multiple streaming modes by passing stream mode as a list: `stre print("\n") ``` +::: + +:::js +You can specify multiple streaming modes by passing streamMode as an array: `streamMode: ["updates", "messages", "custom"]`: + +```typescript +const agent = createReactAgent({ + llm: model, + tools: [getWeather], +}); + +for await (const chunk of await agent.stream( + { messages: [{ role: "user", content: "what is the weather in sf" }] }, + { streamMode: ["updates", "messages", "custom"] } +)) { + console.log(chunk); + console.log("\n"); +} +``` + +::: + ### Disable streaming In some applications you might need to disable streaming of individual tokens for a given model. This is useful in [multi-agent](../agents/multi-agent.md) systems to control which agents stream their output. See the [Models](../agents/models.md#disable-streaming) guide to learn how to disable streaming. -## Stream from a workflow +## Stream from a workflow ### Basic usage example -LangGraph graphs expose the [`.stream()`][langgraph.pregel.Pregel.stream] (sync) and [`.astream()`][langgraph.pregel.Pregel.astream] (async) methods to yield streamed outputs as iterators. +:::python +LangGraph graphs expose the @[`.stream()`][Pregel.stream] (sync) and @[`.astream()`][Pregel.astream] (async) methods to yield streamed outputs as iterators. === "Sync" @@ -229,8 +354,24 @@ LangGraph graphs expose the [`.stream()`][langgraph.pregel.Pregel.stream] (sync) print(chunk) ``` +::: + +:::js +LangGraph graphs expose the @[`.stream()`][Pregel.stream] method to yield streamed outputs as iterators. + +```typescript +for await (const chunk of await graph.stream(inputs, { + streamMode: "updates", +})) { + console.log(chunk); +} +``` + +::: + ??? example "Extended example: streaming updates" + :::python ```python from typing import TypedDict from langgraph.graph import StateGraph, START, END @@ -266,14 +407,49 @@ LangGraph graphs expose the [`.stream()`][langgraph.pregel.Pregel.stream] (sync) 1. The `stream()` method returns an iterator that yields streamed outputs. 2. Set `stream_mode="updates"` to stream only the updates to the graph state after each node. Other stream modes are also available. See [supported stream modes](#supported-stream-modes) for details. + ::: + + :::js + ```typescript + import { StateGraph, START, END } from "@langchain/langgraph"; + import { z } from "zod"; + + const State = z.object({ + topic: z.string(), + joke: z.string(), + }); + + const graph = new StateGraph(State) + .addNode("refineTopic", (state) => { + return { topic: state.topic + " and cats" }; + }) + .addNode("generateJoke", (state) => { + return { joke: `This is a joke about ${state.topic}` }; + }) + .addEdge(START, "refineTopic") + .addEdge("refineTopic", "generateJoke") + .addEdge("generateJoke", END) + .compile(); + + for await (const chunk of await graph.stream( + { topic: "ice cream" }, + { streamMode: "updates" } // (1)! + )) { + console.log(chunk); + } + ``` + + 1. Set `streamMode: "updates"` to stream only the updates to the graph state after each node. Other stream modes are also available. See [supported stream modes](#supported-stream-modes) for details. + ::: ```output - {'refine_topic': {'topic': 'ice cream and cats'}} - {'generate_joke': {'joke': 'This is a joke about ice cream and cats'}} + {'refineTopic': {'topic': 'ice cream and cats'}} + {'generateJoke': {'joke': 'This is a joke about ice cream and cats'}} ``` | ### Stream multiple modes +:::python You can pass a list as the `stream_mode` parameter to stream multiple modes at once. The streamed outputs will be tuples of `(mode, chunk)` where `mode` is the name of the stream mode and `chunk` is the data streamed by that mode. @@ -292,12 +468,31 @@ The streamed outputs will be tuples of `(mode, chunk)` where `mode` is the name print(chunk) ``` +::: + +:::js +You can pass an array as the `streamMode` parameter to stream multiple modes at once. + +The streamed outputs will be tuples of `[mode, chunk]` where `mode` is the name of the stream mode and `chunk` is the data streamed by that mode. + +```typescript +for await (const [mode, chunk] of await graph.stream(inputs, { + streamMode: ["updates", "custom"], +})) { + console.log(chunk); +} +``` + +::: + ### Stream graph state Use the stream modes `updates` and `values` to stream the state of the graph as it executes. -* `updates` streams the **updates** to the state after each step of the graph. -* `values` streams the **full value** of the state after each step of the graph. +- `updates` streams the **updates** to the state after each step of the graph. +- `values` streams the **full value** of the state after each step of the graph. + +:::python ```python from typing import TypedDict @@ -327,11 +522,39 @@ graph = ( ) ``` +::: + +:::js + +```typescript +import { StateGraph, START, END } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ + topic: z.string(), + joke: z.string(), +}); + +const graph = new StateGraph(State) + .addNode("refineTopic", (state) => { + return { topic: state.topic + " and cats" }; + }) + .addNode("generateJoke", (state) => { + return { joke: `This is a joke about ${state.topic}` }; + }) + .addEdge(START, "refineTopic") + .addEdge("refineTopic", "generateJoke") + .addEdge("generateJoke", END) + .compile(); +``` + +::: + === "updates" Use this to stream only the **state updates** returned by the nodes after each step. The streamed outputs include the name of the node as well as the update. - + :::python ```python for chunk in graph.stream( {"topic": "ice cream"}, @@ -340,11 +563,24 @@ graph = ( ): print(chunk) ``` + ::: -=== "values" + :::js + ```typescript + for await (const chunk of await graph.stream( + { topic: "ice cream" }, + { streamMode: "updates" } + )) { + console.log(chunk); + } + ``` + ::: + +=== "values" Use this to stream the **full state** of the graph after each step. + :::python ```python for chunk in graph.stream( {"topic": "ice cream"}, @@ -353,10 +589,22 @@ graph = ( ): print(chunk) ``` + ::: + :::js + ```typescript + for await (const chunk of await graph.stream( + { topic: "ice cream" }, + { streamMode: "values" } + )) { + console.log(chunk); + } + ``` + ::: ### Stream subgraph outputs +:::python To include outputs from [subgraphs](../concepts/subgraphs.md) in the streamed outputs, you can set `subgraphs=True` in the `.stream()` method of the parent graph. This will stream outputs from both the parent graph and any subgraphs. The outputs will be streamed as tuples `(namespace, data)`, where `namespace` is a tuple with the path to the node where a subgraph is invoked, e.g. `("parent_node:<task_id>", "child_node:<task_id>")`. @@ -372,9 +620,31 @@ for chunk in graph.stream( ``` 1. Set `subgraphs=True` to stream outputs from subgraphs. + ::: + +:::js +To include outputs from [subgraphs](../concepts/subgraphs.md) in the streamed outputs, you can set `subgraphs: true` in the `.stream()` method of the parent graph. This will stream outputs from both the parent graph and any subgraphs. + +The outputs will be streamed as tuples `[namespace, data]`, where `namespace` is a tuple with the path to the node where a subgraph is invoked, e.g. `["parent_node:<task_id>", "child_node:<task_id>"]`. + +```typescript +for await (const chunk of await graph.stream( + { foo: "foo" }, + { + subgraphs: true, // (1)! + streamMode: "updates", + } +)) { + console.log(chunk); +} +``` + +1. Set `subgraphs: true` to stream outputs from subgraphs. + ::: ??? example "Extended example: streaming from subgraphs" + :::python ```python from langgraph.graph import START, StateGraph from typing import TypedDict @@ -419,15 +689,77 @@ for chunk in graph.stream( ): print(chunk) ``` - - 1. Set `subgraphs=True` to stream outputs from subgraphs. + 1. Set `subgraphs=True` to stream outputs from subgraphs. + ::: + + :::js + ```typescript + import { StateGraph, START } from "@langchain/langgraph"; + import { z } from "zod"; + + // Define subgraph + const SubgraphState = z.object({ + foo: z.string(), // note that this key is shared with the parent graph state + bar: z.string(), + }); + + const subgraphBuilder = new StateGraph(SubgraphState) + .addNode("subgraphNode1", (state) => { + return { bar: "bar" }; + }) + .addNode("subgraphNode2", (state) => { + return { foo: state.foo + state.bar }; + }) + .addEdge(START, "subgraphNode1") + .addEdge("subgraphNode1", "subgraphNode2"); + const subgraph = subgraphBuilder.compile(); + + // Define parent graph + const ParentState = z.object({ + foo: z.string(), + }); + + const builder = new StateGraph(ParentState) + .addNode("node1", (state) => { + return { foo: "hi! " + state.foo }; + }) + .addNode("node2", subgraph) + .addEdge(START, "node1") + .addEdge("node1", "node2"); + const graph = builder.compile(); + + for await (const chunk of await graph.stream( + { foo: "foo" }, + { + streamMode: "updates", + subgraphs: true, // (1)! + } + )) { + console.log(chunk); + } + ``` + + 1. Set `subgraphs: true` to stream outputs from subgraphs. + ::: + + :::python ``` ((), {'node_1': {'foo': 'hi! foo'}}) (('node_2:dfddc4ba-c3c5-6887-5012-a243b5b377c2',), {'subgraph_node_1': {'bar': 'bar'}}) (('node_2:dfddc4ba-c3c5-6887-5012-a243b5b377c2',), {'subgraph_node_2': {'foo': 'hi! foobar'}}) ((), {'node_2': {'foo': 'hi! foobar'}}) ``` + ::: + + :::js + ``` + [[], {'node1': {'foo': 'hi! foo'}}] + [['node2:dfddc4ba-c3c5-6887-5012-a243b5b377c2'], {'subgraphNode1': {'bar': 'bar'}}] + [['node2:dfddc4ba-c3c5-6887-5012-a243b5b377c2'], {'subgraphNode2': {'foo': 'hi! foobar'}}] + [[], {'node2': {'foo': 'hi! foobar'}}] + ``` + ::: **Note** that we are receiving not just the node updates, but we also the namespaces which tell us what graph (or subgraph) we are streaming from. @@ -435,6 +767,8 @@ for chunk in graph.stream( Use the `debug` streaming mode to stream as much information as possible throughout the execution of the graph. The streamed outputs include the name of the node as well as the full state. +:::python + ```python for chunk in graph.stream( {"topic": "ice cream"}, @@ -444,18 +778,33 @@ for chunk in graph.stream( print(chunk) ``` +::: + +:::js + +```typescript +for await (const chunk of await graph.stream( + { topic: "ice cream" }, + { streamMode: "debug" } +)) { + console.log(chunk); +} +``` + +::: ### LLM tokens {#messages} Use the `messages` streaming mode to stream Large Language Model (LLM) outputs **token by token** from any part of your graph, including nodes, tools, subgraphs, or tasks. +:::python The streamed output from [`messages` mode](#supported-stream-modes) is a tuple `(message_chunk, metadata)` where: - `message_chunk`: the token or message segment from the LLM. - `metadata`: a dictionary containing details about the graph node and LLM invocation. > If your LLM is not available as a LangChain integration, you can stream its outputs using `custom` mode instead. See [use with any LLM](#use-with-any-llm) for details. - + !!! warning "Manual config required for async in Python < 3.11" When using Python < 3.11 with async code, you must explicitly pass `RunnableConfig` to `ainvoke()` to enable proper streaming. See [Async with Python < 3.11](#async) for details or upgrade to Python 3.11+. @@ -503,12 +852,62 @@ for message_chunk, metadata in graph.stream( # (2)! 1. Note that the message events are emitted even when the LLM is run using `.invoke` rather than `.stream`. 2. The "messages" stream mode returns an iterator of tuples `(message_chunk, metadata)` where `message_chunk` is the token streamed by the LLM and `metadata` is a dictionary with information about the graph node where the LLM was called and other information. + ::: +:::js +The streamed output from [`messages` mode](#supported-stream-modes) is a tuple `[message_chunk, metadata]` where: + +- `message_chunk`: the token or message segment from the LLM. +- `metadata`: a dictionary containing details about the graph node and LLM invocation. + +> If your LLM is not available as a LangChain integration, you can stream its outputs using `custom` mode instead. See [use with any LLM](#use-with-any-llm) for details. + +```typescript +import { ChatOpenAI } from "@langchain/openai"; +import { StateGraph, START } from "@langchain/langgraph"; +import { z } from "zod"; + +const MyState = z.object({ + topic: z.string(), + joke: z.string().default(""), +}); + +const llm = new ChatOpenAI({ model: "gpt-4o-mini" }); + +const callModel = async (state: z.infer<typeof MyState>) => { + // Call the LLM to generate a joke about a topic + const llmResponse = await llm.invoke([ + { role: "user", content: `Generate a joke about ${state.topic}` }, + ]); // (1)! + return { joke: llmResponse.content }; +}; + +const graph = new StateGraph(MyState) + .addNode("callModel", callModel) + .addEdge(START, "callModel") + .compile(); + +for await (const [messageChunk, metadata] of await graph.stream( + // (2)! + { topic: "ice cream" }, + { streamMode: "messages" } +)) { + if (messageChunk.content) { + console.log(messageChunk.content + "|"); + } +} +``` + +1. Note that the message events are emitted even when the LLM is run using `.invoke` rather than `.stream`. +2. The "messages" stream mode returns an iterator of tuples `[messageChunk, metadata]` where `messageChunk` is the token streamed by the LLM and `metadata` is a dictionary with information about the graph node where the LLM was called and other information. + ::: #### Filter by LLM invocation You can associate `tags` with LLM invocations to filter the streamed tokens by LLM invocation. +:::python + ```python from langchain.chat_models import init_chat_model @@ -530,10 +929,43 @@ async for msg, metadata in graph.astream( # (3)! 2. llm_2 is tagged with "poem". 3. The `stream_mode` is set to "messages" to stream LLM tokens. The `metadata` contains information about the LLM invocation, including the tags. 4. Filter the streamed tokens by the `tags` field in the metadata to only include the tokens from the LLM invocation with the "joke" tag. + ::: +:::js + +```typescript +import { ChatOpenAI } from "@langchain/openai"; + +const llm1 = new ChatOpenAI({ + model: "gpt-4o-mini", + tags: ['joke'] // (1)! +}); +const llm2 = new ChatOpenAI({ + model: "gpt-4o-mini", + tags: ['poem'] // (2)! +}); + +const graph = // ... define a graph that uses these LLMs + +for await (const [msg, metadata] of await graph.stream( // (3)! + { topic: "cats" }, + { streamMode: "messages" } +)) { + if (metadata.tags?.includes("joke")) { // (4)! + console.log(msg.content + "|"); + } +} +``` + +1. llm1 is tagged with "joke". +2. llm2 is tagged with "poem". +3. The `streamMode` is set to "messages" to stream LLM tokens. The `metadata` contains information about the LLM invocation, including the tags. +4. Filter the streamed tokens by the `tags` field in the metadata to only include the tokens from the LLM invocation with the "joke" tag. + ::: ??? example "Extended example: filtering by tags" + :::python ```python from typing import TypedDict @@ -587,12 +1019,73 @@ async for msg, metadata in graph.astream( # (3)! 2. The `poem_model` is tagged with "poem". 3. The `config` is passed through explicitly to ensure the context vars are propagated correctly. This is required for Python < 3.11 when using async code. Please see the [async section](#async) for more details. 4. The `stream_mode` is set to "messages" to stream LLM tokens. The `metadata` contains information about the LLM invocation, including the tags. + ::: + :::js + ```typescript + import { ChatOpenAI } from "@langchain/openai"; + import { StateGraph, START } from "@langchain/langgraph"; + import { z } from "zod"; + + const jokeModel = new ChatOpenAI({ + model: "gpt-4o-mini", + tags: ["joke"] // (1)! + }); + const poemModel = new ChatOpenAI({ + model: "gpt-4o-mini", + tags: ["poem"] // (2)! + }); + + const State = z.object({ + topic: z.string(), + joke: z.string(), + poem: z.string(), + }); + + const graph = new StateGraph(State) + .addNode("callModel", (state) => { + const topic = state.topic; + console.log("Writing joke..."); + + const jokeResponse = await jokeModel.invoke([ + { role: "user", content: `Write a joke about ${topic}` } + ]); + + console.log("\n\nWriting poem..."); + const poemResponse = await poemModel.invoke([ + { role: "user", content: `Write a short poem about ${topic}` } + ]); + + return { + joke: jokeResponse.content, + poem: poemResponse.content + }; + }) + .addEdge(START, "callModel") + .compile(); + + for await (const [msg, metadata] of await graph.stream( + { topic: "cats" }, + { streamMode: "messages" } // (3)! + )) { + if (metadata.tags?.includes("joke")) { // (4)! + console.log(msg.content + "|"); + } + } + ``` + + 1. The `jokeModel` is tagged with "joke". + 2. The `poemModel` is tagged with "poem". + 3. The `streamMode` is set to "messages" to stream LLM tokens. The `metadata` contains information about the LLM invocation, including the tags. + 4. Filter the streamed tokens by the `tags` field in the metadata to only include the tokens from the LLM invocation with the "joke" tag. + ::: #### Filter by node To stream tokens only from specific nodes, use `stream_mode="messages"` and filter the outputs by the `langgraph_node` field in the streamed metadata: +:::python + ```python for msg, metadata in graph.stream( # (1)! inputs, @@ -606,12 +1099,33 @@ for msg, metadata in graph.stream( # (1)! 1. The "messages" stream mode returns a tuple of `(message_chunk, metadata)` where `message_chunk` is the token streamed by the LLM and `metadata` is a dictionary with information about the graph node where the LLM was called and other information. 2. Filter the streamed tokens by the `langgraph_node` field in the metadata to only include the tokens from the `write_poem` node. + ::: + +:::js + +```typescript +for await (const [msg, metadata] of await graph.stream( + // (1)! + inputs, + { streamMode: "messages" } +)) { + if (msg.content && metadata.langgraph_node === "some_node_name") { + // (2)! + // ... + } +} +``` + +1. The "messages" stream mode returns a tuple of `[messageChunk, metadata]` where `messageChunk` is the token streamed by the LLM and `metadata` is a dictionary with information about the graph node where the LLM was called and other information. +2. Filter the streamed tokens by the `langgraph_node` field in the metadata to only include the tokens from the `writePoem` node. + ::: ??? example "Extended example: streaming LLM tokens from specific nodes" + :::python ```python from typing import TypedDict - from langgraph.graph import START, StateGraph + from langgraph.graph import START, StateGraph from langchain_openai import ChatOpenAI model = ChatOpenAI(model="gpt-4o-mini") @@ -661,9 +1175,59 @@ for msg, metadata in graph.stream( # (1)! 1. The "messages" stream mode returns a tuple of `(message_chunk, metadata)` where `message_chunk` is the token streamed by the LLM and `metadata` is a dictionary with information about the graph node where the LLM was called and other information. 2. Filter the streamed tokens by the `langgraph_node` field in the metadata to only include the tokens from the `write_poem` node. + ::: + + :::js + ```typescript + import { ChatOpenAI } from "@langchain/openai"; + import { StateGraph, START } from "@langchain/langgraph"; + import { z } from "zod"; + + const model = new ChatOpenAI({ model: "gpt-4o-mini" }); + + const State = z.object({ + topic: z.string(), + joke: z.string(), + poem: z.string(), + }); + + const graph = new StateGraph(State) + .addNode("writeJoke", async (state) => { + const topic = state.topic; + const jokeResponse = await model.invoke([ + { role: "user", content: `Write a joke about ${topic}` } + ]); + return { joke: jokeResponse.content }; + }) + .addNode("writePoem", async (state) => { + const topic = state.topic; + const poemResponse = await model.invoke([ + { role: "user", content: `Write a short poem about ${topic}` } + ]); + return { poem: poemResponse.content }; + }) + // write both the joke and the poem concurrently + .addEdge(START, "writeJoke") + .addEdge(START, "writePoem") + .compile(); + + for await (const [msg, metadata] of await graph.stream( // (1)! + { topic: "cats" }, + { streamMode: "messages" } + )) { + if (msg.content && metadata.langgraph_node === "writePoem") { // (2)! + console.log(msg.content + "|"); + } + } + ``` + + 1. The "messages" stream mode returns a tuple of `[messageChunk, metadata]` where `messageChunk` is the token streamed by the LLM and `metadata` is a dictionary with information about the graph node where the LLM was called and other information. + 2. Filter the streamed tokens by the `langgraph_node` field in the metadata to only include the tokens from the `writePoem` node. + ::: ### Stream custom data +:::python To send **custom user-defined data** from inside a LangGraph node or tool, follow these steps: 1. Use `get_stream_writer()` to access the stream writer and emit custom data. @@ -671,11 +1235,10 @@ To send **custom user-defined data** from inside a LangGraph node or tool, follo !!! warning "No `get_stream_writer()` in async for Python < 3.11" - In async code running on Python < 3.11, `get_stream_writer()` will not work. - Instead, add a `writer` parameter to your node or tool and pass it manually. + In async code running on Python < 3.11, `get_stream_writer()` will not work. + Instead, add a `writer` parameter to your node or tool and pass it manually. See [Async with Python < 3.11](#async) for usage examples. - === "node" ```python @@ -725,7 +1288,7 @@ To send **custom user-defined data** from inside a LangGraph node or tool, follo # perform query # highlight-next-line writer({"data": "Retrieved 100/100 records", "type": "progress"}) # (3)! - return "some-answer" + return "some-answer" graph = ... # define a graph that uses this tool @@ -739,9 +1302,84 @@ To send **custom user-defined data** from inside a LangGraph node or tool, follo 3. Emit another custom key-value pair. 4. Set `stream_mode="custom"` to receive the custom data in the stream. +::: + +:::js +To send **custom user-defined data** from inside a LangGraph node or tool, follow these steps: + +1. Use the `writer` parameter from the `LangGraphRunnableConfig` to emit custom data. +2. Set `streamMode: "custom"` when calling `.stream()` to get the custom data in the stream. You can combine multiple modes (e.g., `["updates", "custom"]`), but at least one must be `"custom"`. + +=== "node" + + ```typescript + import { StateGraph, START, LangGraphRunnableConfig } from "@langchain/langgraph"; + import { z } from "zod"; + + const State = z.object({ + query: z.string(), + answer: z.string(), + }); + + const graph = new StateGraph(State) + .addNode("node", async (state, config) => { + config.writer({ custom_key: "Generating custom data inside node" }); // (1)! + return { answer: "some data" }; + }) + .addEdge(START, "node") + .compile(); + + const inputs = { query: "example" }; + + // Usage + for await (const chunk of await graph.stream(inputs, { streamMode: "custom" })) { // (2)! + console.log(chunk); + } + ``` + + 1. Use the writer to emit a custom key-value pair (e.g., progress update). + 2. Set `streamMode: "custom"` to receive the custom data in the stream. + +=== "tool" + + ```typescript + import { tool } from "@langchain/core/tools"; + import { LangGraphRunnableConfig } from "@langchain/langgraph"; + import { z } from "zod"; + + const queryDatabase = tool( + async (input, config: LangGraphRunnableConfig) => { + config.writer({ data: "Retrieved 0/100 records", type: "progress" }); // (1)! + // perform query + config.writer({ data: "Retrieved 100/100 records", type: "progress" }); // (2)! + return "some-answer"; + }, + { + name: "query_database", + description: "Query the database.", + schema: z.object({ + query: z.string().describe("The query to execute."), + }), + } + ); + + const graph = // ... define a graph that uses this tool + + for await (const chunk of await graph.stream(inputs, { streamMode: "custom" })) { // (3)! + console.log(chunk); + } + ``` + + 1. Use the writer to emit a custom key-value pair (e.g., progress update). + 2. Emit another custom key-value pair. + 3. Set `streamMode: "custom"` to receive the custom data in the stream. + +::: + ### Use with any LLM -You can use `stream_mode="custom"` to stream data from **any LLM API** — even if that API does **not** implement the LangChain chat model interface. +:::python +You can use `stream_mode="custom"` to stream data from **any LLM API** — even if that API does **not** implement the LangChain chat model interface. This lets you integrate raw LLM clients or external services that provide their own streaming interfaces, making LangGraph highly flexible for custom setups. @@ -778,9 +1416,51 @@ for chunk in graph.stream( 2. Generate LLM tokens using your custom streaming client. 3. Use the writer to send custom data to the stream. 4. Set `stream_mode="custom"` to receive the custom data in the stream. + ::: +:::js +You can use `streamMode: "custom"` to stream data from **any LLM API** — even if that API does **not** implement the LangChain chat model interface. + +This lets you integrate raw LLM clients or external services that provide their own streaming interfaces, making LangGraph highly flexible for custom setups. + +```typescript +import { LangGraphRunnableConfig } from "@langchain/langgraph"; + +const callArbitraryModel = async ( + state: any, + config: LangGraphRunnableConfig +) => { + // Example node that calls an arbitrary model and streams the output + // Assume you have a streaming client that yields chunks + for await (const chunk of yourCustomStreamingClient(state.topic)) { + // (1)! + config.writer({ custom_llm_chunk: chunk }); // (2)! + } + return { result: "completed" }; +}; + +const graph = new StateGraph(State) + .addNode("callArbitraryModel", callArbitraryModel) + // Add other nodes and edges as needed + .compile(); + +for await (const chunk of await graph.stream( + { topic: "cats" }, + { streamMode: "custom" } // (3)! +)) { + // The chunk will contain the custom data streamed from the llm + console.log(chunk); +} +``` + +1. Generate LLM tokens using your custom streaming client. +2. Use the writer to send custom data to the stream. +3. Set `streamMode: "custom"` to receive the custom data in the stream. + ::: ??? example "Extended example: streaming arbitrary chat model" + + :::python ```python import operator import json @@ -862,7 +1542,7 @@ for chunk in graph.stream( graph = ( - StateGraph(State) + StateGraph(State) .add_node(call_tool) .add_edge(START, "call_tool") .compile() @@ -897,17 +1577,137 @@ for chunk in graph.stream( ): print(chunk["content"], end="|", flush=True) ``` + ::: + :::js + ```typescript + import { StateGraph, START, LangGraphRunnableConfig } from "@langchain/langgraph"; + import { z } from "zod"; + import OpenAI from "openai"; + + const openaiClient = new OpenAI(); + const modelName = "gpt-4o-mini"; + + async function* streamTokens(modelName: string, messages: any[]) { + const response = await openaiClient.chat.completions.create({ + messages, + model: modelName, + stream: true, + }); + + let role: string | null = null; + for await (const chunk of response) { + const delta = chunk.choices[0]?.delta; + + if (delta?.role) { + role = delta.role; + } + + if (delta?.content) { + yield { role, content: delta.content }; + } + } + } + + // this is our tool + const getItems = tool( + async (input, config: LangGraphRunnableConfig) => { + let response = ""; + for await (const msgChunk of streamTokens( + modelName, + [ + { + role: "user", + content: `Can you tell me what kind of items i might find in the following place: '${input.place}'. List at least 3 such items separating them by a comma. And include a brief description of each item.`, + }, + ] + )) { + response += msgChunk.content; + config.writer?.(msgChunk); + } + return response; + }, + { + name: "get_items", + description: "Use this tool to list items one might find in a place you're asked about.", + schema: z.object({ + place: z.string().describe("The place to look up items for."), + }), + } + ); + + const State = z.object({ + messages: z.array(z.any()), + }); + + const graph = new StateGraph(State) + // this is the tool-calling graph node + .addNode("callTool", async (state) => { + const aiMessage = state.messages.at(-1); + const toolCall = aiMessage.tool_calls?.at(-1); + + const functionName = toolCall?.function?.name; + if (functionName !== "get_items") { + throw new Error(`Tool ${functionName} not supported`); + } + + const functionArguments = toolCall?.function?.arguments; + const args = JSON.parse(functionArguments); + + const functionResponse = await getItems.invoke(args); + const toolMessage = { + tool_call_id: toolCall.id, + role: "tool", + name: functionName, + content: functionResponse, + }; + return { messages: [toolMessage] }; + }) + .addEdge(START, "callTool") + .compile(); + ``` + + Let's invoke the graph with an AI message that includes a tool call: + + ```typescript + const inputs = { + messages: [ + { + content: null, + role: "assistant", + tool_calls: [ + { + id: "1", + function: { + arguments: '{"place":"bedroom"}', + name: "get_items", + }, + type: "function", + } + ], + } + ] + }; + + for await (const chunk of await graph.stream( + inputs, + { streamMode: "custom" } + )) { + console.log(chunk.content + "|"); + } + ``` + ::: ### Disable streaming for specific chat models -If your application mixes models that support streaming with those that do not, you may need to explicitly disable streaming for +If your application mixes models that support streaming with those that do not, you may need to explicitly disable streaming for models that do not support it. +:::python Set `disable_streaming=True` when initializing the model. === "init_chat_model" - + ```python from langchain.chat_models import init_chat_model @@ -930,11 +1730,28 @@ Set `disable_streaming=True` when initializing the model. 1. Set `disable_streaming=True` to disable streaming for the chat model. +::: + +:::js +Set `streaming: false` when initializing the model. + +```typescript +import { ChatOpenAI } from "@langchain/openai"; + +const model = new ChatOpenAI({ + model: "o1-preview", + streaming: false, // (1)! +}); +``` + +::: + +:::python ### Async with Python < 3.11 { #async } In Python versions < 3.11, [asyncio tasks](https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task) do not support the `context` parameter. -This limits LangGraph ability to automatically propagate context, and affects LangGraph’s streaming mechanisms in two key ways: +This limits LangGraph ability to automatically propagate context, and affects LangGraph's streaming mechanisms in two key ways: 1. You **must** explicitly pass [`RunnableConfig`](https://python.langchain.com/docs/concepts/runnables/#runnableconfig) into async LLM calls (e.g., `ainvoke()`), as callbacks are not automatically propagated. 2. You **cannot** use `get_stream_writer()` in async nodes or tools — you must pass a `writer` argument directly. @@ -979,7 +1796,7 @@ This limits LangGraph ability to automatically propagate context, and affects La ``` 1. Accept `config` as an argument in the async node function. - 2. Pass `config` to `llm.ainvoke()` to ensure proper context propagation. + 2. Pass `config` to `llm.ainvoke()` to ensure proper context propagation. 3. Set `stream_mode="messages"` to stream LLM tokens. ??? example "Extended example: async custom streaming with stream writer" @@ -1014,3 +1831,5 @@ This limits LangGraph ability to automatically propagate context, and affects La 1. Add `writer` as an argument in the function signature of the async node or tool. LangGraph will automatically pass the stream writer to the function. 2. Set `stream_mode="custom"` to receive the custom data in the stream. + +::: diff --git a/docs/docs/how-tos/tool-calling.md b/docs/docs/how-tos/tool-calling.md index 9968b0036..d8f6e29e5 100644 --- a/docs/docs/how-tos/tool-calling.md +++ b/docs/docs/how-tos/tool-calling.md @@ -1,11 +1,12 @@ # Call tools -[Tools](../concepts/tools.md) encapsulate a callable function and its input schema. These can be passed to compatible [chat models](https://python.langchain.com/docs/concepts/chat_models), allowing the model to decide whether to invoke a tool and determine the appropriate arguments. +[Tools](../concepts/tools.md) encapsulate a callable function and its input schema. These can be passed to compatible chat models, allowing the model to decide whether to invoke a tool and determine the appropriate arguments. You can [define your own tools](#define-a-tool) or use [prebuilt tools](#prebuilt-tools) ## Define a tool +:::python Define a basic tool with the [@tool](https://python.langchain.com/api_reference/core/tools/langchain_core.tools.convert.tool.html) decorator: ```python @@ -18,16 +19,57 @@ def multiply(a: int, b: int) -> int: return a * b ``` +::: + +:::js +Define a basic tool with the [tool](https://js.langchain.com/docs/api/core/tools/classes/tool.html) function: + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +// highlight-next-line +const multiply = tool( + (input) => { + return input.a * input.b; + }, + { + name: "multiply", + description: "Multiply two numbers.", + schema: z.object({ + a: z.number().describe("First operand"), + b: z.number().describe("Second operand"), + }), + } +); +``` + +::: + ## Run a tool Tools conform to the [Runnable interface](https://python.langchain.com/docs/concepts/runnables/), which means you can run a tool using the `invoke` method: +:::python + ```python multiply.invoke({"a": 6, "b": 7}) # returns 42 ``` +::: + +:::js + +```typescript +await multiply.invoke({ a: 6, b: 7 }); // returns 42 +``` + +::: + If the tool is invoked with `type="tool_call"`, it will return a [ToolMessage](https://python.langchain.com/docs/concepts/messages/#toolmessage): +:::python + ```python tool_call = { "type": "tool_call", @@ -43,10 +85,36 @@ Output: ToolMessage(content='294', name='multiply', tool_call_id='1') ``` +::: + +:::js + +```typescript +const toolCall = { + type: "tool_call", + id: "1", + name: "multiply", + args: { a: 42, b: 7 }, +}; +await multiply.invoke(toolCall); // returns a ToolMessage object +``` + +Output: + +``` +ToolMessage { + content: "294", + name: "multiply", + tool_call_id: "1" +} +``` + +::: ## Use in an agent -To create a tool-calling agent, you can use the prebuilt [create_react_agent][langgraph.prebuilt.chat_agent_executor.create_react_agent]: +:::python +To create a tool-calling agent, you can use the prebuilt @[create_react_agent][create_react_agent]: ```python from langchain_core.tools import tool @@ -66,6 +134,44 @@ agent = create_react_agent( agent.invoke({"messages": [{"role": "user", "content": "what's 42 x 7?"}]}) ``` +::: + +:::js +To create a tool-calling agent, you can use the prebuilt [createReactAgent](https://js.langchain.com/docs/api/langgraph_prebuilt/functions/createReactAgent.html): + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +// highlight-next-line +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +const multiply = tool( + (input) => { + return input.a * input.b; + }, + { + name: "multiply", + description: "Multiply two numbers.", + schema: z.object({ + a: z.number().describe("First operand"), + b: z.number().describe("Second operand"), + }), + } +); + +// highlight-next-line +const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-sonnet-20240620" }), + tools: [multiply], +}); + +await agent.invoke({ + messages: [{ role: "user", content: "what's 42 x 7?" }], +}); +``` + +::: + ## Use in a workflow If you are writing a custom workflow, you will need to: @@ -73,7 +179,8 @@ If you are writing a custom workflow, you will need to: 1. register the tools with the chat model 2. call the tool if the model decides to use it -Use `model.bind_tools()` to register the tools with the model. +:::python +Use `model.bind_tools()` to register the tools with the model. ```python from langchain.chat_models import init_chat_model @@ -84,10 +191,27 @@ model = init_chat_model(model="claude-3-5-haiku-latest") model_with_tools = model.bind_tools([multiply]) ``` +::: + +:::js +Use `model.bindTools()` to register the tools with the model. + +```typescript +import { ChatOpenAI } from "@langchain/openai"; + +const model = new ChatOpenAI({ model: "gpt-4o" }); + +// highlight-next-line +const modelWithTools = model.bindTools([multiply]); +``` + +::: + LLMs automatically determine if a tool invocation is necessary and handle calling the tool with the appropriate arguments. ??? example "Extended example: attach tools to a chat model" + :::python ```python from langchain_core.tools import tool from langchain.chat_models import init_chat_model @@ -114,21 +238,62 @@ LLMs automatically determine if a tool invocation is necessary and handle callin tool_call_id='toolu_0176DV4YKSD8FndkeuuLj36c' ) ``` + ::: + + :::js + ```typescript + import { tool } from "@langchain/core/tools"; + import { ChatOpenAI } from "@langchain/openai"; + import { z } from "zod"; + + const multiply = tool( + (input) => { + return input.a * input.b; + }, + { + name: "multiply", + description: "Multiply two numbers.", + schema: z.object({ + a: z.number().describe("First operand"), + b: z.number().describe("Second operand"), + }), + } + ); + + const model = new ChatOpenAI({ model: "gpt-4o" }); + // highlight-next-line + const modelWithTools = model.bindTools([multiply]); + + const responseMessage = await modelWithTools.invoke("what's 42 x 7?"); + const toolCall = responseMessage.tool_calls[0]; + + await multiply.invoke(toolCall); + ``` + + ``` + ToolMessage { + content: "294", + name: "multiply", + tool_call_id: "toolu_0176DV4YKSD8FndkeuuLj36c" + } + ``` + ::: + #### ToolNode -To execute tools in custom workflows, use the prebuilt [`ToolNode`][langgraph.prebuilt.tool_node.ToolNode] or implement your own custom node. +:::python +To execute tools in custom workflows, use the prebuilt @[`ToolNode`][ToolNode] or implement your own custom node. `ToolNode` is a specialized node for executing tools in a workflow. It provides the following features: -* Supports both synchronous and asynchronous tools. -* Executes multiple tools concurrently. -* Handles errors during tool execution (`handle_tool_errors=True`, enabled by default). See [handling tool errors](#handle-errors) for more details. +- Supports both synchronous and asynchronous tools. +- Executes multiple tools concurrently. +- Handles errors during tool execution (`handle_tool_errors=True`, enabled by default). See [handling tool errors](#handle-errors) for more details. `ToolNode` operates on [`MessagesState`](../concepts/low_level.md#messagesstate): -* **Input**: `MessagesState`, where the last message is an `AIMessage` containing the `tool_calls` parameter. -* **Output**: `MessagesState` updated with the resulting [`ToolMessage`](https://python.langchain.com/docs/concepts/messages/#toolmessage) from executed tools. - +- **Input**: `MessagesState`, where the last message is an `AIMessage` containing the `tool_calls` parameter. +- **Output**: `MessagesState` updated with the resulting [`ToolMessage`](https://python.langchain.com/docs/concepts/messages/#toolmessage) from executed tools. ```python # highlight-next-line @@ -150,12 +315,68 @@ tool_node = ToolNode([get_weather, get_coolest_cities]) tool_node.invoke({"messages": [...]}) ``` +::: + +:::js +To execute tools in custom workflows, use the prebuilt [`ToolNode`](https://js.langchain.com/docs/api/langgraph_prebuilt/classes/ToolNode.html) or implement your own custom node. + +`ToolNode` is a specialized node for executing tools in a workflow. It provides the following features: + +- Supports both synchronous and asynchronous tools. +- Executes multiple tools concurrently. +- Handles errors during tool execution (`handleToolErrors: true`, enabled by default). See [handling tool errors](#handle-errors) for more details. + +- **Input**: `MessagesZodState`, where the last message is an `AIMessage` containing the `tool_calls` parameter. +- **Output**: `MessagesZodState` updated with the resulting [`ToolMessage`](https://js.langchain.com/docs/concepts/messages/#toolmessage) from executed tools. + +```typescript +// highlight-next-line +import { ToolNode } from "@langchain/langgraph/prebuilt"; + +const getWeather = tool( + (input) => { + if (["sf", "san francisco"].includes(input.location.toLowerCase())) { + return "It's 60 degrees and foggy."; + } else { + return "It's 90 degrees and sunny."; + } + }, + { + name: "get_weather", + description: "Call to get the current weather.", + schema: z.object({ + location: z.string().describe("Location to get the weather for."), + }), + } +); + +const getCoolestCities = tool( + () => { + return "nyc, sf"; + }, + { + name: "get_coolest_cities", + description: "Get a list of coolest cities", + schema: z.object({ + noOp: z.string().optional().describe("No-op parameter."), + }), + } +); + +// highlight-next-line +const toolNode = new ToolNode([getWeather, getCoolestCities]); +await toolNode.invoke({ messages: [...] }); +``` + +::: + ??? example "Single tool call" + :::python ```python from langchain_core.messages import AIMessage from langgraph.prebuilt import ToolNode - + # Define tools @tool def get_weather(location: str): @@ -164,10 +385,10 @@ tool_node.invoke({"messages": [...]}) return "It's 60 degrees and foggy." else: return "It's 90 degrees and sunny." - + # highlight-next-line tool_node = ToolNode([get_weather]) - + message_with_single_tool_call = AIMessage( content="", tool_calls=[ @@ -179,33 +400,83 @@ tool_node.invoke({"messages": [...]}) } ], ) - + tool_node.invoke({"messages": [message_with_single_tool_call]}) ``` - + ``` {'messages': [ToolMessage(content="It's 60 degrees and foggy.", name='get_weather', tool_call_id='tool_call_id')]} ``` + ::: + + :::js + ```typescript + import { AIMessage } from "@langchain/core/messages"; + import { ToolNode } from "@langchain/langgraph/prebuilt"; + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + + // Define tools + const getWeather = tool( + (input) => { + if (["sf", "san francisco"].includes(input.location.toLowerCase())) { + return "It's 60 degrees and foggy."; + } else { + return "It's 90 degrees and sunny."; + } + }, + { + name: "get_weather", + description: "Call to get the current weather.", + schema: z.object({ + location: z.string().describe("Location to get the weather for."), + }), + } + ); + + // highlight-next-line + const toolNode = new ToolNode([getWeather]); + + const messageWithSingleToolCall = new AIMessage({ + content: "", + tool_calls: [ + { + name: "get_weather", + args: { location: "sf" }, + id: "tool_call_id", + type: "tool_call", + } + ], + }); + + await toolNode.invoke({ messages: [messageWithSingleToolCall] }); + ``` + + ``` + { messages: [ToolMessage { content: "It's 60 degrees and foggy.", name: "get_weather", tool_call_id: "tool_call_id" }] } + ``` + ::: ??? example "Multiple tool calls" + :::python ```python from langchain_core.messages import AIMessage from langgraph.prebuilt import ToolNode - + # Define tools - + def get_weather(location: str): """Call to get the current weather.""" if location.lower() in ["sf", "san francisco"]: return "It's 60 degrees and foggy." else: return "It's 90 degrees and sunny." - + def get_coolest_cities(): """Get a list of coolest cities""" return "nyc, sf" - + # highlight-next-line tool_node = ToolNode([get_weather, get_coolest_cities]) @@ -241,30 +512,105 @@ tool_node.invoke({"messages": [...]}) ] } ``` + ::: + :::js + ```typescript + import { AIMessage } from "@langchain/core/messages"; + import { ToolNode } from "@langchain/langgraph/prebuilt"; + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + // Define tools + const getWeather = tool( + (input) => { + if (["sf", "san francisco"].includes(input.location.toLowerCase())) { + return "It's 60 degrees and foggy."; + } else { + return "It's 90 degrees and sunny."; + } + }, + { + name: "get_weather", + description: "Call to get the current weather.", + schema: z.object({ + location: z.string().describe("Location to get the weather for."), + }), + } + ); + + const getCoolestCities = tool( + () => { + return "nyc, sf"; + }, + { + name: "get_coolest_cities", + description: "Get a list of coolest cities", + schema: z.object({ + noOp: z.string().optional().describe("No-op parameter."), + }), + } + ); + + // highlight-next-line + const toolNode = new ToolNode([getWeather, getCoolestCities]); + + const messageWithMultipleToolCalls = new AIMessage({ + content: "", + tool_calls: [ + { + name: "get_coolest_cities", + args: {}, + id: "tool_call_id_1", + type: "tool_call", + }, + { + name: "get_weather", + args: { location: "sf" }, + id: "tool_call_id_2", + type: "tool_call", + }, + ], + }); + + // highlight-next-line + await toolNode.invoke({ messages: [messageWithMultipleToolCalls] }); // (1)! + ``` + + 1. `ToolNode` will execute both tools in parallel + + ``` + { + messages: [ + ToolMessage { content: "nyc, sf", name: "get_coolest_cities", tool_call_id: "tool_call_id_1" }, + ToolMessage { content: "It's 60 degrees and foggy.", name: "get_weather", tool_call_id: "tool_call_id_2" } + ] + } + ``` + ::: ??? example "Use with a chat model" + :::python ```python from langchain.chat_models import init_chat_model from langgraph.prebuilt import ToolNode - + def get_weather(location: str): """Call to get the current weather.""" if location.lower() in ["sf", "san francisco"]: return "It's 60 degrees and foggy." else: return "It's 90 degrees and sunny." - + # highlight-next-line tool_node = ToolNode([get_weather]) - + model = init_chat_model(model="claude-3-5-haiku-latest") # highlight-next-line model_with_tools = model.bind_tools([get_weather]) # (1)! - - + + # highlight-next-line response_message = model_with_tools.invoke("what's the weather in sf?") tool_node.invoke({"messages": [response_message]}) @@ -275,58 +621,103 @@ tool_node.invoke({"messages": [...]}) ``` {'messages': [ToolMessage(content="It's 60 degrees and foggy.", name='get_weather', tool_call_id='toolu_01Pnkgw5JeTRxXAU7tyHT4UW')]} ``` + ::: + + :::js + ```typescript + import { ChatOpenAI } from "@langchain/openai"; + import { ToolNode } from "@langchain/langgraph/prebuilt"; + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + + const getWeather = tool( + (input) => { + if (["sf", "san francisco"].includes(input.location.toLowerCase())) { + return "It's 60 degrees and foggy."; + } else { + return "It's 90 degrees and sunny."; + } + }, + { + name: "get_weather", + description: "Call to get the current weather.", + schema: z.object({ + location: z.string().describe("Location to get the weather for."), + }), + } + ); + + // highlight-next-line + const toolNode = new ToolNode([getWeather]); + + const model = new ChatOpenAI({ model: "gpt-4o" }); + // highlight-next-line + const modelWithTools = model.bindTools([getWeather]); // (1)! + + // highlight-next-line + const responseMessage = await modelWithTools.invoke("what's the weather in sf?"); + await toolNode.invoke({ messages: [responseMessage] }); + ``` + + 1. Use `.bindTools()` to attach the tool schema to the chat model + + ``` + { messages: [ToolMessage { content: "It's 60 degrees and foggy.", name: "get_weather", tool_call_id: "toolu_01Pnkgw5JeTRxXAU7tyHT4UW" }] } + ``` + ::: ??? example "Use in a tool-calling agent" This is an example of creating a tool-calling agent from scratch using `ToolNode`. You can also use LangGraph's prebuilt [agent](../agents/agents.md). + :::python ```python from langchain.chat_models import init_chat_model from langgraph.prebuilt import ToolNode from langgraph.graph import StateGraph, MessagesState, START, END - + def get_weather(location: str): """Call to get the current weather.""" if location.lower() in ["sf", "san francisco"]: return "It's 60 degrees and foggy." else: return "It's 90 degrees and sunny." - + # highlight-next-line tool_node = ToolNode([get_weather]) - + model = init_chat_model(model="claude-3-5-haiku-latest") # highlight-next-line model_with_tools = model.bind_tools([get_weather]) - + def should_continue(state: MessagesState): messages = state["messages"] last_message = messages[-1] if last_message.tool_calls: return "tools" return END - + def call_model(state: MessagesState): messages = state["messages"] response = model_with_tools.invoke(messages) return {"messages": [response]} - + builder = StateGraph(MessagesState) - + # Define the two nodes we will cycle between builder.add_node("call_model", call_model) # highlight-next-line builder.add_node("tools", tool_node) - + builder.add_edge(START, "call_model") builder.add_conditional_edges("call_model", should_continue, ["tools", END]) builder.add_edge("tools", "call_model") - + graph = builder.compile() - + graph.invoke({"messages": [{"role": "user", "content": "what's the weather in sf?"}]}) ``` - + ``` { 'messages': [ @@ -340,7 +731,86 @@ tool_node.invoke({"messages": [...]}) ] } ``` + ::: + :::js + ```typescript + import { ChatOpenAI } from "@langchain/openai"; + import { ToolNode } from "@langchain/langgraph/prebuilt"; + import { StateGraph, MessagesZodState, START, END } from "@langchain/langgraph"; + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + import { isAIMessage } from "@langchain/core/messages"; + + const getWeather = tool( + (input) => { + if (["sf", "san francisco"].includes(input.location.toLowerCase())) { + return "It's 60 degrees and foggy."; + } else { + return "It's 90 degrees and sunny."; + } + }, + { + name: "get_weather", + description: "Call to get the current weather.", + schema: z.object({ + location: z.string().describe("Location to get the weather for."), + }), + } + ); + + // highlight-next-line + const toolNode = new ToolNode([getWeather]); + + const model = new ChatOpenAI({ model: "gpt-4o" }); + // highlight-next-line + const modelWithTools = model.bindTools([getWeather]); + + const shouldContinue = (state: z.infer<typeof MessagesZodState>) => { + const messages = state.messages; + const lastMessage = messages.at(-1); + if (lastMessage && isAIMessage(lastMessage) && lastMessage.tool_calls?.length) { + return "tools"; + } + return END; + }; + + const callModel = async (state: z.infer<typeof MessagesZodState>) => { + const messages = state.messages; + const response = await modelWithTools.invoke(messages); + return { messages: [response] }; + }; + + const builder = new StateGraph(MessagesZodState) + // Define the two nodes we will cycle between + .addNode("agent", callModel) + // highlight-next-line + .addNode("tools", toolNode) + .addEdge(START, "agent") + .addConditionalEdges("agent", shouldContinue, ["tools", END]) + .addEdge("tools", "agent"); + + const graph = builder.compile(); + + await graph.invoke({ + messages: [{ role: "user", content: "what's the weather in sf?" }] + }); + ``` + + ``` + { + messages: [ + HumanMessage { content: "what's the weather in sf?" }, + AIMessage { + content: [{ text: "I'll help you check the weather in San Francisco right now.", type: "text" }, { id: "toolu_01A4vwUEgBKxfFVc5H3v1CNs", input: { location: "San Francisco" }, name: "get_weather", type: "tool_use" }], + tool_calls: [{ name: "get_weather", args: { location: "San Francisco" }, id: "toolu_01A4vwUEgBKxfFVc5H3v1CNs", type: "tool_call" }] + }, + ToolMessage { content: "It's 60 degrees and foggy." }, + AIMessage { content: "The current weather in San Francisco is 60 degrees and foggy. Typical San Francisco weather with its famous marine layer!" } + ] + } + ``` + ::: ## Tool customization @@ -348,6 +818,7 @@ For more control over tool behavior, use the `@tool` decorator. ### Parameter descriptions +:::python Auto-generate descriptions from docstrings: ```python @@ -366,8 +837,36 @@ def multiply(a: int, b: int) -> int: return a * b ``` +::: + +:::js +Auto-generate descriptions from schema: + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +// highlight-next-line +const multiply = tool( + (input) => { + return input.a * input.b; + }, + { + name: "multiply", + description: "Multiply two numbers.", + schema: z.object({ + a: z.number().describe("First operand"), + b: z.number().describe("Second operand"), + }), + } +); +``` + +::: + ### Explicit input schema +:::python Define schemas using `args_schema`: ```python @@ -385,9 +884,13 @@ def multiply(a: int, b: int) -> int: return a * b ``` +::: + ### Tool name -Override the default tool name (function name) using the first argument: +Override the default tool name using the first argument or name property: + +:::python ```python from langchain_core.tools import tool @@ -399,18 +902,45 @@ def multiply(a: int, b: int) -> int: return a * b ``` +::: + +:::js + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +// highlight-next-line +const multiply = tool( + (input) => { + return input.a * input.b; + }, + { + name: "multiply_tool", // Custom name + description: "Multiply two numbers.", + schema: z.object({ + a: z.number().describe("First operand"), + b: z.number().describe("Second operand"), + }), + } +); +``` + +::: + ## Context management Tools within LangGraph sometimes require context data, such as runtime-only arguments (e.g., user IDs or session details), that should not be controlled by the model. LangGraph provides three methods for managing such context: | Type | Usage Scenario | Mutable | Lifetime | -|-----------------------------------------|------------------------------------------|---------|--------------------------| -| [Configuration](#configuration) | Static, immutable runtime data | ❌ | Single invocation | -| [Short-term memory](#short-term-memory) | Dynamic, changing data during invocation | ✅ | Single invocation | -| [Long-term memory](#long-term-memory) | Persistent, cross-session data | ✅ | Across multiple sessions | +| --------------------------------------- | ---------------------------------------- | ------- | ------------------------ | +| [Configuration](#configuration) | Static, immutable runtime data | ❌ | Single invocation | +| [Short-term memory](#short-term-memory) | Dynamic, changing data during invocation | ✅ | Single invocation | +| [Long-term memory](#long-term-memory) | Persistent, cross-session data | ✅ | Across multiple sessions | ### Configuration +:::python Use configuration when you have **immutable** runtime data that tools require, such as user identifiers. You pass these arguments via [`RunnableConfig`](https://python.langchain.com/docs/concepts/runnables/#runnableconfig) at invocation and access them in the tool: ```python @@ -432,13 +962,47 @@ agent.invoke( ) ``` +::: + +:::js +Use configuration when you have **immutable** runtime data that tools require, such as user identifiers. You pass these arguments via [`LangGraphRunnableConfig`](https://js.langchain.com/docs/api/langgraph/interfaces/LangGraphRunnableConfig.html) at invocation and access them in the tool: + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import type { LangGraphRunnableConfig } from "@langchain/langgraph"; + +const getUserInfo = tool( + // highlight-next-line + async (_, config: LangGraphRunnableConfig) => { + const userId = config?.configurable?.user_id; + return userId === "user_123" ? "User is John Smith" : "Unknown user"; + }, + { + name: "get_user_info", + description: "Retrieve user information based on user ID.", + schema: z.object({}), + } +); + +// Invocation example with an agent +await agent.invoke( + { messages: [{ role: "user", content: "look up user info" }] }, + // highlight-next-line + { configurable: { user_id: "user_123" } } +); +``` + +::: + ??? example "Extended example: Access config in tools" + :::python ```python from langchain_core.runnables import RunnableConfig from langchain_core.tools import tool from langgraph.prebuilt import create_react_agent - + def get_user_info( # highlight-next-line config: RunnableConfig, @@ -447,24 +1011,61 @@ agent.invoke( # highlight-next-line user_id = config["configurable"].get("user_id") return "User is John Smith" if user_id == "user_123" else "Unknown user" - + agent = create_react_agent( model="anthropic:claude-3-7-sonnet-latest", tools=[get_user_info], ) - + agent.invoke( {"messages": [{"role": "user", "content": "look up user information"}]}, # highlight-next-line config={"configurable": {"user_id": "user_123"}} ) ``` + ::: + + :::js + ```typescript + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import type { LangGraphRunnableConfig } from "@langchain/langgraph"; + import { ChatAnthropic } from "@langchain/anthropic"; + + const getUserInfo = tool( + // highlight-next-line + async (_, config: LangGraphRunnableConfig) => { + // highlight-next-line + const userId = config?.configurable?.user_id; + return userId === "user_123" ? "User is John Smith" : "Unknown user"; + }, + { + name: "get_user_info", + description: "Look up user info.", + schema: z.object({}), + } + ); + + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-sonnet-20240620" }), + tools: [getUserInfo], + }); + + await agent.invoke( + { messages: [{ role: "user", content: "look up user information" }] }, + // highlight-next-line + { configurable: { user_id: "user_123" } } + ); + ``` + ::: ### Short-term memory -Short-term memory maintains **dynamic** state that changes during a single execution. +Short-term memory maintains **dynamic** state that changes during a single execution. -To **access** (read) the graph state inside the tools, you can use a special parameter **annotation** — [`InjectedState`][langgraph.prebuilt.InjectedState]: +:::python +To **access** (read) the graph state inside the tools, you can use a special parameter **annotation** — @[`InjectedState`][InjectedState]: ```python from typing import Annotated, NotRequired @@ -496,6 +1097,38 @@ agent = create_react_agent( agent.invoke({"messages": "what's my name?"}) ``` +::: + +:::js +To **access** (read) the graph state inside the tools, you can use the @[`getContextVariable`][getContextVariable] function: + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import { getContextVariable } from "@langchain/core/context"; +import { MessagesZodState } from "@langchain/langgraph"; +import type { LangGraphRunnableConfig } from "@langchain/langgraph"; + +const getUserName = tool( + // highlight-next-line + async (_, config: LangGraphRunnableConfig) => { + // highlight-next-line + const currentState = getContextVariable("currentState") as z.infer< + typeof MessagesZodState + > & { userName?: string }; + return currentState?.userName || "Unknown user"; + }, + { + name: "get_user_name", + description: "Retrieve the current user name from state.", + schema: z.object({}), + } +); +``` + +::: + +:::python Use a tool that returns a `Command` to **update** `user_name` and append a confirmation message: ```python @@ -524,17 +1157,79 @@ def update_user_name( }) ``` +::: + +:::js +To **update** short-term memory, you can use tools that return a `Command` to update state: + +```typescript +import { Command } from "@langchain/langgraph"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const updateUserName = tool( + async (input) => { + // highlight-next-line + return new Command({ + // highlight-next-line + update: { + // highlight-next-line + userName: input.newName, + // highlight-next-line + messages: [ + // highlight-next-line + { + // highlight-next-line + role: "assistant", + // highlight-next-line + content: `Updated user name to ${input.newName}`, + // highlight-next-line + }, + // highlight-next-line + ], + // highlight-next-line + }, + // highlight-next-line + }); + }, + { + name: "update_user_name", + description: "Update user name in short-term memory.", + schema: z.object({ + newName: z.string().describe("The new user name"), + }), + } +); +``` + +::: + !!! important - If you want to use tools that return `Command` and update graph state, you can either use prebuilt [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] / [`ToolNode`][langgraph.prebuilt.tool_node.ToolNode] components, or implement your own tool-executing node that collects `Command` objects returned by the tools and returns a list of them, e.g.: - + :::python + If you want to use tools that return `Command` and update graph state, you can either use prebuilt @[`create_react_agent`][create_react_agent] / @[`ToolNode`][ToolNode] components, or implement your own tool-executing node that collects `Command` objects returned by the tools and returns a list of them, e.g.: + ```python def call_tools(state): ... commands = [tools_by_name[tool_call["name"]].invoke(tool_call) for tool_call in tool_calls] return commands ``` + ::: + :::js + If you want to use tools that return `Command` and update graph state, you can either use prebuilt @[`createReactAgent`][create_react_agent] / @[ToolNode] components, or implement your own tool-executing node that collects `Command` objects returned by the tools and returns a list of them, e.g.: + + ```typescript + const callTools = async (state: State) => { + // ... + const commands = await Promise.all( + toolCalls.map(toolCall => toolsByName[toolCall.name].invoke(toolCall)) + ); + return commands; + }; + ``` + ::: ### Long-term memory @@ -543,8 +1238,9 @@ Use [long-term memory](../concepts/memory.md#long-term-memory) to store user-spe To use long-term memory, you need to: 1. [Configure a store](memory/add-memory.md#add-long-term-memory) to persist data across invocations. -2. Use the [`get_store`][langgraph.config.get_store] function to access the store from within tools or prompts. +2. Access the store from within tools. +:::python To **access** information in the store: ```python @@ -557,7 +1253,7 @@ from langgraph.config import get_store @tool def get_user_info(config: RunnableConfig) -> str: """Look up user info.""" - # Same as that provided to `builder.compile(store=store)` + # Same as that provided to `builder.compile(store=store)` # or `create_react_agent` # highlight-next-line store = get_store() @@ -571,18 +1267,52 @@ builder = StateGraph(...) graph = builder.compile(store=store) ``` +::: + +:::js +To **access** information in the store: + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import type { LangGraphRunnableConfig } from "@langchain/langgraph"; + +const getUserInfo = tool( + async (_, config: LangGraphRunnableConfig) => { + // Same as that provided to `builder.compile({ store })` + // or `createReactAgent` + // highlight-next-line + const store = config.store; + if (!store) throw new Error("Store not provided"); + + const userId = config?.configurable?.user_id; + // highlight-next-line + const userInfo = await store.get(["users"], userId); + return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user"; + }, + { + name: "get_user_info", + description: "Look up user info.", + schema: z.object({}), + } +); +``` + +::: + ??? example "Access long-term memory" + :::python ```python from langchain_core.runnables import RunnableConfig from langchain_core.tools import tool from langgraph.config import get_store from langgraph.prebuilt import create_react_agent from langgraph.store.memory import InMemoryStore - + # highlight-next-line store = InMemoryStore() # (1)! - + # highlight-next-line store.put( # (2)! ("users",), # (3)! @@ -603,14 +1333,14 @@ graph = builder.compile(store=store) # highlight-next-line user_info = store.get(("users",), user_id) # (7)! return str(user_info.value) if user_info else "Unknown user" - + agent = create_react_agent( model="anthropic:claude-3-7-sonnet-latest", tools=[get_user_info], # highlight-next-line store=store # (8)! ) - + # Run the agent agent.invoke( {"messages": [{"role": "user", "content": "look up user information"}]}, @@ -618,16 +1348,84 @@ graph = builder.compile(store=store) config={"configurable": {"user_id": "user_123"}} ) ``` - + 1. The `InMemoryStore` is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the [store documentation][../reference/store.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready store for you. - 2. For this example, we write some sample data to the store using the `put` method. Please see the [BaseStore.put][langgraph.store.base.BaseStore.put] API reference for more details. + 2. For this example, we write some sample data to the store using the `put` method. Please see the @[BaseStore.put] API reference for more details. 3. The first argument is the namespace. This is used to group related data together. In this case, we are using the `users` namespace to group user data. 4. A key within the namespace. This example uses a user ID for the key. 5. The data that we want to store for the given user. 6. The `get_store` function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. 7. The `get` method is used to retrieve data from the store. The first argument is the namespace, and the second argument is the key. This will return a `StoreValue` object, which contains the value and metadata about the value. 8. The `store` is passed to the agent. This enables the agent to access the store when running tools. You can also use the `get_store` function to access the store from anywhere in your code. + ::: + :::js + ```typescript + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import { InMemoryStore } from "@langchain/langgraph"; + import { ChatAnthropic } from "@langchain/anthropic"; + import type { LangGraphRunnableConfig } from "@langchain/langgraph"; + + // highlight-next-line + const store = new InMemoryStore(); // (1)! + + // highlight-next-line + await store.put( // (2)! + ["users"], // (3)! + "user_123", // (4)! + { + name: "John Smith", + language: "English", + } // (5)! + ); + + const getUserInfo = tool( + async (_, config: LangGraphRunnableConfig) => { + // Same as that provided to `createReactAgent` + // highlight-next-line + const store = config.store; // (6)! + if (!store) throw new Error("Store not provided"); + + const userId = config?.configurable?.user_id; + // highlight-next-line + const userInfo = await store.get(["users"], userId); // (7)! + return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user"; + }, + { + name: "get_user_info", + description: "Look up user info.", + schema: z.object({}), + } + ); + + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-sonnet-20240620" }), + tools: [getUserInfo], + // highlight-next-line + store: store // (8)! + }); + + // Run the agent + await agent.invoke( + { messages: [{ role: "user", content: "look up user information" }] }, + // highlight-next-line + { configurable: { user_id: "user_123" } } + ); + ``` + + 1. The `InMemoryStore` is a store that stores data in memory. In production, you would typically use a database or other persistent storage. Please review the [store documentation](../reference/store.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready store for you. + 2. For this example, we write some sample data to the store using the `put` method. Please see the [BaseStore.put](https://js.langchain.com/docs/api/langgraph_store/classes/BaseStore.html#put) API reference for more details. + 3. The first argument is the namespace. This is used to group related data together. In this case, we are using the `users` namespace to group user data. + 4. A key within the namespace. This example uses a user ID for the key. + 5. The data that we want to store for the given user. + 6. The store is accessible from the config object that is passed to the tool. This enables the tool to access the store when running. + 7. The `get` method is used to retrieve data from the store. The first argument is the namespace, and the second argument is the key. This will return a `StoreValue` object, which contains the value and metadata about the value. + 8. The `store` is passed to the agent. This enables the agent to access the store when running tools. + ::: + +:::python To **update** information in the store: ```python @@ -640,7 +1438,7 @@ from langgraph.config import get_store @tool def save_user_info(user_info: str, config: RunnableConfig) -> str: """Save user info.""" - # Same as that provided to `builder.compile(store=store)` + # Same as that provided to `builder.compile(store=store)` # or `create_react_agent` # highlight-next-line store = get_store() @@ -654,8 +1452,44 @@ builder = StateGraph(...) graph = builder.compile(store=store) ``` +::: + +:::js +To **update** information in the store: + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; +import type { LangGraphRunnableConfig } from "@langchain/langgraph"; + +const saveUserInfo = tool( + async (input, config: LangGraphRunnableConfig) => { + // Same as that provided to `builder.compile({ store })` + // or `createReactAgent` + // highlight-next-line + const store = config.store; + if (!store) throw new Error("Store not provided"); + + const userId = config?.configurable?.user_id; + // highlight-next-line + await store.put(["users"], userId, input.userInfo); + return "Successfully saved user info."; + }, + { + name: "save_user_info", + description: "Save user info.", + schema: z.object({ + userInfo: z.string().describe("User information to save"), + }), + } +); +``` + +::: + ??? example "Update long-term memory" + :::python ```python from typing_extensions import TypedDict @@ -663,9 +1497,9 @@ graph = builder.compile(store=store) from langgraph.config import get_store from langgraph.prebuilt import create_react_agent from langgraph.store.memory import InMemoryStore - + store = InMemoryStore() # (1)! - + class UserInfo(TypedDict): # (2)! name: str @@ -679,36 +1513,99 @@ graph = builder.compile(store=store) # highlight-next-line store.put(("users",), user_id, user_info) # (5)! return "Successfully saved user info." - + agent = create_react_agent( model="anthropic:claude-3-7-sonnet-latest", tools=[save_user_info], # highlight-next-line store=store ) - + # Run the agent agent.invoke( {"messages": [{"role": "user", "content": "My name is John Smith"}]}, # highlight-next-line config={"configurable": {"user_id": "user_123"}} # (6)! ) - + # You can access the store directly to get the value store.get(("users",), "user_123").value ``` - + 1. The `InMemoryStore` is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the [store documentation](../reference/store.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready store for you. 2. The `UserInfo` class is a `TypedDict` that defines the structure of the user information. The LLM will use this to format the response according to the schema. 3. The `save_user_info` function is a tool that allows an agent to update user information. This could be useful for a chat application where the user wants to update their profile information. 4. The `get_store` function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. 5. The `put` method is used to store data in the store. The first argument is the namespace, and the second argument is the key. This will store the user information in the store. 6. The `user_id` is passed in the config. This is used to identify the user whose information is being updated. + ::: + + :::js + ```typescript + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import { InMemoryStore } from "@langchain/langgraph"; + import { ChatAnthropic } from "@langchain/anthropic"; + import type { LangGraphRunnableConfig } from "@langchain/langgraph"; + + const store = new InMemoryStore(); // (1)! + + const UserInfoSchema = z.object({ // (2)! + name: z.string(), + }); + + const saveUserInfo = tool( + async (input, config: LangGraphRunnableConfig) => { // (3)! + // Same as that provided to `createReactAgent` + // highlight-next-line + const store = config.store; // (4)! + if (!store) throw new Error("Store not provided"); + + const userId = config?.configurable?.user_id; + // highlight-next-line + await store.put(["users"], userId, input); // (5)! + return "Successfully saved user info."; + }, + { + name: "save_user_info", + description: "Save user info.", + schema: UserInfoSchema, + } + ); + + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-sonnet-20240620" }), + tools: [saveUserInfo], + // highlight-next-line + store: store + }); + + // Run the agent + await agent.invoke( + { messages: [{ role: "user", content: "My name is John Smith" }] }, + // highlight-next-line + { configurable: { user_id: "user_123" } } // (6)! + ); + + // You can access the store directly to get the value + const userInfo = await store.get(["users"], "user_123"); + console.log(userInfo?.value); + ``` + + 1. The `InMemoryStore` is a store that stores data in memory. In production, you would typically use a database or other persistent storage. Please review the [store documentation](../reference/store.md) for more options. If you're deploying with **LangGraph Platform**, the platform will provide a production-ready store for you. + 2. The `UserInfoSchema` is a Zod schema that defines the structure of the user information. The LLM will use this to format the response according to the schema. + 3. The `saveUserInfo` function is a tool that allows an agent to update user information. This could be useful for a chat application where the user wants to update their profile information. + 4. The store is accessible from the config object that is passed to the tool. This enables the tool to access the store when running. + 5. The `put` method is used to store data in the store. The first argument is the namespace, and the second argument is the key. This will store the user information in the store. + 6. The `user_id` is passed in the config. This is used to identify the user whose information is being updated. + ::: ## Advanced tool features ### Immediate return +:::python Use `return_direct=True` to immediately return a tool's result without executing additional logic. This is useful for tools that should not trigger further processing or tool calls, allowing you to return results directly to the user. @@ -721,8 +1618,40 @@ def add(a: int, b: int) -> int: return a + b ``` +::: + +:::js +Use `returnDirect: true` to immediately return a tool's result without executing additional logic. + +This is useful for tools that should not trigger further processing or tool calls, allowing you to return results directly to the user. + +```typescript +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +// highlight-next-line +const add = tool( + (input) => { + return input.a + input.b; + }, + { + name: "add", + description: "Add two numbers", + schema: z.object({ + a: z.number(), + b: z.number(), + }), + // highlight-next-line + returnDirect: true, + } +); +``` + +::: + ??? example "Extended example: Using return_direct in a prebuilt agent" + :::python ```python from langchain_core.tools import tool from langgraph.prebuilt import create_react_agent @@ -742,20 +1671,63 @@ def add(a: int, b: int) -> int: {"messages": [{"role": "user", "content": "what's 3 + 5?"}]} ) ``` + ::: + :::js + ```typescript + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import { ChatAnthropic } from "@langchain/anthropic"; + + // highlight-next-line + const add = tool( + (input) => { + return input.a + input.b; + }, + { + name: "add", + description: "Add two numbers", + schema: z.object({ + a: z.number(), + b: z.number(), + }), + // highlight-next-line + returnDirect: true, + } + ); + + const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-sonnet-20240620" }), + tools: [add] + }); + + await agent.invoke({ + messages: [{ role: "user", content: "what's 3 + 5?" }] + }); + ``` + ::: !!! important "Using without prebuilt components" + :::python If you are building a custom workflow and are not relying on `create_react_agent` or `ToolNode`, you will also need to implement the control flow to handle `return_direct=True`. + ::: + + :::js + If you are building a custom workflow and are not relying on `createReactAgent` or `ToolNode`, you will also + need to implement the control flow to handle `returnDirect: true`. + ::: ### Force tool use -If you need to force a specific tool to be used, you will need to configure this -at the **model** level using the `tool_choice` parameter in the `bind_tools` method. +If you need to force a specific tool to be used, you will need to configure this at the **model** level using the `tool_choice` parameter in the bind_tools method. Force specific tool usage via tool_choice: +:::python + ```python @tool(return_direct=True) def greet(user_name: str) -> int: @@ -770,11 +1742,42 @@ configured_model = model.bind_tools( # highlight-next-line tool_choice={"type": "tool", "name": "greet"} ) - ``` +::: + +:::js + +```typescript +const greet = tool( + (input) => { + return `Hello ${input.userName}!`; + }, + { + name: "greet", + description: "Greet user.", + schema: z.object({ + userName: z.string(), + }), + returnDirect: true, + } +); + +const tools = [greet]; + +const configuredModel = model.bindTools( + tools, + // Force the use of the 'greet' tool + // highlight-next-line + { tool_choice: { type: "tool", name: "greet" } } +); +``` + +::: + ??? example "Extended example: Force tool usage in an agent" + :::python To force the agent to use specific tools, you can set the `tool_choice` option in `model.bind_tools()`: ```python @@ -798,14 +1801,63 @@ configured_model = model.bind_tools( {"messages": [{"role": "user", "content": "Hi, I am Bob"}]} ) ``` + ::: + + :::js + To force the agent to use specific tools, you can set the `tool_choice` option in `model.bindTools()`: + + ```typescript + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + import { ChatOpenAI } from "@langchain/openai"; + + // highlight-next-line + const greet = tool( + (input) => { + return `Hello ${input.userName}!`; + }, + { + name: "greet", + description: "Greet user.", + schema: z.object({ + userName: z.string(), + }), + // highlight-next-line + returnDirect: true, + } + ); + + const tools = [greet]; + const model = new ChatOpenAI({ model: "gpt-4o" }); + + const agent = createReactAgent({ + // highlight-next-line + llm: model.bindTools(tools, { tool_choice: { type: "tool", name: "greet" } }), + tools: tools + }); + + await agent.invoke({ + messages: [{ role: "user", content: "Hi, I am Bob" }] + }); + ``` + ::: !!! Warning "Avoid infinite loops" + :::python Forcing tool usage without stopping conditions can create infinite loops. Use one of the following safeguards: - - Mark the tool with [`return_direct=True`](#immediate-return to end the loop after execution. + - Mark the tool with [`return_direct=True`](#immediate-return) to end the loop after execution. - Set [`recursion_limit`](../concepts/low_level.md#recursion-limit) to restrict the number of execution steps. + ::: + :::js + Forcing tool usage without stopping conditions can create infinite loops. Use one of the following safeguards: + + - Mark the tool with [`returnDirect: true`](#immediate-return) to end the loop after execution. + - Set [`recursionLimit`](../concepts/low_level.md#recursion-limit) to restrict the number of execution steps. + ::: !!! tip "Tool choice configuration" @@ -815,18 +1867,35 @@ configured_model = model.bind_tools( ### Disable parallel calls +:::python For supported providers, you can disable parallel tool calling by setting `parallel_tool_calls=False` via the `model.bind_tools()` method: ```python model.bind_tools( - tools, + tools, # highlight-next-line parallel_tool_calls=False ) ``` +::: + +:::js +For supported providers, you can disable parallel tool calling by setting `parallel_tool_calls: false` via the `model.bindTools()` method: + +```typescript +model.bindTools( + tools, + // highlight-next-line + { parallel_tool_calls: false } +); +``` + +::: + ??? example "Extended example: disable parallel tool calls in a prebuilt agent" + :::python ```python from langchain.chat_models import init_chat_model @@ -851,10 +1920,63 @@ model.bind_tools( {"messages": [{"role": "user", "content": "what's 3 + 5 and 4 * 7?"}]} ) ``` + ::: + + :::js + ```typescript + import { ChatOpenAI } from "@langchain/openai"; + import { tool } from "@langchain/core/tools"; + import { z } from "zod"; + import { createReactAgent } from "@langchain/langgraph/prebuilt"; + + const add = tool( + (input) => { + return input.a + input.b; + }, + { + name: "add", + description: "Add two numbers", + schema: z.object({ + a: z.number(), + b: z.number(), + }), + } + ); + + const multiply = tool( + (input) => { + return input.a * input.b; + }, + { + name: "multiply", + description: "Multiply two numbers.", + schema: z.object({ + a: z.number(), + b: z.number(), + }), + } + ); + + const model = new ChatOpenAI({ model: "gpt-4o", temperature: 0 }); + const tools = [add, multiply]; + + const agent = createReactAgent({ + // disable parallel tool calls + // highlight-next-line + llm: model.bindTools(tools, { parallel_tool_calls: false }), + tools: tools + }); + + await agent.invoke({ + messages: [{ role: "user", content: "what's 3 + 5 and 4 * 7?" }] + }); + ``` + ::: ### Handle errors -LangGraph provides built-in error handling for tool execution through the prebuilt [ToolNode][langgraph.prebuilt.tool_node.ToolNode] component, used both independently and in prebuilt agents. +:::python +LangGraph provides built-in error handling for tool execution through the prebuilt @[ToolNode][ToolNode] component, used both independently and in prebuilt agents. By **default**, `ToolNode` catches exceptions raised during tool execution and returns them as `ToolMessage` objects with a status indicating an error. @@ -896,19 +2018,96 @@ Output: ]} ``` +::: + +:::js +LangGraph provides built-in error handling for tool execution through the prebuilt [ToolNode](https://js.langchain.com/docs/api/langgraph_prebuilt/classes/ToolNode.html) component, used both independently and in prebuilt agents. + +By **default**, `ToolNode` catches exceptions raised during tool execution and returns them as `ToolMessage` objects with a status indicating an error. + +```typescript +import { AIMessage } from "@langchain/core/messages"; +import { ToolNode } from "@langchain/langgraph/prebuilt"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const multiply = tool( + (input) => { + if (input.a === 42) { + throw new Error("The ultimate error"); + } + return input.a * input.b; + }, + { + name: "multiply", + description: "Multiply two numbers", + schema: z.object({ + a: z.number(), + b: z.number(), + }), + } +); + +// Default error handling (enabled by default) +const toolNode = new ToolNode([multiply]); + +const message = new AIMessage({ + content: "", + tool_calls: [ + { + name: "multiply", + args: { a: 42, b: 7 }, + id: "tool_call_id", + type: "tool_call", + }, + ], +}); + +const result = await toolNode.invoke({ messages: [message] }); +``` + +Output: + +``` +{ messages: [ + ToolMessage { + content: "Error: The ultimate error\n Please fix your mistakes.", + name: "multiply", + tool_call_id: "tool_call_id", + status: "error" + } +]} +``` + +::: + #### Disable error handling To propagate exceptions directly, disable error handling: +:::python + ```python tool_node = ToolNode([multiply], handle_tool_errors=False) ``` +::: + +:::js + +```typescript +const toolNode = new ToolNode([multiply], { handleToolErrors: false }); +``` + +::: + With error handling disabled, exceptions raised by tools will propagate up, requiring explicit management. #### Custom error messages -Provide a custom error message by setting `handle_tool_errors` to a string: +Provide a custom error message by setting the error handling parameter to a string: + +:::python ```python tool_node = ToolNode( @@ -930,8 +2129,35 @@ Example output: ]} ``` +::: + +:::js + +```typescript +const toolNode = new ToolNode([multiply], { + handleToolErrors: + "Can't use 42 as the first operand, please switch operands!", +}); +``` + +Example output: + +```typescript +{ messages: [ + ToolMessage { + content: "Can't use 42 as the first operand, please switch operands!", + name: "multiply", + tool_call_id: "tool_call_id", + status: "error" + } +]} +``` + +::: + #### Error handling in agents +:::python Error handling in prebuilt agents (`create_react_agent`) leverages `ToolNode`: ```python @@ -962,6 +2188,45 @@ agent_custom = create_react_agent( agent_custom.invoke({"messages": [{"role": "user", "content": "what's 42 x 7?"}]}) ``` +::: + +:::js +Error handling in prebuilt agents (`createReactAgent`) leverages `ToolNode`: + +```typescript +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { ChatAnthropic } from "@langchain/anthropic"; + +const agent = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-sonnet-20240620" }), + tools: [multiply], +}); + +// Default error handling +await agent.invoke({ + messages: [{ role: "user", content: "what's 42 x 7?" }], +}); +``` + +To disable or customize error handling in prebuilt agents, explicitly pass a configured `ToolNode`: + +```typescript +const customToolNode = new ToolNode([multiply], { + handleToolErrors: "Cannot use 42 as a first operand!", +}); + +const agentCustom = createReactAgent({ + llm: new ChatAnthropic({ model: "claude-3-5-sonnet-20240620" }), + tools: customToolNode, +}); + +await agentCustom.invoke({ + messages: [{ role: "user", content: "what's 42 x 7?" }], +}); +``` + +::: + ### Handle large numbers of tools As the number of available tools grows, you may want to limit the scope of the LLM's selection, to decrease token consumption and to help manage sources of error in LLM reasoning. @@ -974,13 +2239,14 @@ See [`langgraph-bigtool`](https://github.com/langchain-ai/langgraph-bigtool) pre ### LLM provider tools +:::python You can use prebuilt tools from model providers by passing a dictionary with tool specs to the `tools` parameter of `create_react_agent`. For example, to use the `web_search_preview` tool from OpenAI: ```python from langgraph.prebuilt import create_react_agent agent = create_react_agent( - model="openai:gpt-4o-mini", + model="openai:gpt-4o-mini", tools=[{"type": "web_search_preview"}] ) response = agent.invoke( @@ -989,11 +2255,35 @@ response = agent.invoke( ``` Please consult the documentation for the specific model you are using to see which tools are available and how to use them. +::: + +:::js +You can use prebuilt tools from model providers by passing a dictionary with tool specs to the `tools` parameter of `createReactAgent`. For example, to use the `web_search_preview` tool from OpenAI: + +```typescript +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { ChatOpenAI } from "@langchain/openai"; + +const agent = createReactAgent({ + llm: new ChatOpenAI({ model: "gpt-4o-mini" }), + tools: [{ type: "web_search_preview" }], +}); + +const response = await agent.invoke({ + messages: [ + { role: "user", content: "What was a positive news story from today?" }, + ], +}); +``` + +Please consult the documentation for the specific model you are using to see which tools are available and how to use them. +::: ### LangChain tools Additionally, LangChain supports a wide range of prebuilt tool integrations for interacting with APIs, databases, file systems, web data, and more. These tools extend the functionality of agents and enable rapid development. +:::python You can browse the full list of available integrations in the [LangChain integrations directory](https://python.langchain.com/docs/integrations/tools/). Some commonly used tool categories include: @@ -1005,4 +2295,18 @@ Some commonly used tool categories include: - **APIs**: OpenWeatherMap, NewsAPI, and others These integrations can be configured and added to your agents using the same `tools` parameter shown in the examples above. +::: +:::js +You can browse the full list of available integrations in the [LangChain integrations directory](https://js.langchain.com/docs/integrations/tools/). + +Some commonly used tool categories include: + +- **Search**: Tavily, SerpAPI +- **Code interpreters**: Web browsers, calculators +- **Databases**: SQL, vector databases +- **Web data**: Web scraping and browsing +- **APIs**: Various API integrations + +These integrations can be configured and added to your agents using the same `tools` parameter shown in the examples above. +::: diff --git a/docs/docs/how-tos/ttl/configure_ttl.md b/docs/docs/how-tos/ttl/configure_ttl.md index a5f1e0b05..9dc402397 100644 --- a/docs/docs/how-tos/ttl/configure_ttl.md +++ b/docs/docs/how-tos/ttl/configure_ttl.md @@ -16,6 +16,7 @@ Checkpoints capture the state of conversation threads. Setting a TTL ensures old Add a `checkpointer.ttl` configuration to your `langgraph.json` file: +:::python ```json { "dependencies": ["."], @@ -31,6 +32,25 @@ Add a `checkpointer.ttl` configuration to your `langgraph.json` file: } } ``` +::: + +:::js +```json +{ + "dependencies": ["."], + "graphs": { + "agent": "./agent.ts:graph" + }, + "checkpointer": { + "ttl": { + "strategy": "delete", + "sweep_interval_minutes": 60, + "default_ttl": 43200 + } + } +} +``` +::: * `strategy`: Specifies the action taken on expiration. Currently, only `"delete"` is supported, which deletes all checkpoints in the thread upon expiration. * `sweep_interval_minutes`: Defines how often, in minutes, the system checks for expired checkpoints. @@ -42,6 +62,7 @@ Store items allow cross-thread data persistence. Configuring TTL for store items Add a `store.ttl` configuration to your `langgraph.json` file: +:::python ```json { "dependencies": ["."], @@ -57,6 +78,25 @@ Add a `store.ttl` configuration to your `langgraph.json` file: } } ``` +::: + +:::js +```json +{ + "dependencies": ["."], + "graphs": { + "agent": "./agent.ts:graph" + }, + "store": { + "ttl": { + "refresh_on_read": true, + "sweep_interval_minutes": 120, + "default_ttl": 10080 + } + } +} +``` +::: * `refresh_on_read`: (Optional, default `true`) If `true`, accessing an item via `get` or `search` resets its expiration timer. If `false`, TTL only refreshes on `put`. * `sweep_interval_minutes`: (Optional) Defines how often, in minutes, the system checks for expired items. If omitted, no sweeping occurs. @@ -66,6 +106,7 @@ Add a `store.ttl` configuration to your `langgraph.json` file: You can configure TTLs for both checkpoints and store items in the same `langgraph.json` file to set different policies for each data type. Here is an example: +:::python ```json { "dependencies": ["."], @@ -88,6 +129,32 @@ You can configure TTLs for both checkpoints and store items in the same `langgra } } ``` +::: + +:::js +```json +{ + "dependencies": ["."], + "graphs": { + "agent": "./agent.ts:graph" + }, + "checkpointer": { + "ttl": { + "strategy": "delete", + "sweep_interval_minutes": 60, + "default_ttl": 43200 + } + }, + "store": { + "ttl": { + "refresh_on_read": true, + "sweep_interval_minutes": 120, + "default_ttl": 10080 + } + } +} +``` +::: ## Runtime Overrides @@ -97,6 +164,4 @@ The default `store.ttl` settings from `langgraph.json` can be overridden at runt After configuring TTLs in `langgraph.json`, deploy or restart your LangGraph application for the changes to take effect. Use `langgraph dev` for local development or `langgraph up` for Docker deployment. - -See the [langgraph.json CLI reference][configuration-file] for more details on the other configurable options. - +See the @[langgraph.json CLI reference][langgraph.json] for more details on the other configurable options. \ No newline at end of file diff --git a/docs/docs/how-tos/use-functional-api.md b/docs/docs/how-tos/use-functional-api.md index 654930f69..a57e2718c 100644 --- a/docs/docs/how-tos/use-functional-api.md +++ b/docs/docs/how-tos/use-functional-api.md @@ -6,11 +6,12 @@ The [**Functional API**](../concepts/functional_api.md) allows you to add LangGr For conceptual information on the functional API, see [Functional API](../concepts/functional_api.md). - ## Creating a simple workflow When defining an `entrypoint`, input is restricted to the first argument of the function. To pass multiple inputs, you can use a dictionary. +:::python + ```python @entrypoint(checkpointer=checkpointer) def my_workflow(inputs: dict) -> int: @@ -18,11 +19,33 @@ def my_workflow(inputs: dict) -> int: another_value = inputs["another_value"] ... -my_workflow.invoke({"value": 1, "another_value": 2}) +my_workflow.invoke({"value": 1, "another_value": 2}) ``` -??? example "Extended example: simple workflow" +::: +:::js + +```typescript +const checkpointer = new MemorySaver(); + +const myWorkflow = entrypoint( + { checkpointer, name: "myWorkflow" }, + async (inputs: { value: number; anotherValue: number }) => { + const value = inputs.value; + const anotherValue = inputs.anotherValue; + // ... + } +); + +await myWorkflow.invoke({ value: 1, anotherValue: 2 }); +``` + +::: + +??? example "Extended example: simple workflow" + + :::python ```python import uuid from langgraph.func import entrypoint, task @@ -52,6 +75,41 @@ my_workflow.invoke({"value": 1, "another_value": 2}) result = workflow.invoke({"number": 7}, config=config) print(result) ``` + ::: + + :::js + ```typescript + import { v4 as uuidv4 } from "uuid"; + import { entrypoint, task, MemorySaver } from "@langchain/langgraph"; + + // Task that checks if a number is even + const isEven = task("isEven", async (number: number) => { + return number % 2 === 0; + }); + + // Task that formats a message + const formatMessage = task("formatMessage", async (isEven: boolean) => { + return isEven ? "The number is even." : "The number is odd."; + }); + + // Create a checkpointer for persistence + const checkpointer = new MemorySaver(); + + const workflow = entrypoint( + { checkpointer, name: "workflow" }, + async (inputs: { number: number }) => { + // Simple workflow to classify a number + const even = await isEven(inputs.number); + return await formatMessage(even); + } + ); + + // Run the workflow with a unique thread ID + const config = { configurable: { thread_id: uuidv4() } }; + const result = await workflow.invoke({ number: 7 }, config); + console.log(result); + ``` + ::: ??? example "Extended example: Compose an essay with an LLM" @@ -59,6 +117,7 @@ my_workflow.invoke({"value": 1, "another_value": 2}) syntactically. Given that a checkpointer is provided, the workflow results will be persisted in the checkpointer. + :::python ```python import uuid from langchain.chat_models import init_chat_model @@ -89,11 +148,50 @@ my_workflow.invoke({"value": 1, "another_value": 2}) result = workflow.invoke("the history of flight", config=config) print(result) ``` + ::: + + :::js + ```typescript + import { v4 as uuidv4 } from "uuid"; + import { ChatOpenAI } from "@langchain/openai"; + import { entrypoint, task, MemorySaver } from "@langchain/langgraph"; + + const llm = new ChatOpenAI({ model: "gpt-3.5-turbo" }); + + // Task: generate essay using an LLM + const composeEssay = task("composeEssay", async (topic: string) => { + // Generate an essay about the given topic + const response = await llm.invoke([ + { role: "system", content: "You are a helpful assistant that writes essays." }, + { role: "user", content: `Write an essay about ${topic}.` } + ]); + return response.content as string; + }); + + // Create a checkpointer for persistence + const checkpointer = new MemorySaver(); + + const workflow = entrypoint( + { checkpointer, name: "workflow" }, + async (topic: string) => { + // Simple workflow that generates an essay with an LLM + return await composeEssay(topic); + } + ); + + // Execute the workflow + const config = { configurable: { thread_id: uuidv4() } }; + const result = await workflow.invoke("the history of flight", config); + console.log(result); + ``` + ::: ## Parallel execution Tasks can be executed in parallel by invoking them concurrently and waiting for the results. This is useful for improving performance in IO bound tasks (e.g., calling APIs for LLMs). +:::python + ```python @task def add_one(number: int) -> int: @@ -105,11 +203,30 @@ def graph(numbers: list[int]) -> list[str]: return [f.result() for f in futures] ``` +::: + +:::js + +```typescript +const addOne = task("addOne", async (number: number) => { + return number + 1; +}); + +const graph = entrypoint( + { checkpointer, name: "graph" }, + async (numbers: number[]) => { + return await Promise.all(numbers.map(addOne)); + } +); +``` + +::: ??? example "Extended example: parallel LLM calls" This example demonstrates how to run multiple LLM calls in parallel using `@task`. Each call generates a paragraph on a different topic, and results are joined into a single text output. + :::python ```python import uuid from langchain.chat_models import init_chat_model @@ -143,13 +260,53 @@ def graph(numbers: list[int]) -> list[str]: result = workflow.invoke(["quantum computing", "climate change", "history of aviation"], config=config) print(result) ``` + ::: + + :::js + ```typescript + import { v4 as uuidv4 } from "uuid"; + import { ChatOpenAI } from "@langchain/openai"; + import { entrypoint, task, MemorySaver } from "@langchain/langgraph"; + + // Initialize the LLM model + const llm = new ChatOpenAI({ model: "gpt-3.5-turbo" }); + + // Task that generates a paragraph about a given topic + const generateParagraph = task("generateParagraph", async (topic: string) => { + const response = await llm.invoke([ + { role: "system", content: "You are a helpful assistant that writes educational paragraphs." }, + { role: "user", content: `Write a paragraph about ${topic}.` } + ]); + return response.content as string; + }); + + // Create a checkpointer for persistence + const checkpointer = new MemorySaver(); + + const workflow = entrypoint( + { checkpointer, name: "workflow" }, + async (topics: string[]) => { + // Generates multiple paragraphs in parallel and combines them + const paragraphs = await Promise.all(topics.map(generateParagraph)); + return paragraphs.join("\n\n"); + } + ); + + // Run the workflow + const config = { configurable: { thread_id: uuidv4() } }; + const result = await workflow.invoke(["quantum computing", "climate change", "history of aviation"], config); + console.log(result); + ``` + ::: This example uses LangGraph's concurrency model to improve execution time, especially when tasks involve I/O like LLM completions. -## Calling graphs +## Calling graphs The **Functional API** and the [**Graph API**](../concepts/low_level.md) can be used together in the same application as they share the same underlying runtime. +:::python + ```python from langgraph.func import entrypoint from langgraph.graph import StateGraph @@ -170,8 +327,38 @@ def some_workflow(some_input: dict) -> int: } ``` +::: + +:::js + +```typescript +import { entrypoint } from "@langchain/langgraph"; +import { StateGraph } from "@langchain/langgraph"; + +const builder = new StateGraph(/* ... */); +// ... +const someGraph = builder.compile(); + +const someWorkflow = entrypoint( + { name: "someWorkflow" }, + async (someInput: Record<string, any>) => { + // Call a graph defined using the graph API + const result1 = await someGraph.invoke(/* ... */); + // Call another graph defined using the graph API + const result2 = await anotherGraph.invoke(/* ... */); + return { + result1, + result2, + }; + } +); +``` + +::: + ??? example "Extended example: calling a simple graph from the functional API" + :::python ```python import uuid from typing import TypedDict @@ -205,12 +392,51 @@ def some_workflow(some_input: dict) -> int: config = {"configurable": {"thread_id": str(uuid.uuid4())}} print(workflow.invoke(5, config=config)) # Output: {'bar': 10} ``` + ::: + :::js + ```typescript + import { v4 as uuidv4 } from "uuid"; + import { entrypoint, MemorySaver } from "@langchain/langgraph"; + import { StateGraph } from "@langchain/langgraph"; + import { z } from "zod"; + + // Define the shared state type + const State = z.object({ + foo: z.number(), + }); + + // Build the graph using the Graph API + const builder = new StateGraph(State) + .addNode("double", (state) => { + return { foo: state.foo * 2 }; + }) + .addEdge("__start__", "double"); + const graph = builder.compile(); + + // Define the functional API workflow + const checkpointer = new MemorySaver(); + + const workflow = entrypoint( + { checkpointer, name: "workflow" }, + async (x: number) => { + const result = await graph.invoke({ foo: x }); + return { bar: result.foo }; + } + ); + + // Execute the workflow + const config = { configurable: { thread_id: uuidv4() } }; + console.log(await workflow.invoke(5, config)); // Output: { bar: 10 } + ``` + ::: ## Call other entrypoints You can call other **entrypoints** from within an **entrypoint** or a **task**. +:::python + ```python @entrypoint() # Will automatically use the checkpointer from the parent entrypoint def some_other_workflow(inputs: dict) -> int: @@ -222,8 +448,33 @@ def my_workflow(inputs: dict) -> int: return value ``` +::: + +:::js + +```typescript +// Will automatically use the checkpointer from the parent entrypoint +const someOtherWorkflow = entrypoint( + { name: "someOtherWorkflow" }, + async (inputs: { value: number }) => { + return inputs.value; + } +); + +const myWorkflow = entrypoint( + { checkpointer, name: "myWorkflow" }, + async (inputs: { value: number }) => { + const value = await someOtherWorkflow.invoke({ value: 1 }); + return value; + } +); +``` + +::: + ??? example "Extended example: calling another entrypoint" + :::python ```python import uuid from langgraph.func import entrypoint @@ -247,7 +498,38 @@ def my_workflow(inputs: dict) -> int: config = {"configurable": {"thread_id": str(uuid.uuid4())}} print(main.invoke({"x": 6, "y": 7}, config=config)) # Output: {'product': 42} ``` - + ::: + + :::js + ```typescript + import { v4 as uuidv4 } from "uuid"; + import { entrypoint, MemorySaver } from "@langchain/langgraph"; + + // Initialize a checkpointer + const checkpointer = new MemorySaver(); + + // A reusable sub-workflow that multiplies a number + const multiply = entrypoint( + { name: "multiply" }, + async (inputs: { a: number; b: number }) => { + return inputs.a * inputs.b; + } + ); + + // Main workflow that invokes the sub-workflow + const main = entrypoint( + { checkpointer, name: "main" }, + async (inputs: { x: number; y: number }) => { + const result = await multiply.invoke({ a: inputs.x, b: inputs.y }); + return { product: result }; + } + ); + + // Execute the main workflow + const config = { configurable: { thread_id: uuidv4() } }; + console.log(await main.invoke({ x: 6, y: 7 }, config)); // Output: { product: 42 } + ``` + ::: ## Streaming @@ -256,6 +538,8 @@ read the [**streaming guide**](../concepts/streaming.md) section for more detail Example of using the streaming API to stream both updates and custom data. +:::python + ```python from langgraph.func import entrypoint from langgraph.checkpoint.memory import InMemorySaver @@ -297,11 +581,9 @@ for mode, chunk in main.stream( # (5)! ('updates', {'main': 5}) ``` - - !!! important "Async with Python < 3.11" - If using Python < 3.11 and writing async code, using `get_stream_writer()` will not work. Instead please + If using Python < 3.11 and writing async code, using `get_stream_writer()` will not work. Instead please use the `StreamWriter` class directly. See [Async with Python < 3.11](../how-tos/streaming.md#async) for more details. ```python @@ -313,8 +595,62 @@ for mode, chunk in main.stream( # (5)! ... ``` +::: + +:::js + +```typescript +import { + entrypoint, + MemorySaver, + LangGraphRunnableConfig, +} from "@langchain/langgraph"; + +const checkpointer = new MemorySaver(); + +const main = entrypoint( + { checkpointer, name: "main" }, + async ( + inputs: { x: number }, + config: LangGraphRunnableConfig + ): Promise<number> => { + config.writer?.("Started processing"); // (1)! + const result = inputs.x * 2; + config.writer?.(`Result is ${result}`); // (2)! + return result; + } +); + +const config = { configurable: { thread_id: "abc" } }; + +// (3)! +for await (const [mode, chunk] of await main.stream( + { x: 5 }, + { streamMode: ["custom", "updates"], ...config } // (4)! +)) { + console.log(`${mode}: ${JSON.stringify(chunk)}`); +} +``` + +1. Emit custom data before computation begins. +2. Emit another custom message after computing the result. +3. Use `.stream()` to process streamed output. +4. Specify which streaming modes to use. + +``` +updates: {"addOne": 2} +updates: {"addTwo": 3} +custom: "hello" +custom: "world" +updates: {"main": 5} +``` + +::: + ## Retry policy +:::python + ```python from langgraph.checkpoint.memory import InMemorySaver from langgraph.func import entrypoint, task @@ -328,7 +664,7 @@ attempts = 0 # The default RetryPolicy is optimized for retrying specific network errors. retry_policy = RetryPolicy(retry_on=ValueError) -@task(retry_policy=retry_policy) +@task(retry_policy=retry_policy) def get_info(): global attempts attempts += 1 @@ -356,8 +692,69 @@ main.invoke({'any_input': 'foobar'}, config=config) 'OK' ``` +::: + +:::js + +```typescript +import { + MemorySaver, + entrypoint, + task, + RetryPolicy, +} from "@langchain/langgraph"; + +// This variable is just used for demonstration purposes to simulate a network failure. +// It's not something you will have in your actual code. +let attempts = 0; + +// Let's configure the RetryPolicy to retry on ValueError. +// The default RetryPolicy is optimized for retrying specific network errors. +const retryPolicy: RetryPolicy = { retryOn: (error) => error instanceof Error }; + +const getInfo = task( + { + name: "getInfo", + retry: retryPolicy, + }, + () => { + attempts += 1; + + if (attempts < 2) { + throw new Error("Failure"); + } + return "OK"; + } +); + +const checkpointer = new MemorySaver(); + +const main = entrypoint( + { checkpointer, name: "main" }, + async (inputs: Record<string, any>) => { + return await getInfo(); + } +); + +const config = { + configurable: { + thread_id: "1", + }, +}; + +await main.invoke({ any_input: "foobar" }, config); +``` + +``` +'OK' +``` + +::: + ## Caching Tasks +:::python + ```python import time from langgraph.cache.memory import InMemoryCache @@ -387,9 +784,57 @@ for chunk in main.stream({"x": 5}, stream_mode="updates"): ``` 1. `ttl` is specified in seconds. The cache will be invalidated after this time. + ::: + +:::js + +```typescript +import { + InMemoryCache, + entrypoint, + task, + CachePolicy, +} from "@langchain/langgraph"; + +const slowAdd = task( + { + name: "slowAdd", + cache: { ttl: 120 }, // (1)! + }, + async (x: number) => { + await new Promise((resolve) => setTimeout(resolve, 1000)); + return x * 2; + } +); + +const main = entrypoint( + { cache: new InMemoryCache(), name: "main" }, + async (inputs: { x: number }) => { + const result1 = await slowAdd(inputs.x); + const result2 = await slowAdd(inputs.x); + return { result1, result2 }; + } +); + +for await (const chunk of await main.stream( + { x: 5 }, + { streamMode: "updates" } +)) { + console.log(chunk); +} + +//> { slowAdd: 10 } +//> { slowAdd: 10, '__metadata__': { cached: true } } +//> { main: { result1: 10, result2: 10 } } +``` + +1. `ttl` is specified in seconds. The cache will be invalidated after this time. + ::: ## Resuming after an error +:::python + ```python import time from langgraph.checkpoint.memory import InMemorySaver @@ -465,6 +910,87 @@ main.invoke(None, config=config) 'Ran slow task.' ``` +::: + +:::js + +```typescript +import { entrypoint, task, MemorySaver } from "@langchain/langgraph"; + +// This variable is just used for demonstration purposes to simulate a network failure. +// It's not something you will have in your actual code. +let attempts = 0; + +const getInfo = task("getInfo", async () => { + /** + * Simulates a task that fails once before succeeding. + * Throws an exception on the first attempt, then returns "OK" on subsequent tries. + */ + attempts += 1; + + if (attempts < 2) { + throw new Error("Failure"); // Simulate a failure on the first attempt + } + return "OK"; +}); + +// Initialize an in-memory checkpointer for persistence +const checkpointer = new MemorySaver(); + +const slowTask = task("slowTask", async () => { + /** + * Simulates a slow-running task by introducing a 1-second delay. + */ + await new Promise((resolve) => setTimeout(resolve, 1000)); + return "Ran slow task."; +}); + +const main = entrypoint( + { checkpointer, name: "main" }, + async (inputs: Record<string, any>) => { + /** + * Main workflow function that runs the slowTask and getInfo tasks sequentially. + * + * Parameters: + * - inputs: Record<string, any> containing workflow input values. + * + * The workflow first executes `slowTask` and then attempts to execute `getInfo`, + * which will fail on the first invocation. + */ + const slowTaskResult = await slowTask(); // Blocking call to slowTask + await getInfo(); // Exception will be raised here on the first attempt + return slowTaskResult; + } +); + +// Workflow execution configuration with a unique thread identifier +const config = { + configurable: { + thread_id: "1", // Unique identifier to track workflow execution + }, +}; + +// This invocation will take ~1 second due to the slowTask execution +try { + // First invocation will raise an exception due to the `getInfo` task failing + await main.invoke({ any_input: "foobar" }, config); +} catch (err) { + // Handle the failure gracefully +} +``` + +When we resume execution, we won't need to re-run the `slowTask` as its result is already saved in the checkpoint. + +```typescript +await main.invoke(null, config); +``` + +``` +'Ran slow task.' +``` + +::: + ## Human-in-the-loop The functional API supports [human-in-the-loop](../concepts/human_in_the_loop.md) workflows using the `interrupt` function and the `Command` primitive. @@ -477,6 +1003,8 @@ We will create three [tasks](../concepts/functional_api.md#task): 2. Pause for human input. When resuming, append human input. 3. Append `"qux"`. +:::python + ```python from langgraph.func import entrypoint, task from langgraph.types import Command, interrupt @@ -499,10 +1027,38 @@ def human_feedback(input_query): def step_3(input_query): """Append qux.""" return f"{input_query} qux" -``` +``` + +::: + +:::js + +```typescript +import { entrypoint, task, interrupt, Command } from "@langchain/langgraph"; + +const step1 = task("step1", async (inputQuery: string) => { + // Append bar + return `${inputQuery} bar`; +}); + +const humanFeedback = task("humanFeedback", async (inputQuery: string) => { + // Append user input + const feedback = interrupt(`Please provide feedback: ${inputQuery}`); + return `${inputQuery} ${feedback}`; +}); + +const step3 = task("step3", async (inputQuery: string) => { + // Append qux + return `${inputQuery} qux`; +}); +``` + +::: We can now compose these tasks in an [entrypoint](../concepts/functional_api.md#entrypoint): +:::python + ```python from langgraph.checkpoint.memory import InMemorySaver @@ -518,10 +1074,35 @@ def graph(input_query): return result_3 ``` +::: + +:::js + +```typescript +import { MemorySaver } from "@langchain/langgraph"; + +const checkpointer = new MemorySaver(); + +const graph = entrypoint( + { checkpointer, name: "graph" }, + async (inputQuery: string) => { + const result1 = await step1(inputQuery); + const result2 = await humanFeedback(result1); + const result3 = await step3(result2); + + return result3; + } +); +``` + +::: + [interrupt()](../how-tos/human_in_the_loop/add-human-in-the-loop.md#pause-using-interrupt) is called inside a task, enabling a human to review and edit the output of the previous task. The results of prior tasks-- in this case `step_1`-- are persisted, so that they are not run again following the `interrupt`. Let's send in a query string: +:::python + ```python config = {"configurable": {"thread_id": "1"}} @@ -530,14 +1111,49 @@ for event in graph.stream("foo", config): print("\n") ``` +::: + +:::js + +```typescript +const config = { configurable: { thread_id: "1" } }; + +for await (const event of await graph.stream("foo", config)) { + console.log(event); + console.log("\n"); +} +``` + +::: + Note that we've paused with an `interrupt` after `step_1`. The interrupt provides instructions to resume the run. To resume, we issue a [Command](../how-tos/human_in_the_loop/add-human-in-the-loop.md#resume-using-the-command-primitive) containing the data expected by the `human_feedback` task. +:::python + ```python # Continue execution for event in graph.stream(Command(resume="baz"), config): print(event) print("\n") ``` + +::: + +:::js + +```typescript +// Continue execution +for await (const event of await graph.stream( + new Command({ resume: "baz" }), + config +)) { + console.log(event); + console.log("\n"); +} +``` + +::: + After resuming, the run proceeds through the remaining step and terminates as expected. ### Review tool calls @@ -550,6 +1166,8 @@ Given a tool call, our function will `interrupt` for human review. At that point - Revise the tool call and continue - Generate a custom tool message (e.g., instructing the model to re-format its tool call) +:::python + ```python from typing import Union @@ -574,8 +1192,47 @@ def review_tool_call(tool_call: ToolCall) -> Union[ToolCall, ToolMessage]: ) ``` +::: + +:::js + +```typescript +import { ToolCall } from "@langchain/core/messages/tool"; +import { ToolMessage } from "@langchain/core/messages"; + +function reviewToolCall(toolCall: ToolCall): ToolCall | ToolMessage { + // Review a tool call, returning a validated version + const humanReview = interrupt({ + question: "Is this correct?", + tool_call: toolCall, + }); + + const reviewAction = humanReview.action; + const reviewData = humanReview.data; + + if (reviewAction === "continue") { + return toolCall; + } else if (reviewAction === "update") { + const updatedToolCall = { ...toolCall, args: reviewData }; + return updatedToolCall; + } else if (reviewAction === "feedback") { + return new ToolMessage({ + content: reviewData, + name: toolCall.name, + tool_call_id: toolCall.id, + }); + } + + throw new Error(`Unknown review action: ${reviewAction}`); +} +``` + +::: + We can now update our [entrypoint](../concepts/functional_api.md#entrypoint) to review the generated tool calls. If a tool call is accepted or revised, we execute in the same way as before. Otherwise, we just append the `ToolMessage` supplied by the human. The results of prior tasks — in this case the initial model call — are persisted, so that they are not run again following the `interrupt`. +:::python + ```python from langgraph.checkpoint.memory import InMemorySaver from langgraph.graph.message import add_messages @@ -625,6 +1282,80 @@ def agent(messages, previous): return entrypoint.final(value=llm_response, save=messages) ``` +::: + +:::js + +```typescript +import { + MemorySaver, + entrypoint, + interrupt, + Command, + addMessages, +} from "@langchain/langgraph"; +import { ToolMessage, AIMessage, BaseMessage } from "@langchain/core/messages"; + +const checkpointer = new MemorySaver(); + +const agent = entrypoint( + { checkpointer, name: "agent" }, + async ( + messages: BaseMessage[], + previous?: BaseMessage[] + ): Promise<BaseMessage> => { + if (previous !== undefined) { + messages = addMessages(previous, messages); + } + + let llmResponse = await callModel(messages); + while (true) { + if (!llmResponse.tool_calls?.length) { + break; + } + + // Review tool calls + const toolResults: ToolMessage[] = []; + const toolCalls: ToolCall[] = []; + + for (let i = 0; i < llmResponse.tool_calls.length; i++) { + const review = reviewToolCall(llmResponse.tool_calls[i]); + if (review instanceof ToolMessage) { + toolResults.push(review); + } else { + // is a validated tool call + toolCalls.push(review); + if (review !== llmResponse.tool_calls[i]) { + llmResponse.tool_calls[i] = review; // update message + } + } + } + + // Execute remaining tool calls + const remainingToolResults = await Promise.all( + toolCalls.map((toolCall) => callTool(toolCall)) + ); + + // Append to message list + messages = addMessages(messages, [ + llmResponse, + ...toolResults, + ...remainingToolResults, + ]); + + // Call model again + llmResponse = await callModel(messages); + } + + // Generate final response + messages = addMessages(messages, llmResponse); + return entrypoint.final({ value: llmResponse, save: messages }); + } +); +``` + +::: + ## Short-term memory Short-term memory allows storing information across different **invocations** of the same **thread id**. See [short-term memory](../concepts/functional_api.md#short-term-memory) for more details. @@ -635,6 +1366,8 @@ You can view and delete the information stored by the checkpointer. #### View thread state (checkpoint) +:::python + ```python config = { "configurable": { @@ -644,7 +1377,7 @@ config = { # otherwise the latest checkpoint is shown # highlight-next-line # "checkpoint_id": "1f029ca3-1f5b-6704-8004-820c16b69a5a" - + } } # highlight-next-line @@ -653,7 +1386,7 @@ graph.get_state(config) ``` StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today?), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, next=(), + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today?), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, next=(), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, metadata={ 'source': 'loop', @@ -663,14 +1396,63 @@ StateSnapshot( 'thread_id': '1' }, created_at='2025-05-05T16:01:24.680462+00:00', - parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, + parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, tasks=(), interrupts=() ) ``` +::: + +:::js + +```typescript +const config = { + configurable: { + // highlight-next-line + thread_id: "1", + // optionally provide an ID for a specific checkpoint, + // otherwise the latest checkpoint is shown + // highlight-next-line + // checkpoint_id: "1f029ca3-1f5b-6704-8004-820c16b69a5a" + }, +}; +// highlight-next-line +await graph.getState(config); +``` + +``` +StateSnapshot { + values: { + messages: [ + HumanMessage { content: "hi! I'm bob" }, + AIMessage { content: "Hi Bob! How are you doing today?" }, + HumanMessage { content: "what's my name?" }, + AIMessage { content: "Your name is Bob." } + ] + }, + next: [], + config: { configurable: { thread_id: '1', checkpoint_ns: '', checkpoint_id: '1f029ca3-1f5b-6704-8004-820c16b69a5a' } }, + metadata: { + source: 'loop', + writes: { call_model: { messages: AIMessage { content: "Your name is Bob." } } }, + step: 4, + parents: {}, + thread_id: '1' + }, + createdAt: '2025-05-05T16:01:24.680462+00:00', + parentConfig: { configurable: { thread_id: '1', checkpoint_ns: '', checkpoint_id: '1f029ca3-1790-6b0a-8003-baf965b6a38f' } }, + tasks: [], + interrupts: [] +} +``` + +::: + #### View the history of the thread (checkpoints) +:::python + ```python config = { "configurable": { @@ -685,9 +1467,9 @@ list(graph.get_state_history(config)) ``` [ StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, - next=(), - config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, + next=(), + config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}}, metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Your name is Bob.')}}, 'step': 4, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:24.680462+00:00', parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, @@ -695,8 +1477,8 @@ list(graph.get_state_history(config)) interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?")]}, - next=('call_model',), + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?")]}, + next=('call_model',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}}, metadata={'source': 'loop', 'writes': None, 'step': 3, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:23.863421+00:00', @@ -705,9 +1487,9 @@ list(graph.get_state_history(config)) interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, - next=('__start__',), - config={...}, + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, + next=('__start__',), + config={...}, metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "what's my name?"}]}}, 'step': 2, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:23.863173+00:00', parent_config={...} @@ -715,9 +1497,9 @@ list(graph.get_state_history(config)) interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, - next=(), - config={...}, + values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]}, + next=(), + config={...}, metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}}, 'step': 1, 'parents': {}, 'thread_id': '1'}, created_at='2025-05-05T16:01:23.862295+00:00', parent_config={...} @@ -725,34 +1507,79 @@ list(graph.get_state_history(config)) interrupts=() ), StateSnapshot( - values={'messages': [HumanMessage(content="hi! I'm bob")]}, - next=('call_model',), - config={...}, - metadata={'source': 'loop', 'writes': None, 'step': 0, 'parents': {}, 'thread_id': '1'}, - created_at='2025-05-05T16:01:22.278960+00:00', + values={'messages': [HumanMessage(content="hi! I'm bob")]}, + next=('call_model',), + config={...}, + metadata={'source': 'loop', 'writes': None, 'step': 0, 'parents': {}, 'thread_id': '1'}, + created_at='2025-05-05T16:01:22.278960+00:00', parent_config={...} - tasks=(PregelTask(id='8cbd75e0-3720-b056-04f7-71ac805140a0', name='call_model', path=('__pregel_pull', 'call_model'), error=None, interrupts=(), state=None, result={'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}),), + tasks=(PregelTask(id='8cbd75e0-3720-b056-04f7-71ac805140a0', name='call_model', path=('__pregel_pull', 'call_model'), error=None, interrupts=(), state=None, result={'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}),), interrupts=() ), StateSnapshot( - values={'messages': []}, - next=('__start__',), + values={'messages': []}, + next=('__start__',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-0870-6ce2-bfff-1f3f14c3e565'}}, - metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}}, 'step': -1, 'parents': {}, 'thread_id': '1'}, - created_at='2025-05-05T16:01:22.277497+00:00', + metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}}, 'step': -1, 'parents': {}, 'thread_id': '1'}, + created_at='2025-05-05T16:01:22.277497+00:00', parent_config=None, - tasks=(PregelTask(id='d458367b-8265-812c-18e2-33001d199ce6', name='__start__', path=('__pregel_pull', '__start__'), error=None, interrupts=(), state=None, result={'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}),), + tasks=(PregelTask(id='d458367b-8265-812c-18e2-33001d199ce6', name='__start__', path=('__pregel_pull', '__start__'), error=None, interrupts=(), state=None, result={'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}),), interrupts=() ) -] +] ``` +::: + +:::js + +```typescript +const config = { + configurable: { + // highlight-next-line + thread_id: "1", + }, +}; +// highlight-next-line +const history = []; +for await (const state of graph.getStateHistory(config)) { + history.push(state); +} +``` + +``` +[ + StateSnapshot { + values: { + messages: [ + HumanMessage { content: "hi! I'm bob" }, + AIMessage { content: "Hi Bob! How are you doing today? Is there anything I can help you with?" }, + HumanMessage { content: "what's my name?" }, + AIMessage { content: "Your name is Bob." } + ] + }, + next: [], + config: { configurable: { thread_id: '1', checkpoint_ns: '', checkpoint_id: '1f029ca3-1f5b-6704-8004-820c16b69a5a' } }, + metadata: { source: 'loop', writes: { call_model: { messages: AIMessage { content: "Your name is Bob." } } }, step: 4, parents: {}, thread_id: '1' }, + createdAt: '2025-05-05T16:01:24.680462+00:00', + parentConfig: { configurable: { thread_id: '1', checkpoint_ns: '', checkpoint_id: '1f029ca3-1790-6b0a-8003-baf965b6a38f' } }, + tasks: [], + interrupts: [] + }, + // ... more state snapshots +] +``` + +::: + ### Decouple return value from saved value Use `entrypoint.final` to decouple what is returned to the caller from what is persisted in the checkpoint. This is useful when: -* You want to return a computed result (e.g., a summary or status), but save a different internal value for use on the next invocation. -* You need to control what gets passed to the previous parameter on the next run. +- You want to return a computed result (e.g., a summary or status), but save a different internal value for use on the next invocation. +- You need to control what gets passed to the previous parameter on the next run. + +:::python ```python from typing import Optional @@ -775,11 +1602,41 @@ print(accumulate.invoke(2, config=config)) # 1 print(accumulate.invoke(3, config=config)) # 3 ``` +::: + +:::js + +```typescript +import { entrypoint, MemorySaver } from "@langchain/langgraph"; + +const checkpointer = new MemorySaver(); + +const accumulate = entrypoint( + { checkpointer, name: "accumulate" }, + async (n: number, previous?: number) => { + const prev = previous || 0; + const total = prev + n; + // Return the *previous* value to the caller but save the *new* total to the checkpoint. + return entrypoint.final({ value: prev, save: total }); + } +); + +const config = { configurable: { thread_id: "my-thread" } }; + +console.log(await accumulate.invoke(1, config)); // 0 +console.log(await accumulate.invoke(2, config)); // 1 +console.log(await accumulate.invoke(3, config)); // 3 +``` + +::: + ### Chatbot example An example of a simple chatbot using the functional API and the `InMemorySaver` checkpointer. The bot is able to remember the previous conversation and continue from where it left off. +:::python + ```python from langchain_core.messages import BaseMessage from langgraph.graph import add_messages @@ -814,6 +1671,72 @@ for chunk in workflow.stream([input_message], config, stream_mode="values"): chunk.pretty_print() ``` +::: + +:::js + +```typescript +import { BaseMessage } from "@langchain/core/messages"; +import { + addMessages, + entrypoint, + task, + MemorySaver, +} from "@langchain/langgraph"; +import { ChatAnthropic } from "@langchain/anthropic"; + +const model = new ChatAnthropic({ model: "claude-3-5-sonnet-latest" }); + +const callModel = task( + "callModel", + async (messages: BaseMessage[]): Promise<BaseMessage> => { + const response = await model.invoke(messages); + return response; + } +); + +const checkpointer = new MemorySaver(); + +const workflow = entrypoint( + { checkpointer, name: "workflow" }, + async ( + inputs: BaseMessage[], + previous?: BaseMessage[] + ): Promise<BaseMessage> => { + let messages = inputs; + if (previous) { + messages = addMessages(previous, inputs); + } + + const response = await callModel(messages); + return entrypoint.final({ + value: response, + save: addMessages(messages, response), + }); + } +); + +const config = { configurable: { thread_id: "1" } }; +const inputMessage = { role: "user", content: "hi! I'm bob" }; + +for await (const chunk of await workflow.stream([inputMessage], { + ...config, + streamMode: "values", +})) { + console.log(chunk.content); +} + +const inputMessage2 = { role: "user", content: "what's my name?" }; +for await (const chunk of await workflow.stream([inputMessage2], { + ...config, + streamMode: "values", +})) { + console.log(chunk.content); +} +``` + +::: + ??? example "Extended example: build a simple chatbot" [How to add thread-level persistence (functional API)](./persistence-functional.ipynb): Shows how to add thread-level persistence to a functional API workflow and implements a simple chatbot. @@ -822,21 +1745,20 @@ for chunk in workflow.stream([input_message], config, stream_mode="values"): [long-term memory](../concepts/memory.md#long-term-memory) allows storing information across different **thread ids**. This could be useful for learning information about a given user in one conversation and using it in another. - ??? example "Extended example: add long-term memory" [How to add cross-thread persistence (functional API)](./cross-thread-persistence-functional.ipynb): Shows how to add cross-thread persistence to a functional API workflow and implements a simple chatbot. ## Workflows -* [Workflows and agent](../tutorials/workflows.md) guide for more examples of how to build workflows using the Functional API. +- [Workflows and agent](../tutorials/workflows.md) guide for more examples of how to build workflows using the Functional API. ## Agents -* [How to create an agent from scratch (Functional API)](./react-agent-from-scratch-functional.ipynb): Shows how to create a simple agent from scratch using the functional API. -* [How to build a multi-agent network](./multi-agent-network-functional.ipynb): Shows how to build a multi-agent network using the functional API. -* [How to add multi-turn conversation in a multi-agent application (functional API)](./multi-agent-multi-turn-convo-functional.ipynb): allow an end-user to engage in a multi-turn conversation with one or more agents. +- [How to create an agent from scratch (Functional API)](./react-agent-from-scratch-functional.ipynb): Shows how to create a simple agent from scratch using the functional API. +- [How to build a multi-agent network](./multi-agent-network-functional.ipynb): Shows how to build a multi-agent network using the functional API. +- [How to add multi-turn conversation in a multi-agent application (functional API)](./multi-agent-multi-turn-convo-functional.ipynb): allow an end-user to engage in a multi-turn conversation with one or more agents. ## Integrate with other libraries -* [Add LangGraph's features to other frameworks using the functional API](./autogen-integration-functional.ipynb): Add LangGraph features like persistence, memory and streaming to other agent frameworks that do not provide them out of the box. +- [Add LangGraph's features to other frameworks using the functional API](./autogen-integration-functional.ipynb): Add LangGraph features like persistence, memory and streaming to other agent frameworks that do not provide them out of the box. diff --git a/docs/docs/how-tos/use-remote-graph.md b/docs/docs/how-tos/use-remote-graph.md index 819c46f4b..380e30bc3 100644 --- a/docs/docs/how-tos/use-remote-graph.md +++ b/docs/docs/how-tos/use-remote-graph.md @@ -1,6 +1,7 @@ # How to interact with the deployment using RemoteGraph !!! info "Prerequisites" + - [LangGraph Platform](../concepts/langgraph_platform.md) - [LangGraph Server](../concepts/langgraph_server.md) @@ -8,9 +9,11 @@ ## Initializing the graph +:::python + When initializing a `RemoteGraph`, you must always specify: -- `name`: the name of the graph you want to interact with. This is the same graph name you use in `langgraph.json` configuration file for your deployment. +- `name`: the name of the graph you want to interact with. This is the same graph name you use in `langgraph.json` configuration file for your deployment. - `api_key`: a valid LangSmith API key. Can be set as an environment variable (`LANGSMITH_API_KEY`) or passed directly via the `api_key` argument. The API key could also be provided via the `client` / `sync_client` arguments, if `LangGraphClient` / `SyncLangGraphClient` were initialized with `api_key` argument. Additionally, you have to provide one of the following: @@ -23,57 +26,81 @@ Additionally, you have to provide one of the following: If you pass both `client` or `sync_client` as well as `url` argument, they will take precedence over the `url` argument. If none of the `client` / `sync_client` / `url` arguments are provided, `RemoteGraph` will raise a `ValueError` at runtime. +::: + +:::js + +When initializing a `RemoteGraph`, you must always specify: + +- `name`: the name of the graph you want to interact with. This is the same graph name you use in `langgraph.json` configuration file for your deployment. +- `apiKey`: a valid LangSmith API key. Can be set as an environment variable (`LANGSMITH_API_KEY`) or passed directly via the `apiKey` argument. The API key could also be provided via the `client`if `LangGraphClient` were initialized with `apiKey` argument. + +Additionally, you have to provide one of the following: + +- `url`: URL of the deployment you want to interact with. If you pass `url` argument, both sync and async clients will be created using the provided URL, headers (if provided) and default configuration values (e.g. timeout, etc). +- `client`: a `LangGraphClient` instance for interacting with the deployment asynchronously + +::: ### Using URL -=== "Python" +:::python - ```python - from langgraph.pregel.remote import RemoteGraph +```python +from langgraph.pregel.remote import RemoteGraph - url = <DEPLOYMENT_URL> - graph_name = "agent" - remote_graph = RemoteGraph(graph_name, url=url) - ``` +url = <DEPLOYMENT_URL> +graph_name = "agent" +remote_graph = RemoteGraph(graph_name, url=url) +``` -=== "JavaScript" +::: - ```ts - import { RemoteGraph } from "@langchain/langgraph/remote"; +:::js - const url = `<DEPLOYMENT_URL>`; - const graphName = "agent"; - const remoteGraph = new RemoteGraph({ graphId: graphName, url }); - ``` +```ts +import { RemoteGraph } from "@langchain/langgraph/remote"; + +const url = `<DEPLOYMENT_URL>`; +const graphName = "agent"; +const remoteGraph = new RemoteGraph({ graphId: graphName, url }); +``` + +::: ### Using clients -=== "Python" +:::python - ```python - from langgraph_sdk import get_client, get_sync_client - from langgraph.pregel.remote import RemoteGraph +```python +from langgraph_sdk import get_client, get_sync_client +from langgraph.pregel.remote import RemoteGraph - url = <DEPLOYMENT_URL> - graph_name = "agent" - client = get_client(url=url) - sync_client = get_sync_client(url=url) - remote_graph = RemoteGraph(graph_name, client=client, sync_client=sync_client) - ``` +url = <DEPLOYMENT_URL> +graph_name = "agent" +client = get_client(url=url) +sync_client = get_sync_client(url=url) +remote_graph = RemoteGraph(graph_name, client=client, sync_client=sync_client) +``` -=== "JavaScript" +::: - ```ts - import { Client } from "@langchain/langgraph-sdk"; - import { RemoteGraph } from "@langchain/langgraph/remote"; +:::js - const client = new Client({ apiUrl: `<DEPLOYMENT_URL>` }); - const graphName = "agent"; - const remoteGraph = new RemoteGraph({ graphId: graphName, client }); - ``` +```ts +import { Client } from "@langchain/langgraph-sdk"; +import { RemoteGraph } from "@langchain/langgraph/remote"; + +const client = new Client({ apiUrl: `<DEPLOYMENT_URL>` }); +const graphName = "agent"; +const remoteGraph = new RemoteGraph({ graphId: graphName, client }); +``` + +::: ## Invoking the graph +:::python Since `RemoteGraph` is a `Runnable` that implements the same methods as `CompiledGraph`, you can interact with it the same way you normally would with a compiled graph, i.e. by calling `.invoke()`, `.stream()`, `.get_state()`, `.update_state()`, etc (as well as their async counterparts). ### Asynchronously @@ -82,35 +109,18 @@ Since `RemoteGraph` is a `Runnable` that implements the same methods as `Compile To use the graph asynchronously, you must provide either the `url` or `client` when initializing the `RemoteGraph`. -=== "Python" +```python +# invoke the graph +result = await remote_graph.ainvoke({ + "messages": [{"role": "user", "content": "what's the weather in sf"}] +}) - ```python - # invoke the graph - result = await remote_graph.ainvoke({ - "messages": [{"role": "user", "content": "what's the weather in sf"}] - }) - - # stream outputs from the graph - async for chunk in remote_graph.astream({ - "messages": [{"role": "user", "content": "what's the weather in la"}] - }): - print(chunk) - ``` - -=== "JavaScript" - - ```ts - // invoke the graph - const result = await remoteGraph.invoke({ - messages: [{role: "user", content: "what's the weather in sf"}] - }) - - // stream outputs from the graph - for await (const chunk of await remoteGraph.stream({ - messages: [{role: "user", content: "what's the weather in la"}] - })): - console.log(chunk) - ``` +# stream outputs from the graph +async for chunk in remote_graph.astream({ + "messages": [{"role": "user", "content": "what's the weather in la"}] +}): + print(chunk) +``` ### Synchronously @@ -118,72 +128,97 @@ Since `RemoteGraph` is a `Runnable` that implements the same methods as `Compile To use the graph synchronously, you must provide either the `url` or `sync_client` when initializing the `RemoteGraph`. -=== "Python" +```python +# invoke the graph +result = remote_graph.invoke({ + "messages": [{"role": "user", "content": "what's the weather in sf"}] +}) - ```python - # invoke the graph - result = remote_graph.invoke({ - "messages": [{"role": "user", "content": "what's the weather in sf"}] - }) +# stream outputs from the graph +for chunk in remote_graph.stream({ + "messages": [{"role": "user", "content": "what's the weather in la"}] +}): + print(chunk) +``` - # stream outputs from the graph - for chunk in remote_graph.stream({ - "messages": [{"role": "user", "content": "what's the weather in la"}] - }): - print(chunk) - ``` +::: + +:::js +Since `RemoteGraph` is a `Runnable` that implements the same methods as `CompiledGraph`, you can interact with it the same way you normally would with a compiled graph, i.e. by calling `.invoke()`, `.stream()`, `.getState()`, `.updateState()`, etc. + +```ts +// invoke the graph +const result = await remoteGraph.invoke({ + messages: [{role: "user", content: "what's the weather in sf"}] +}) + +// stream outputs from the graph +for await (const chunk of await remoteGraph.stream({ + messages: [{role: "user", content: "what's the weather in la"}] +})): + console.log(chunk) +``` + +::: ## Thread-level persistence By default, the graph runs (i.e. `.invoke()` or `.stream()` invocations) are stateless - the checkpoints and the final state of the graph are not persisted. If you would like to persist the outputs of the graph run (for example, to enable human-in-the-loop features), you can create a thread and provide the thread ID via the `config` argument, same as you would with a regular compiled graph: -=== "Python" +:::python - ```python - from langgraph_sdk import get_sync_client - url = <DEPLOYMENT_URL> - graph_name = "agent" - sync_client = get_sync_client(url=url) - remote_graph = RemoteGraph(graph_name, url=url) +```python +from langgraph_sdk import get_sync_client +url = <DEPLOYMENT_URL> +graph_name = "agent" +sync_client = get_sync_client(url=url) +remote_graph = RemoteGraph(graph_name, url=url) - # create a thread (or use an existing thread instead) - thread = sync_client.threads.create() +# create a thread (or use an existing thread instead) +thread = sync_client.threads.create() - # invoke the graph with the thread config - config = {"configurable": {"thread_id": thread["thread_id"]}} - result = remote_graph.invoke({ - "messages": [{"role": "user", "content": "what's the weather in sf"}] - }, config=config) +# invoke the graph with the thread config +config = {"configurable": {"thread_id": thread["thread_id"]}} +result = remote_graph.invoke({ + "messages": [{"role": "user", "content": "what's the weather in sf"}] +}, config=config) - # verify that the state was persisted to the thread - thread_state = remote_graph.get_state(config) - print(thread_state) - ``` +# verify that the state was persisted to the thread +thread_state = remote_graph.get_state(config) +print(thread_state) +``` -=== "JavaScript" +::: - ```ts - import { Client } from "@langchain/langgraph-sdk"; - import { RemoteGraph } from "@langchain/langgraph/remote"; +:::js - const url = `<DEPLOYMENT_URL>`; - const graphName = "agent"; - const client = new Client({ apiUrl: url }); - const remoteGraph = new RemoteGraph({ graphId: graphName, url }); +```ts +import { Client } from "@langchain/langgraph-sdk"; +import { RemoteGraph } from "@langchain/langgraph/remote"; - // create a thread (or use an existing thread instead) - const thread = await client.threads.create(); +const url = `<DEPLOYMENT_URL>`; +const graphName = "agent"; +const client = new Client({ apiUrl: url }); +const remoteGraph = new RemoteGraph({ graphId: graphName, url }); - // invoke the graph with the thread config - const config = { configurable: { thread_id: thread.thread_id }}; - const result = await remoteGraph.invoke({ - messages: [{ role: "user", content: "what's the weather in sf" }], - }, config); +// create a thread (or use an existing thread instead) +const thread = await client.threads.create(); - // verify that the state was persisted to the thread - const threadState = await remoteGraph.getState(config); - console.log(threadState); - ``` +// invoke the graph with the thread config +const config = { configurable: { thread_id: thread.thread_id } }; +const result = await remoteGraph.invoke( + { + messages: [{ role: "user", content: "what's the weather in sf" }], + }, + config +); + +// verify that the state was persisted to the thread +const threadState = await remoteGraph.getState(config); +console.log(threadState); +``` + +::: ## Using as a subgraph @@ -191,66 +226,72 @@ By default, the graph runs (i.e. `.invoke()` or `.stream()` invocations) are sta If you need to use a `checkpointer` with a graph that has a `RemoteGraph` subgraph node, make sure to use UUIDs as thread IDs. - Since the `RemoteGraph` behaves the same way as a regular `CompiledGraph`, it can be also used as a subgraph in another graph. For example: -=== "Python" +:::python - ```python - from langgraph_sdk import get_sync_client - from langgraph.graph import StateGraph, MessagesState, START - from typing import TypedDict +```python +from langgraph_sdk import get_sync_client +from langgraph.graph import StateGraph, MessagesState, START +from typing import TypedDict - url = <DEPLOYMENT_URL> - graph_name = "agent" - remote_graph = RemoteGraph(graph_name, url=url) +url = <DEPLOYMENT_URL> +graph_name = "agent" +remote_graph = RemoteGraph(graph_name, url=url) - # define parent graph - builder = StateGraph(MessagesState) - # add remote graph directly as a node - builder.add_node("child", remote_graph) - builder.add_edge(START, "child") - graph = builder.compile() +# define parent graph +builder = StateGraph(MessagesState) +# add remote graph directly as a node +builder.add_node("child", remote_graph) +builder.add_edge(START, "child") +graph = builder.compile() - # invoke the parent graph - result = graph.invoke({ - "messages": [{"role": "user", "content": "what's the weather in sf"}] - }) - print(result) +# invoke the parent graph +result = graph.invoke({ + "messages": [{"role": "user", "content": "what's the weather in sf"}] +}) +print(result) - # stream outputs from both the parent graph and subgraph - for chunk in graph.stream({ - "messages": [{"role": "user", "content": "what's the weather in sf"}] - }, subgraphs=True): - print(chunk) - ``` +# stream outputs from both the parent graph and subgraph +for chunk in graph.stream({ + "messages": [{"role": "user", "content": "what's the weather in sf"}] +}, subgraphs=True): + print(chunk) +``` -=== "JavaScript" +::: - ```ts - import { MessagesAnnotation, StateGraph, START } from "@langchain/langgraph"; - import { RemoteGraph } from "@langchain/langgraph/remote"; +:::js - const url = `<DEPLOYMENT_URL>`; - const graphName = "agent"; - const remoteGraph = new RemoteGraph({ graphId: graphName, url }); +```ts +import { MessagesAnnotation, StateGraph, START } from "@langchain/langgraph"; +import { RemoteGraph } from "@langchain/langgraph/remote"; - // define parent graph and add remote graph directly as a node - const graph = new StateGraph(MessagesAnnotation) - .addNode("child", remoteGraph) - .addEdge(START, "child") - .compile() +const url = `<DEPLOYMENT_URL>`; +const graphName = "agent"; +const remoteGraph = new RemoteGraph({ graphId: graphName, url }); - // invoke the parent graph - const result = await graph.invoke({ - messages: [{ role: "user", content: "what's the weather in sf" }] - }); - console.log(result); +// define parent graph and add remote graph directly as a node +const graph = new StateGraph(MessagesAnnotation) + .addNode("child", remoteGraph) + .addEdge(START, "child") + .compile(); - // stream outputs from both the parent graph and subgraph - for await (const chunk of await graph.stream({ - messages: [{ role: "user", content: "what's the weather in la" }] - }, { subgraphs: true })) { - console.log(chunk); - } - ``` \ No newline at end of file +// invoke the parent graph +const result = await graph.invoke({ + messages: [{ role: "user", content: "what's the weather in sf" }], +}); +console.log(result); + +// stream outputs from both the parent graph and subgraph +for await (const chunk of await graph.stream( + { + messages: [{ role: "user", content: "what's the weather in la" }], + }, + { subgraphs: true } +)) { + console.log(chunk); +} +``` + +::: diff --git a/docs/docs/troubleshooting/errors/GRAPH_RECURSION_LIMIT.md b/docs/docs/troubleshooting/errors/GRAPH_RECURSION_LIMIT.md index 332294d16..5a2c9bed2 100644 --- a/docs/docs/troubleshooting/errors/GRAPH_RECURSION_LIMIT.md +++ b/docs/docs/troubleshooting/errors/GRAPH_RECURSION_LIMIT.md @@ -3,6 +3,8 @@ Your LangGraph [`StateGraph`](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.state.StateGraph) reached the maximum number of steps before hitting a stop condition. This is often due to an infinite loop caused by code like the example below: +:::python + ```python class State(TypedDict): some_key: str @@ -17,13 +19,52 @@ builder.add_edge("b", "a") graph = builder.compile() ``` +::: + +:::js + +```typescript +import { StateGraph } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ + someKey: z.string(), +}); + +const builder = new StateGraph(State) + .addNode("a", ...) + .addNode("b", ...) + .addEdge("a", "b") + .addEdge("b", "a") + ... + +const graph = builder.compile(); +``` + +::: + However, complex graphs may hit the default limit naturally. ## Troubleshooting - If you are not expecting your graph to go through many iterations, you likely have a cycle. Check your logic for infinite loops. + +:::python + - If you have a complex graph, you can pass in a higher `recursion_limit` value into your `config` object when invoking your graph like this: ```python graph.invoke({...}, {"recursion_limit": 100}) -``` \ No newline at end of file +``` + +::: + +:::js + +- If you have a complex graph, you can pass in a higher `recursionLimit` value into your `config` object when invoking your graph like this: + +```typescript +await graph.invoke({...}, { recursionLimit: 100 }); +``` + +::: diff --git a/docs/docs/troubleshooting/errors/INVALID_CHAT_HISTORY.md b/docs/docs/troubleshooting/errors/INVALID_CHAT_HISTORY.md index 7582dd515..e3089d5d1 100644 --- a/docs/docs/troubleshooting/errors/INVALID_CHAT_HISTORY.md +++ b/docs/docs/troubleshooting/errors/INVALID_CHAT_HISTORY.md @@ -1,16 +1,42 @@ # INVALID_CHAT_HISTORY -This error is raised in the prebuilt [create_react_agent][langgraph.prebuilt.chat_agent_executor.create_react_agent] when the `call_model` graph node receives a malformed list of messages. Specifically, it is malformed when there are `AIMessages` with `tool_calls` (LLM requesting to call a tool) that do not have a corresponding `ToolMessage` (result of a tool invocation to return to the LLM). +:::python +This error is raised in the prebuilt @[create_react_agent][create_react_agent] when the `call_model` graph node receives a malformed list of messages. Specifically, it is malformed when there are `AIMessages` with `tool_calls` (LLM requesting to call a tool) that do not have a corresponding `ToolMessage` (result of a tool invocation to return to the LLM). +::: + +:::js +This error is raised in the prebuilt @[createReactAgent][create_react_agent] when the `callModel` graph node receives a malformed list of messages. Specifically, it is malformed when there are `AIMessage`s with `tool_calls` (LLM requesting to call a tool) that do not have a corresponding `ToolMessage` (result of a tool invocation to return to the LLM). +::: There could be a few reasons you're seeing this error: +:::python + 1. You manually passed a malformed list of messages when invoking the graph, e.g. `graph.invoke({'messages': [AIMessage(..., tool_calls=[...])]})` 2. The graph was interrupted before receiving updates from the `tools` node (i.e. a list of ToolMessages) -and you invoked it with an input that is not None or a ToolMessage, -e.g. `graph.invoke({'messages': [HumanMessage(...)]}, config)`. - This interrupt could have been triggered in one of the following ways: - - You manually set `interrupt_before = ['tools']` in `create_react_agent` - - One of the tools raised an error that wasn't handled by the [ToolNode][langgraph.prebuilt.tool_node.ToolNode] (`"tools"`) + and you invoked it with an input that is not None or a ToolMessage, + e.g. `graph.invoke({'messages': [HumanMessage(...)]}, config)`. + + This interrupt could have been triggered in one of the following ways: + + - You manually set `interrupt_before = ['tools']` in `create_react_agent` + - One of the tools raised an error that wasn't handled by the @[ToolNode][ToolNode] (`"tools"`) + +::: + +:::js + +1. You manually passed a malformed list of messages when invoking the graph, e.g. `graph.invoke({messages: [new AIMessage({..., tool_calls: [...]})]})` +2. The graph was interrupted before receiving updates from the `tools` node (i.e. a list of ToolMessages) + and you invoked it with an input that is not null or a ToolMessage, + e.g. `graph.invoke({messages: [new HumanMessage(...)]}, config)`. + + This interrupt could have been triggered in one of the following ways: + + - You manually set `interruptBefore: ['tools']` in `createReactAgent` + - One of the tools raised an error that wasn't handled by the [ToolNode][ToolNode] (`"tools"`) + +::: ## Troubleshooting @@ -19,12 +45,28 @@ To resolve this, you can do one of the following: 1. Don't invoke the graph with a malformed list of messages 2. In case of an interrupt (manual or due to an error) you can: - - provide ToolMessages that match existing tool calls and call `graph.invoke({'messages': [ToolMessage(...)]})`. - **NOTE**: this will append the messages to the history and run the graph from the START node. - - manually update the state and resume the graph from the interrupt: +:::python - 1. get the list of most recent messages from the graph state with `graph.get_state(config)` - 2. modify the list of messages to either remove unanswered tool calls from AIMessages -or add ToolMessages with tool_call_ids that match unanswered tool calls - 3. call `graph.update_state(config, {'messages': ...})` with the modified list of messages - 4. resume the graph, e.g. call `graph.invoke(None, config)` +- provide ToolMessages that match existing tool calls and call `graph.invoke({'messages': [ToolMessage(...)]})`. + **NOTE**: this will append the messages to the history and run the graph from the START node. + + - manually update the state and resume the graph from the interrupt: + + 1. get the list of most recent messages from the graph state with `graph.get_state(config)` + 2. modify the list of messages to either remove unanswered tool calls from AIMessages + +or add ToolMessages with tool_call_ids that match unanswered tool calls 3. call `graph.update_state(config, {'messages': ...})` with the modified list of messages 4. resume the graph, e.g. call `graph.invoke(None, config)` +::: + +:::js + +- provide ToolMessages that match existing tool calls and call `graph.invoke({messages: [new ToolMessage(...)]})`. + **NOTE**: this will append the messages to the history and run the graph from the START node. + + - manually update the state and resume the graph from the interrupt: + + 1. get the list of most recent messages from the graph state with `graph.getState(config)` + 2. modify the list of messages to either remove unanswered tool calls from AIMessages + +or add ToolMessages with `toolCallId`s that match unanswered tool calls 3. call `graph.updateState(config, {messages: ...})` with the modified list of messages 4. resume the graph, e.g. call `graph.invoke(null, config)` +::: diff --git a/docs/docs/troubleshooting/errors/INVALID_CONCURRENT_GRAPH_UPDATE.md b/docs/docs/troubleshooting/errors/INVALID_CONCURRENT_GRAPH_UPDATE.md index c87a1ce49..a5d200448 100644 --- a/docs/docs/troubleshooting/errors/INVALID_CONCURRENT_GRAPH_UPDATE.md +++ b/docs/docs/troubleshooting/errors/INVALID_CONCURRENT_GRAPH_UPDATE.md @@ -6,6 +6,8 @@ support it. One way this can occur is if you are using a [fanout](https://langchain-ai.github.io/langgraph/how-tos/map-reduce/) or other parallel execution in your graph and you have defined a graph like this: +:::python + ```python hl_lines="2" class State(TypedDict): some_key: str @@ -25,12 +27,49 @@ builder.add_edge(START, "other_node") graph = builder.compile() ``` +::: + +:::js + +```typescript hl_lines="2" +import { StateGraph, Annotation, START } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ + someKey: z.string(), +}); + +const builder = new StateGraph(State) + .addNode("node", (state) => { + return { someKey: "some_string_value" }; + }) + .addNode("otherNode", (state) => { + return { someKey: "some_string_value" }; + }) + .addEdge(START, "node") + .addEdge(START, "otherNode"); + +const graph = builder.compile(); +``` + +::: + +:::python If a node in the above graph returns `{ "some_key": "some_string_value" }`, this will overwrite the state value for `"some_key"` with `"some_string_value"`. However, if multiple nodes in e.g. a fanout within a single step return values for `"some_key"`, the graph will throw this error because there is uncertainty around how to update the internal state. +::: + +:::js +If a node in the above graph returns `{ someKey: "some_string_value" }`, this will overwrite the state value for `someKey` with `"some_string_value"`. +However, if multiple nodes in e.g. a fanout within a single step return values for `someKey`, the graph will throw this error because +there is uncertainty around how to update the internal state. +::: To get around this, you can define a reducer that combines multiple values: +:::python + ```python hl_lines="5-6" import operator from typing import Annotated @@ -40,10 +79,30 @@ class State(TypedDict): some_key: Annotated[list, operator.add] ``` +::: + +:::js + +```typescript hl_lines="4-7" +import { withLangGraph } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ + someKey: withLangGraph(z.array(z.string()), { + reducer: { + fn: (existing, update) => existing.concat(update), + }, + default: () => [], + }), +}); +``` + +::: + This will allow you to define logic that handles the same key returned from multiple nodes executed in parallel. ## Troubleshooting The following may help resolve this error: -- If your graph executes nodes in parallel, make sure you have defined relevant state keys with a reducer. \ No newline at end of file +- If your graph executes nodes in parallel, make sure you have defined relevant state keys with a reducer. diff --git a/docs/docs/troubleshooting/errors/INVALID_GRAPH_NODE_RETURN_VALUE.md b/docs/docs/troubleshooting/errors/INVALID_GRAPH_NODE_RETURN_VALUE.md index 41d4fb4a7..5fbcca2b7 100644 --- a/docs/docs/troubleshooting/errors/INVALID_GRAPH_NODE_RETURN_VALUE.md +++ b/docs/docs/troubleshooting/errors/INVALID_GRAPH_NODE_RETURN_VALUE.md @@ -1,5 +1,6 @@ # INVALID_GRAPH_NODE_RETURN_VALUE +:::python A LangGraph [`StateGraph`](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.state.StateGraph) received a non-dict return type from a node. Here's an example: @@ -30,9 +31,55 @@ For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/er ``` Nodes in your graph must return a dict containing one or more keys defined in your state. +::: + +:::js +A LangGraph [`StateGraph`](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.state.StateGraph) +received a non-object return type from a node. Here's an example: + +```typescript +import { z } from "zod"; +import { StateGraph } from "@langchain/langgraph"; + +const State = z.object({ + someKey: z.string(), +}); + +const badNode = (state: z.infer<typeof State>) => { + // Should return an object with a value for "someKey", not an array + return ["whoops"]; +}; + +const builder = new StateGraph(State).addNode("badNode", badNode); +// ... + +const graph = builder.compile(); +``` + +Invoking the above graph will result in an error like this: + +```typescript +await graph.invoke({ someKey: "someval" }); +``` + +``` +InvalidUpdateError: Expected object, got ['whoops'] +For troubleshooting, visit: https://langchain-ai.github.io/langgraphjs/troubleshooting/errors/INVALID_GRAPH_NODE_RETURN_VALUE +``` + +Nodes in your graph must return an object containing one or more keys defined in your state. +::: ## Troubleshooting The following may help resolve this error: -- If you have complex logic in your node, make sure all code paths return an appropriate dict for your defined state. \ No newline at end of file +:::python + +- If you have complex logic in your node, make sure all code paths return an appropriate dict for your defined state. + ::: + +:::js + +- If you have complex logic in your node, make sure all code paths return an appropriate object for your defined state. + ::: diff --git a/docs/docs/troubleshooting/errors/MULTIPLE_SUBGRAPHS.md b/docs/docs/troubleshooting/errors/MULTIPLE_SUBGRAPHS.md index f14902a9b..e7be3badd 100644 --- a/docs/docs/troubleshooting/errors/MULTIPLE_SUBGRAPHS.md +++ b/docs/docs/troubleshooting/errors/MULTIPLE_SUBGRAPHS.md @@ -8,5 +8,14 @@ This is currently not allowed due to internal restrictions on how checkpoint nam The following may help resolve this error: +:::python + - If you don't need to interrupt/resume from a subgraph, pass `checkpointer=False` when compiling it like this: `.compile(checkpointer=False)` + ::: + +:::js + +- If you don't need to interrupt/resume from a subgraph, pass `checkpointer: false` when compiling it like this: `.compile({ checkpointer: false })` + ::: + - Don't imperatively call graphs multiple times in the same node, and instead use the [`Send`](https://langchain-ai.github.io/langgraph/concepts/low_level/#send) API. diff --git a/docs/docs/troubleshooting/studio.md b/docs/docs/troubleshooting/studio.md index 3fcbdea7f..ea1925a3e 100644 --- a/docs/docs/troubleshooting/studio.md +++ b/docs/docs/troubleshooting/studio.md @@ -6,19 +6,22 @@ Safari blocks plain-HTTP traffic on localhost. When running Studio with `langgra ### Solution 1: Use Cloudflare Tunnel -=== "Python" +:::python - ```shell - pip install -U langgraph-cli>=0.2.6 - langgraph dev --tunnel - ``` +```shell +pip install -U langgraph-cli>=0.2.6 +langgraph dev --tunnel +``` -=== "JS" +::: - ```shell - # Requires @langchain/langgraph-cli>=0.0.26 - npx @langchain/langgraph-cli dev --tunnel - ``` +:::js + +```shell +npx @langchain/langgraph-cli dev +``` + +::: The command outputs a URL in this format: @@ -44,19 +47,22 @@ Disable Brave Shields for LangSmith using the Brave icon in the URL bar. ### Solution 2: Use Cloudflare Tunnel -=== "Python" +:::python - ```shell - pip install -U langgraph-cli>=0.2.6 - langgraph dev --tunnel - ``` +```shell +pip install -U langgraph-cli>=0.2.6 +langgraph dev --tunnel +``` -=== "JS" +::: - ```shell - # Requires @langchain/langgraph-cli>=0.0.26 - npx @langchain/langgraph-cli dev --tunnel - ``` +:::js + +```shell +npx @langchain/langgraph-cli dev +``` + +::: The command outputs a URL in this format: @@ -68,6 +74,7 @@ Use this URL in Brave to load Studio. Here, the `baseUrl` parameter specifies yo ## Graph Edge Issues +:::python Undefined conditional edges may show unexpected connections in your graph. This is because without proper definition, LangGraph Studio assumes the conditional edge could access all other nodes. To address this, explicitly define the routing paths using one of these methods: @@ -75,17 +82,9 @@ because without proper definition, LangGraph Studio assumes the conditional edge Define a mapping between router outputs and target nodes: -=== "Python" - - ```python - graph.add_conditional_edges("node_a", routing_function, {True: "node_b", False: "node_c"}) - ``` - -=== "Javascript" - - ```ts - graph.addConditionalEdges("node_a", routingFunction, { true: "node_b", false: "node_c" }); - ``` +```python +graph.add_conditional_edges("node_a", routing_function, {True: "node_b", False: "node_c"}) +``` ### Solution 2: Router Type Definition (Python) @@ -98,3 +97,18 @@ def routing_function(state: GraphState) -> Literal["node_b","node_c"]: else: return "node_c" ``` + +::: + +:::js +Undefined conditional edges may show unexpected connections in your graph. This is because without proper definition, LangGraph Studio assumes the conditional edge could access all other nodes. +To address this, explicitly define a mapping between router outputs and target nodes: + +```typescript +graph.addConditionalEdges("node_a", routingFunction, { + true: "node_b", + false: "node_c", +}); +``` + +::: diff --git a/docs/docs/tutorials/auth/add_auth_server.md b/docs/docs/tutorials/auth/add_auth_server.md index 3ab37bd6b..038653cfa 100644 --- a/docs/docs/tutorials/auth/add_auth_server.md +++ b/docs/docs/tutorials/auth/add_auth_server.md @@ -2,7 +2,13 @@ In [the last tutorial](resource_auth.md), you added [resource authorization](../../tutorials/auth/resource_auth.md) to give users private conversations. However, you are still using hard-coded tokens for authentication, which is not secure. Now you'll replace those tokens with real user accounts using [OAuth2](../auth/getting_started.md). +:::python You'll keep the same [`Auth`](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth) object and [resource-level access control](../../concepts/auth.md#single-owner-resources), but upgrade authentication to use Supabase as your identity provider. While Supabase is used in this tutorial, the concepts apply to any OAuth2 provider. You'll learn how to: +::: + +:::js +You'll keep the same [`Auth`](../../cloud/reference/sdk/typescript_sdk_ref.md#auth) object and [resource-level access control](../../concepts/auth.md#single-owner-resources), but upgrade authentication to use Supabase as your identity provider. While Supabase is used in this tutorial, the concepts apply to any OAuth2 provider. You'll learn how to: +::: 1. Replace test tokens with real JWT tokens 2. Integrate with OAuth2 providers for secure user authentication @@ -18,7 +24,6 @@ OAuth2 involves three main roles: A standard OAuth2 flow works something like this: - ```mermaid sequenceDiagram participant User @@ -40,35 +45,49 @@ sequenceDiagram Before you start this tutorial, ensure you have: - The [bot from the second tutorial](resource_auth.md) running without errors. -- A [Supabase project](https://supabase.com/dashboard) to use its authentication server. - +- A [Supabase project](https://supabase.com/dashboard) to use as your authentication server. ## 1. Install dependencies Install the required dependencies. Start in your `custom-auth` directory and ensure you have the `langgraph-cli` installed: +:::python + ```bash cd custom-auth pip install -U "langgraph-cli[inmem]" ``` +::: + +:::js + +```bash +cd custom-auth +npm install -g @langchain/langgraph-cli +``` + +::: + ## 2. Set up the authentication provider {#setup-auth-provider} Next, fetch the URL of your auth server and the private key for authentication. Since you're using Supabase for this, you can do this in the Supabase dashboard: -1. In the left sidebar, click on t️⚙ Project Settings" and then click "API" -1. Copy your project URL and add it to your `.env` file +1. In the left sidebar, click on "⚙️ Project Settings" and then click "API" +2. Copy your project URL and add it to your `.env` file ```shell echo "SUPABASE_URL=your-project-url" >> .env ``` -1. Copy your service role secret key and add it to your `.env` file: + +3. Copy your service role secret key and add it to your `.env` file: ```shell echo "SUPABASE_SERVICE_KEY=your-service-role-key" >> .env ``` -1. Copy your "anon public" key and note it down. This will be used later when you set up our client code. + +4. Copy your "anon public" key and note it down. This will be used later when you set up our client code. ```bash SUPABASE_URL=your-project-url @@ -77,14 +96,23 @@ Since you're using Supabase for this, you can do this in the Supabase dashboard: ## 3. Implement token validation +:::python In the previous tutorials, you used the [`Auth`](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth) object to [validate hard-coded tokens](getting_started.md) and [add resource ownership](resource_auth.md). Now you'll upgrade your authentication to validate real JWT tokens from Supabase. The main changes will all be in the [`@auth.authenticate`](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.authenticate) decorated function: +::: + +:::js +In the previous tutorials, you used the [`Auth`](../../cloud/reference/sdk/typescript_sdk_ref.md#auth) object to [validate hard-coded tokens](getting_started.md) and [add resource ownership](resource_auth.md). + +Now you'll upgrade your authentication to validate real JWT tokens from Supabase. The main changes will all be in the [`auth.authenticate`](../../cloud/reference/sdk/typescript_sdk_ref.md#auth) decorated function: +::: - Instead of checking against a hard-coded list of tokens, you'll make an HTTP request to Supabase to validate the token. - You'll extract real user information (ID, email) from the validated token. - The existing resource authorization logic remains unchanged. +:::python Update `src/security/auth.py` to implement this: ```python hl_lines="8-9 20-30" title="src/security/auth.py" @@ -138,6 +166,69 @@ async def add_owner(ctx, value): return filters ``` +::: + +:::js +Update `src/security/auth.ts` to implement this: + +```typescript hl_lines="1-2 9-10 21-31" title="src/security/auth.ts" +import { Auth } from "@langchain/langgraph-sdk"; + +// This is loaded from the `.env` file you created above +const SUPABASE_URL = process.env.SUPABASE_URL; +const SUPABASE_SERVICE_KEY = process.env.SUPABASE_SERVICE_KEY; + +const auth = new Auth() + .authenticate(async (request) => { + // Validate JWT tokens and extract user information. + const apiKey = request.headers.get("x-api-key"); + if (!apiKey || !isValidKey(apiKey)) { + throw new HTTPException(401, "Invalid API key"); + } + + const [scheme, token] = apiKey.split(" "); + if (scheme.toLowerCase() !== "bearer") { + throw new Error("Invalid authorization scheme"); + } + + try { + // Verify token with auth provider + const response = await fetch(`${SUPABASE_URL}/auth/v1/user`, { + headers: { + Authorization: authorization, + apiKey: SUPABASE_SERVICE_KEY!, + }, + }); + + if (response.status !== 200) { + throw new Error("Invalid token"); + } + + const user = await response.json(); + return { + identity: user.id, // Unique user identifier + email: user.email, + is_authenticated: true, + }; + } catch (e) { + throw new Auth.HTTPException(401, String(e)); + } + }) + .on(async ({ user, value }) => { + // Keep our resource authorization from the previous tutorial + // Make resources private to their creator using resource metadata. + const filters = { owner: user.identity }; + const metadata = value.metadata || {}; + Object.assign(metadata, filters); + value.metadata = metadata; + return filters; + }); + +export { auth }; +``` + +::: + The most important change is that we're now validating tokens with a real authentication server. Our authentication handler has the private key for our Supabase project, which we can use to validate the user's token and extract their information. ## 4. Test authentication flow @@ -148,6 +239,8 @@ Let's test out the new authentication flow. You can run the following code in a - A Supabase project URL (from [above](#setup-auth-provider)) - A Supabase anon **public key** (also from [above](#setup-auth-provider)) +:::python + ```python import os import httpx @@ -190,9 +283,63 @@ await sign_up(email1, password) await sign_up(email2, password) ``` +::: + +:::js + +```typescript +import { Client } from "@langchain/langgraph-sdk"; + +// Get email from command line +const email = process.env.TEST_EMAIL || "your-email@example.com"; +const baseEmail = email.split("@"); +const password = "secure-password"; // CHANGEME +const email1 = `${baseEmail[0]}+1@${baseEmail[1]}`; +const email2 = `${baseEmail[0]}+2@${baseEmail[1]}`; + +const SUPABASE_URL = process.env.SUPABASE_URL; +if (!SUPABASE_URL) { + throw new Error("SUPABASE_URL environment variable is required"); +} + +// This is your PUBLIC anon key (which is safe to use client-side) +// Do NOT mistake this for the secret service role key +const SUPABASE_ANON_KEY = process.env.SUPABASE_ANON_KEY; +if (!SUPABASE_ANON_KEY) { + throw new Error("SUPABASE_ANON_KEY environment variable is required"); +} + +async function signUp(email: string, password: string) { + /**Create a new user account.*/ + const response = await fetch(`${SUPABASE_URL}/auth/v1/signup`, { + method: "POST", + headers: { + apiKey: SUPABASE_ANON_KEY, + "Content-Type": "application/json", + }, + body: JSON.stringify({ email, password }), + }); + + if (response.status !== 200) { + throw new Error(`Failed to sign up: ${response.statusText}`); + } + + return response.json(); +} + +// Create two test users +console.log(`Creating test users: ${email1} and ${email2}`); +await signUp(email1, password); +await signUp(email2, password); +``` + +::: + ⚠️ Before continuing: Check your email and click both confirmation links. Supabase will reject `/login` requests until after you have confirmed your users' email. -Now test that users can only see their own data. Make sure the server is running (run `langgraph dev`) before proceeding. The following snippet requires the "anon public" key that you copied from the Supabase dashboard while [setting up the auth provider](#setup-auth-provider) previously. +Now test that users can only see their own data. Make sure the server is running (run `langgraph dev`) before proceeding. The following snippet requires the "anon public" key that you copied from the Supabase dashboard while [setting up the auth provider](#setup-auth-provider) previously. + +:::python ```python async def login(email: str, password: str): @@ -243,6 +390,71 @@ try: except Exception as e: print("✅ User 2 blocked from User 1's thread:", e) ``` + +::: + +:::js + +```typescript +async function login(email: string, password: string): Promise<string> { + /**Get an access token for an existing user.*/ + const response = await fetch( + `${SUPABASE_URL}/auth/v1/token?grant_type=password`, + { + method: "POST", + headers: { + apikey: SUPABASE_ANON_KEY, + "Content-Type": "application/json", + }, + body: JSON.stringify({ email, password }), + } + ); + + if (response.status !== 200) { + throw new Error(`Failed to login: ${response.statusText}`); + } + + const data = await response.json(); + return data.access_token; +} + +// Log in as user 1 +const user1Token = await login(email1, password); +const user1Client = new Client({ + apiUrl: "http://localhost:2024", + headers: { Authorization: `Bearer ${user1Token}` }, +}); + +// Create a thread as user 1 +const thread = await user1Client.threads.create(); +console.log(`✅ User 1 created thread: ${thread.thread_id}`); + +// Try to access without a token +const unauthenticatedClient = new Client({ apiUrl: "http://localhost:2024" }); +try { + await unauthenticatedClient.threads.create(); + console.log("❌ Unauthenticated access should fail!"); +} catch (e) { + console.log("✅ Unauthenticated access blocked:", e.message); +} + +// Try to access user 1's thread as user 2 +const user2Token = await login(email2, password); +const user2Client = new Client({ + apiUrl: "http://localhost:2024", + headers: { Authorization: `Bearer ${user2Token}` }, +}); + +try { + await user2Client.threads.get(thread.thread_id); + console.log("❌ User 2 shouldn't see User 1's thread!"); +} catch (e) { + console.log("✅ User 2 blocked from User 1's thread:", e.message); +} +``` + +::: + The output should look like this: ```shell @@ -272,4 +484,11 @@ Now that you have production authentication, consider: 1. Building a web UI with your preferred framework (see the [Custom Auth](https://github.com/langchain-ai/custom-auth) template for an example) 2. Learn more about the other aspects of authentication and authorization in the [conceptual guide on authentication](../../concepts/auth.md). -3. Customize your handlers and setup further after reading the [reference docs](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth). \ No newline at end of file + +:::python +3. Customize your handlers and setup further after reading the [reference docs](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth). +::: + +:::js +3. Customize your handlers and setup further after reading the [reference docs](../../cloud/reference/sdk/typescript_sdk_ref.md#auth). +::: diff --git a/docs/docs/tutorials/auth/getting_started.md b/docs/docs/tutorials/auth/getting_started.md index b9b2340e0..6ebb7e0d2 100644 --- a/docs/docs/tutorials/auth/getting_started.md +++ b/docs/docs/tutorials/auth/getting_started.md @@ -10,8 +10,8 @@ This is part 1 of our authentication series: This guide assumes basic familiarity with the following concepts: -* [**Authentication & Access Control**](../../concepts/auth.md) -* [**LangGraph Platform**](../../concepts/langgraph_platform.md) +- [**Authentication & Access Control**](../../concepts/auth.md) +- [**LangGraph Platform**](../../concepts/langgraph_platform.md) !!! note @@ -21,26 +21,52 @@ This guide assumes basic familiarity with the following concepts: Create a new chatbot using the LangGraph starter template: +:::python + ```bash pip install -U "langgraph-cli[inmem]" langgraph new --template=new-langgraph-project-python custom-auth cd custom-auth ``` +::: + +:::js + +```bash +npx @langchain/langgraph-cli new --template=new-langgraph-project-typescript custom-auth +cd custom-auth +``` + +::: + The template gives us a placeholder LangGraph app. Try it out by installing the local dependencies and running the development server: +:::python + ```shell pip install -e . langgraph dev ``` +::: + +:::js + +```shell +npm install +npm run langgraph dev +``` + +::: + The server will start and open the studio in your browser: ``` > - 🚀 API: http://127.0.0.1:2024 > - 🎨 Studio UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024 > - 📚 API Docs: http://127.0.0.1:2024/docs -> +> > This in-memory server is designed for development and testing. > For production use, please use LangGraph Platform. ``` @@ -49,7 +75,6 @@ If you were to self-host this on the public internet, anyone could access it! ![No auth](./img/no_auth.png) - ## 2. Add authentication Now that you have a base LangGraph app, add authentication to it. @@ -58,6 +83,7 @@ Now that you have a base LangGraph app, add authentication to it. In this tutorial, you will start with a hard-coded token for example purposes. You will get to a "production-ready" authentication scheme in the third tutorial. +:::python The [`Auth`](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth) object lets you register an authentication function that the LangGraph platform will run on every request. This function receives each request and decides whether to accept or reject. Create a new file `src/security/auth.py`. This is where your code will live to check if users are allowed to access your bot: @@ -98,9 +124,61 @@ Notice that your [authentication](../../cloud/reference/sdk/python_sdk_ref.md#la 1. Checks if a valid token is provided in the request's [Authorization header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization) 2. Returns the user's [identity](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.MinimalUserDict) + ::: + +:::js +The [`Auth`](../../cloud/reference/sdk/js_sdk_ref.md#Auth) object lets you register an authentication function that the LangGraph platform will run on every request. This function receives each request and decides whether to accept or reject. + +Create a new file `src/security/auth.ts`. This is where your code will live to check if users are allowed to access your bot: + +```typescript title="src/security/auth.ts" +import { Auth } from "@langchain/langgraph-sdk"; + +// This is our toy user database. Do not do this in production +const VALID_TOKENS: Record<string, { id: string; name: string }> = { + "user1-token": { id: "user1", name: "Alice" }, + "user2-token": { id: "user2", name: "Bob" }, +}; + +// The "Auth" object is a container that LangGraph will use to mark our authentication function +const auth = new Auth(); + // The `authenticate` method tells LangGraph to call this function as middleware + // for every request. This will determine whether the request is allowed or not + .authenticate((request) => { + // Our authentication handler from the previous tutorial. + const apiKey = request.headers.get("x-api-key"); + if (!apiKey || !isValidKey(apiKey)) { + throw new HTTPException(401, "Invalid API key"); + } + + const [scheme, token] = apiKey.split(" "); + if (scheme.toLowerCase() !== "bearer") { + throw new Error("Bearer token required"); + } + + if (!VALID_TOKENS[token]) { + throw new HTTPException(401, "Invalid token"); + } + + const userData = VALID_TOKENS[token]; + return { + identity: userData.id, + }; + }); + +export { auth }; +``` + +Notice that your [authentication](../../cloud/reference/sdk/js_sdk_ref.md#Auth) handler does two important things: + +1. Checks if a valid token is provided in the request's [Authorization header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization) +2. Returns the user's [identity](../../cloud/reference/sdk/js_sdk_ref.md#Auth.types.MinimalUserDict) + ::: Now tell LangGraph to use authentication by adding the following to the [`langgraph.json`](../../cloud/reference/cli.md#configuration-file) configuration: +:::python + ```json hl_lines="7-9" title="langgraph.json" { "dependencies": ["."], @@ -114,6 +192,25 @@ Now tell LangGraph to use authentication by adding the following to the [`langgr } ``` +::: + +:::js + +```json hl_lines="7-9" title="langgraph.json" +{ + "dependencies": ["."], + "graphs": { + "agent": "./src/agent/graph.ts:graph" + }, + "env": ".env", + "auth": { + "path": "src/security/auth.ts:auth" + } +} +``` + +::: + ## 3. Test your bot Start the server again to test everything out: @@ -124,21 +221,39 @@ langgraph dev --no-browser If you didn't add the `--no-browser`, the studio UI will open in the browser. You may wonder, how is the studio able to still connect to our server? By default, we also permit access from the LangGraph studio, even when using custom auth. This makes it easier to develop and test your bot in the studio. You can remove this alternative authentication option by setting `disable_studio_auth: "true"` in your auth configuration: +:::python + ```json { - "auth": { - "path": "src/security/auth.py:auth", - "disable_studio_auth": "true" - } + "auth": { + "path": "src/security/auth.py:auth", + "disable_studio_auth": "true" + } } ``` +::: + +:::js + +```json +{ + "auth": { + "path": "src/security/auth.ts:auth", + "disable_studio_auth": "true" + } +} +``` + +::: + ## 4. Chat with your bot You should now only be able to access the bot if you provide a valid token in the request header. Users will still, however, be able to access each other's resources until you add [resource authorization handlers](../../concepts/auth.md#resource-specific-handlers) in the next section of the tutorial. ![Authentication, no authorization handlers](./img/authentication.png) +:::python Run the following code in a file or notebook: ```python @@ -170,6 +285,46 @@ print("✅ Bot responded:") print(response) ``` +::: + +:::js +Run the following code in a TypeScript file: + +```typescript +import { Client } from "@langchain/langgraph-sdk"; + +async function testAuth() { + // Try without a token (should fail) + const clientWithoutToken = new Client({ apiUrl: "http://localhost:2024" }); + try { + const thread = await clientWithoutToken.threads.create(); + console.log("❌ Should have failed without token!"); + } catch (e) { + console.log("✅ Correctly blocked access:", e); + } + + // Try with a valid token + const client = new Client({ + apiUrl: "http://localhost:2024", + headers: { Authorization: "Bearer user1-token" }, + }); + + // Create a thread and chat + const thread = await client.threads.create(); + console.log(`✅ Created thread as Alice: ${thread.thread_id}`); + + const response = await client.runs.create(thread.thread_id, "agent", { + input: { messages: [{ role: "user", content: "Hello!" }] }, + }); + console.log("✅ Bot responded:"); + console.log(response); +} + +testAuth().catch(console.error); +``` + +::: + You should see that: 1. Without a valid token, we can't access the bot @@ -183,4 +338,11 @@ Now that you can control who accesses your bot, you might want to: 1. Continue the tutorial by going to [Make conversations private](resource_auth.md) to learn about resource authorization. 2. Read more about [authentication concepts](../../concepts/auth.md). -3. Check out the [API reference](../../cloud/reference/sdk/python_sdk_ref.md) for more authentication details. \ No newline at end of file + +:::python +3. Check out the [API reference](../../cloud/reference/sdk/python_sdk_ref.md) for more authentication details. +::: + +:::js +3. Check out the [API reference](../../cloud/reference/sdk/js_sdk_ref.md) for more authentication details. +::: diff --git a/docs/docs/tutorials/auth/resource_auth.md b/docs/docs/tutorials/auth/resource_auth.md index 267e00f25..d216a68cd 100644 --- a/docs/docs/tutorials/auth/resource_auth.md +++ b/docs/docs/tutorials/auth/resource_auth.md @@ -10,10 +10,17 @@ Before you start this tutorial, ensure you have the [bot from the first tutorial ## 1. Add resource authorization +:::python Recall that in the last tutorial, the [`Auth`](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth) object lets you register an [authentication function](../../concepts/auth.md#authentication), which LangGraph Platform uses to validate the bearer tokens in incoming requests. Now you'll use it to register an **authorization** handler. +::: + +:::js +Recall that in the last tutorial, the @[`Auth`][Auth] object lets you register an [authentication function](../../concepts/auth.md#authentication), which LangGraph Platform uses to validate the bearer tokens in incoming requests. Now you'll use it to register an **authorization** handler. +::: Authorization handlers are functions that run **after** authentication succeeds. These handlers can add [metadata](../../concepts/auth.md#filter-operations) to resources (like who owns them) and filter what each user can see. +:::python Update your `src/security/auth.py` and add one authorization handler to run on every request: ```python hl_lines="29-39" title="src/security/auth.py" @@ -61,7 +68,7 @@ async def add_owner( # resource='threads', # action='create_run' # ) - # value: + # value: # { # 'thread_id': UUID('1e1b2733-303f-4dcd-9620-02d370287d72'), # 'assistant_id': UUID('fe096781-5601-53d2-b2f6-0d3403f7e9ca'), @@ -103,10 +110,112 @@ async def add_owner( return filters ``` +::: + +:::js +Update your `src/security/auth.ts` and add one authorization handler to run on every request: + +```typescript hl_lines="29-39" title="src/security/auth.ts" +import { Auth, HTTPException } from "@langchain/langgraph-sdk"; + +// Keep our test users from the previous tutorial +const VALID_TOKENS: Record<string, { id: string; name: string }> = { + "user1-token": { id: "user1", name: "Alice" }, + "user2-token": { id: "user2", name: "Bob" }, +}; + +const auth = new Auth() + .authenticate(async (request) => { + // Our authentication handler from the previous tutorial. + const apiKey = request.headers.get("x-api-key"); + if (!apiKey || !isValidKey(apiKey)) { + throw new HTTPException(401, "Invalid API key"); + } + + const [scheme, token] = apiKey.split(" "); + if (scheme.toLowerCase() !== "bearer") { + throw new Error("Bearer token required"); + } + + if (!VALID_TOKENS[token]) { + throw new HTTPException(401, "Invalid token"); + } + + const userData = VALID_TOKENS[token]; + return { + identity: userData.id, + }; + }) + .on("*", ({ value, user }) => { + // This handler makes resources private to their creator by doing 2 things: + // 1. Add the user's ID to the resource's metadata. Each LangGraph resource has a `metadata` object that persists with the resource. + // this metadata is useful for filtering in read and update operations + // 2. Return a filter that lets users only see their own resources + // Examples: + // { + // user: ProxyUser { + // identity: 'user1', + // is_authenticated: true, + // display_name: 'user1' + // }, + // value: { + // 'thread_id': UUID('1e1b2733-303f-4dcd-9620-02d370287d72'), + // 'assistant_id': UUID('fe096781-5601-53d2-b2f6-0d3403f7e9ca'), + // 'run_id': UUID('1efbe268-1627-66d4-aa8d-b956b0f02a41'), + // 'status': 'pending', + // 'metadata': {}, + // 'prevent_insert_if_inflight': true, + // 'multitask_strategy': 'reject', + // 'if_not_exists': 'reject', + // 'after_seconds': 0, + // 'kwargs': { + // 'input': {'messages': [{'role': 'user', 'content': 'Hello!'}]}, + // 'command': null, + // 'config': { + // 'configurable': { + // 'langgraph_auth_user': ... Your user object... + // 'langgraph_auth_user_id': 'user1' + // } + // }, + // 'stream_mode': ['values'], + // 'interrupt_before': null, + // 'interrupt_after': null, + // 'webhook': null, + // 'feedback_keys': null, + // 'temporary': false, + // 'subgraphs': false + // } + // } + // } + + const filters = { owner: user.identity }; + const metadata = value.metadata || {}; + Object.assign(metadata, filters); + value.metadata = metadata; + + // Only let users see their own resources + return filters; + }); + +export { auth }; +``` + +::: + +:::python The handler receives two parameters: 1. `ctx` ([AuthContext](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.types.AuthContext)): contains info about the current `user`, the user's `permissions`, the `resource` ("threads", "crons", "assistants"), and the `action` being taken ("create", "read", "update", "delete", "search", "create_run") 2. `value` (`dict`): data that is being created or accessed. The contents of this dict depend on the resource and action being accessed. See [adding scoped authorization handlers](#scoped-authorization) below for information on how to get more tightly scoped access control. + ::: + +:::js +The handler receives an object with the following properties: + +1. `user` ([ProxyUser](../../cloud/reference/sdk/js_ts_sdk_ref.md#langgraph_sdk.auth.types.ProxyUser)): contains info about the current `user`, the user's `permissions`, the `resource` ("threads", "crons", "assistants") +2. `action` contains information about the action being taken ("create", "read", "update", "delete", "search", "create_run") +3. `value` (`Record<string, any>`): data that is being created or accessed. The contents of this object depend on the resource and action being accessed. See [adding scoped authorization handlers](#scoped-authorization) below for information on how to get more tightly scoped access control. + ::: Notice that the simple handler does two things: @@ -117,6 +226,8 @@ Notice that the simple handler does two things: Test your authorization. If you have set things up correctly, you will see all ✅ messages. Be sure to have your development server running (run `langgraph dev`): +:::python + ```python from langgraph_sdk import get_client @@ -168,6 +279,64 @@ print(f"✅ Alice sees {len(alice_threads)} thread") print(f"✅ Bob sees {len(bob_threads)} thread") ``` +::: + +:::js + +```typescript +import { getClient } from "@langgraph/sdk"; + +// Create clients for both users +const alice = getClient({ + url: "http://localhost:2024", + headers: { Authorization: "Bearer user1-token" }, +}); + +const bob = getClient({ + url: "http://localhost:2024", + headers: { Authorization: "Bearer user2-token" }, +}); + +// Alice creates an assistant +const aliceAssistant = await alice.assistants.create(); +console.log(`✅ Alice created assistant: ${aliceAssistant.assistant_id}`); + +// Alice creates a thread and chats +const aliceThread = await alice.threads.create(); +console.log(`✅ Alice created thread: ${aliceThread.thread_id}`); + +await alice.runs.create(aliceThread.thread_id, "agent", { + input: { + messages: [{ role: "user", content: "Hi, this is Alice's private chat" }], + }, +}); + +// Bob tries to access Alice's thread +try { + await bob.threads.get(aliceThread.thread_id); + console.log("❌ Bob shouldn't see Alice's thread!"); +} catch (error) { + console.log("✅ Bob correctly denied access:", error); +} + +// Bob creates his own thread +const bobThread = await bob.threads.create(); +await bob.runs.create(bobThread.thread_id, "agent", { + input: { + messages: [{ role: "user", content: "Hi, this is Bob's private chat" }], + }, +}); +console.log(`✅ Bob created his own thread: ${bobThread.thread_id}`); + +// List threads - each user only sees their own +const aliceThreads = await alice.threads.search(); +const bobThreads = await bob.threads.search(); +console.log(`✅ Alice sees ${aliceThreads.length} thread`); +console.log(`✅ Bob sees ${bobThreads.length} thread`); +``` + +::: + Output: ```bash @@ -188,6 +357,7 @@ This means: ## 3. Add scoped authorization handlers {#scoped-authorization} +:::python The broad `@auth.on` handler matches on all [authorization events](../../concepts/auth.md#supported-resources). This is concise, but it means the contents of the `value` dict are not well-scoped, and the same user-level access control is applied to every resource. If you want to be more fine-grained, you can also control specific actions on resources. Update `src/security/auth.py` to add handlers for specific resource types: @@ -203,7 +373,7 @@ async def on_thread_create( value: Auth.types.on.threads.create.value, ): """Add owner when creating threads. - + This handler runs when creating new threads and does two things: 1. Sets metadata on the thread being created to track ownership 2. Returns a filter that ensures only the creator can access it @@ -215,8 +385,7 @@ async def on_thread_create( # This metadata is stored with the thread and persists metadata = value.setdefault("metadata", {}) metadata["owner"] = ctx.user.identity - - + # Return filter to restrict access to just the creator return {"owner": ctx.user.identity} @@ -226,7 +395,7 @@ async def on_thread_read( value: Auth.types.on.threads.read.value, ): """Only let users read their own threads. - + This handler runs on read operations. We don't need to set metadata since the thread already exists - we just need to return a filter to ensure users can only see their own threads. @@ -261,16 +430,88 @@ async def authorize_store(ctx: Auth.types.AuthContext, value: dict): assert namespace[0] == ctx.user.identity, "Not authorized" ``` +::: + +:::js +The broad `auth.on("*")` handler matches on all [authorization events](../../concepts/auth.md#supported-resources). This is concise, but it means the contents of the `value` object are not well-scoped, and the same user-level access control is applied to every resource. If you want to be more fine-grained, you can also control specific actions on resources. + +Update `src/security/auth.ts` to add handlers for specific resource types: + +```typescript +// Keep our previous handlers... + +import { Auth, HTTPException } from "@langchain/langgraph-sdk"; + +auth.on("threads:create", async ({ user, value }) => { + // Add owner when creating threads. + // This handler runs when creating new threads and does two things: + // 1. Sets metadata on the thread being created to track ownership + // 2. Returns a filter that ensures only the creator can access it + + // Example value: + // {thread_id: UUID('99b045bc-b90b-41a8-b882-dabc541cf740'), metadata: {}, if_exists: 'raise'} + + // Add owner metadata to the thread being created + // This metadata is stored with the thread and persists + const metadata = value.metadata || {}; + metadata.owner = user.identity; + value.metadata = metadata; + + // Return filter to restrict access to just the creator + return { owner: user.identity }; +}); + +auth.on("threads:read", async ({ user, value }) => { + // Only let users read their own threads. + // This handler runs on read operations. We don't need to set + // metadata since the thread already exists - we just need to + // return a filter to ensure users can only see their own threads. + return { owner: user.identity }; +}); + +auth.on("assistants", async ({ user, value }) => { + // For illustration purposes, we will deny all requests + // that touch the assistants resource + // Example value: + // { + // 'assistant_id': UUID('63ba56c3-b074-4212-96e2-cc333bbc4eb4'), + // 'graph_id': 'agent', + // 'config': {}, + // 'metadata': {}, + // 'name': 'Untitled' + // } + throw new HTTPException(403, "User lacks the required permissions."); +}); + +auth.on("store", async ({ user, value }) => { + // The "namespace" field for each store item is a tuple you can think of as the directory of an item. + const namespace: string[] = value.namespace; + if (namespace[0] !== user.identity) { + throw new Error("Not authorized"); + } +}); +``` + +::: + Notice that instead of one global handler, you now have specific handlers for: 1. Creating threads 2. Reading threads 3. Accessing assistants +:::python The first three of these match specific **actions** on each resource (see [resource actions](../../concepts/auth.md#resource-specific-handlers)), while the last one (`@auth.on.assistants`) matches _any_ action on the `assistants` resource. For each request, LangGraph will run the most specific handler that matches the resource and action being accessed. This means that the four handlers above will run rather than the broadly scoped "`@auth.on`" handler. +::: + +:::js +The first three of these match specific **actions** on each resource (see [resource actions](../../concepts/auth.md#resource-specific-handlers)), while the last one (`auth.on.assistants`) matches _any_ action on the `assistants` resource. For each request, LangGraph will run the most specific handler that matches the resource and action being accessed. This means that the four handlers above will run rather than the broadly scoped "`auth.on`" handler. +::: Try adding the following test code to your test file: +:::python + ```python # ... Same as before # Try creating an assistant. This should fail @@ -292,6 +533,38 @@ alice_thread = await alice.threads.create() print(f"✅ Alice created thread: {alice_thread['thread_id']}") ``` +::: + +:::js + +```typescript +// ... Same as before +// Try creating an assistant. This should fail +try { + await alice.assistants.create("agent"); + console.log("❌ Alice shouldn't be able to create assistants!"); +} catch (error) { + console.log("✅ Alice correctly denied access:", error); +} + +// Try searching for assistants. This also should fail +try { + await alice.assistants.search(); + console.log("❌ Alice shouldn't be able to search assistants!"); +} catch (error) { + console.log( + "✅ Alice correctly denied access to searching assistants:", + error + ); +} + +// Alice can still create threads +const aliceThread = await alice.threads.create(); +console.log(`✅ Alice created thread: ${aliceThread.thread_id}`); +``` + +::: + Output: ```bash @@ -302,7 +575,7 @@ For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/St ✅ Alice sees 1 thread ✅ Bob sees 1 thread ✅ Alice correctly denied access: -For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500 +For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/50j0 ✅ Alice correctly denied access to searching assistants: ``` @@ -314,4 +587,11 @@ Now that you can control access to resources, you might want to: 1. Move on to [Connect an authentication provider](add_auth_server.md) to add real user accounts. 2. Read more about [authorization patterns](../../concepts/auth.md#authorization). + +:::python 3. Check out the [API reference](../../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth) for details about the interfaces and methods used in this tutorial. +::: + +:::js +3. Check out the [API reference](../../cloud/reference/sdk/js_sdk_ref.md#langgraph_sdk.auth.Auth) for details about the interfaces and methods used in this tutorial. +::: diff --git a/docs/docs/tutorials/get-started/1-build-basic-chatbot.md b/docs/docs/tutorials/get-started/1-build-basic-chatbot.md index b32e42861..fc2a46766 100644 --- a/docs/docs/tutorials/get-started/1-build-basic-chatbot.md +++ b/docs/docs/tutorials/get-started/1-build-basic-chatbot.md @@ -1,6 +1,6 @@ # Build a basic chatbot -In this tutorial, you will build a basic chatbot. This chatbot is the basis for the following series of tutorials where you will progressively add more sophisticated capabilities, and be introduced to key LangGraph concepts along the way. Let’s dive in! 🌟 +In this tutorial, you will build a basic chatbot. This chatbot is the basis for the following series of tutorials where you will progressively add more sophisticated capabilities, and be introduced to key LangGraph concepts along the way. Let's dive in! 🌟 ## Prerequisites @@ -13,13 +13,44 @@ tool-calling features, such as [OpenAI](https://platform.openai.com/api-keys), Install the required packages: +:::python + ```bash pip install -U langgraph langsmith ``` +::: + +:::js +=== "npm" + + ```bash + npm install @langchain/langgraph @langchain/core zod + ``` + +=== "yarn" + + ```bash + yarn add @langchain/langgraph @langchain/core zod + ``` + +=== "pnpm" + + ```bash + pnpm add @langchain/langgraph @langchain/core zod + ``` + +=== "bun" + + ```bash + bun add @langchain/langgraph @langchain/core zod + ``` + +::: + !!! tip - Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph. For more information on how to get started, see [LangSmith docs](https://docs.smith.langchain.com). + Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph. For more information on how to get started, see [LangSmith docs](https://docs.smith.langchain.com). ## 2. Create a `StateGraph` @@ -27,6 +58,8 @@ Now you can create a basic chatbot using LangGraph. This chatbot will respond di Start by creating a `StateGraph`. A `StateGraph` object defines the structure of our chatbot as a "state machine". We'll add `nodes` to represent the llm and functions our chatbot can call and `edges` to specify how the bot should transition between these functions. +:::python + ```python from typing import Annotated @@ -46,23 +79,40 @@ class State(TypedDict): graph_builder = StateGraph(State) ``` +::: + +:::js + +```typescript +import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const graph = new StateGraph(State).compile(); +``` + +::: + Our graph can now handle two key tasks: 1. Each `node` can receive the current `State` as input and output an update to the state. -2. Updates to `messages` will be appended to the existing list rather than overwriting it, thanks to the prebuilt [`add_messages`](https://langchain-ai.github.io/langgraph/reference/graphs/?h=add+messages#add_messages) function used with the `Annotated` syntax. - ------- +2. Updates to `messages` will be appended to the existing list rather than overwriting it, thanks to the prebuilt reducer function. !!! tip "Concept" - When defining a graph, the first step is to define its `State`. The `State` includes the graph's schema and [reducer functions](https://langchain-ai.github.io/langgraph/concepts/low_level/#reducers) that handle state updates. In our example, `State` is a `TypedDict` with one key: `messages`. The [`add_messages`](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.message.add_messages) reducer function is used to append new messages to the list instead of overwriting it. Keys without a reducer annotation will overwrite previous values. To learn more about state, reducers, and related concepts, see [LangGraph reference docs](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.message.add_messages). + When defining a graph, the first step is to define its `State`. The `State` includes the graph's schema and [reducer functions](https://langchain-ai.github.io/langgraph/concepts/low_level/#reducers) that handle state updates. In our example, `State` is a schema with one key: `messages`. The reducer function is used to append new messages to the list instead of overwriting it. Keys without a reducer annotation will overwrite previous values. + + To learn more about state, reducers, and related concepts, see [LangGraph reference docs](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.message.add_messages). ## 3. Add a node -Next, add a "`chatbot`" node. **Nodes** represent units of work and are typically regular Python functions. +Next, add a "`chatbot`" node. **Nodes** represent units of work and are typically regular functions. Let's first select a chat model: +:::python + {% include-markdown "../../../snippets/chat_model_tabs.md" %} <!--- @@ -73,9 +123,26 @@ llm = init_chat_model("anthropic:claude-3-5-sonnet-latest") ``` --> +::: + +:::js + +```typescript +import { ChatOpenAI } from "@langchain/openai"; +// or import { ChatAnthropic } from "@langchain/anthropic"; + +const llm = new ChatOpenAI({ + model: "gpt-4o", + temperature: 0, +}); +``` + +::: We can now incorporate the chat model into a simple node: +:::python + ```python def chatbot(state: State): @@ -88,38 +155,133 @@ def chatbot(state: State): graph_builder.add_node("chatbot", chatbot) ``` +::: + +:::js + +```typescript hl_lines="7-9" +import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const graph = new StateGraph(State) + .addNode("chatbot", async (state: z.infer<typeof State>) => { + return { messages: [await llm.invoke(state.messages)] }; + }) + .compile(); +``` + +::: + **Notice** how the `chatbot` node function takes the current `State` as input and returns a dictionary containing an updated `messages` list under the key "messages". This is the basic pattern for all LangGraph node functions. +:::python The `add_messages` function in our `State` will append the LLM's response messages to whatever messages are already in the state. +::: + +:::js +The `addMessages` function used within `MessagesZodState` will append the LLM's response messages to whatever messages are already in the state. +::: ## 4. Add an `entry` point Add an `entry` point to tell the graph **where to start its work** each time it is run: +:::python + ```python graph_builder.add_edge(START, "chatbot") ``` +::: + +:::js + +```typescript hl_lines="10" +import { StateGraph, MessagesZodState, START } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const graph = new StateGraph(State) + .addNode("chatbot", async (state: z.infer<typeof State>) => { + return { messages: [await llm.invoke(state.messages)] }; + }) + .addEdge(START, "chatbot") + .compile(); +``` + +::: + ## 5. Add an `exit` point Add an `exit` point to indicate **where the graph should finish execution**. This is helpful for more complex flows, but even in a simple graph like this, adding an end node improves clarity. +:::python + ```python graph_builder.add_edge("chatbot", END) ``` + +::: + +:::js + +```typescript hl_lines="11" +import { StateGraph, MessagesZodState, START, END } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const graph = new StateGraph(State) + .addNode("chatbot", async (state: z.infer<typeof State>) => { + return { messages: [await llm.invoke(state.messages)] }; + }) + .addEdge(START, "chatbot") + .addEdge("chatbot", END) + .compile(); +``` + +::: + This tells the graph to terminate after running the chatbot node. ## 6. Compile the graph Before running the graph, we'll need to compile it. We can do so by calling `compile()` -on the graph builder. This creates a `CompiledStateGraph` we can invoke on our state. +on the graph builder. This creates a `CompiledGraph` we can invoke on our state. + +:::python ```python graph = graph_builder.compile() ``` +::: + +:::js + +```typescript hl_lines="12" +import { StateGraph, MessagesZodState, START, END } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const graph = new StateGraph(State) + .addNode("chatbot", async (state: z.infer<typeof State>) => { + return { messages: [await llm.invoke(state.messages)] }; + }) + .addEdge(START, "chatbot") + .addEdge("chatbot", END) + .compile(); +``` + +::: + ## 7. Visualize the graph (optional) +:::python You can visualize the graph using the `get_graph` method and one of the "draw" methods, like `draw_ascii` or `draw_png`. The `draw` methods each require additional dependencies. ```python @@ -132,17 +294,35 @@ except Exception: pass ``` -![basic chatbot diagram](basic-chatbot.png) +::: +:::js +You can visualize the graph using the `getGraph` method and render the graph with the `drawMermaidPng` method. + +```typescript +import * as fs from "node:fs/promises"; + +const drawableGraph = await graph.getGraphAsync(); +const image = await drawableGraph.drawMermaidPng(); +const imageBuffer = new Uint8Array(await image.arrayBuffer()); + +await fs.writeFile("basic-chatbot.png", imageBuffer); +``` + +::: + +![basic chatbot diagram](basic-chatbot.png) ## 8. Run the chatbot -Now run the chatbot! +Now run the chatbot! !!! tip You can exit the chat loop at any time by typing `quit`, `exit`, or `q`. +:::python + ```python def stream_graph_updates(user_input: str): for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}): @@ -165,13 +345,86 @@ while True: break ``` +::: + +:::js + +```typescript +import { HumanMessage } from "@langchain/core/messages"; + +async function streamGraphUpdates(userInput: string) { + const stream = await graph.stream({ + messages: [new HumanMessage(userInput)], + }); + +import * as readline from "node:readline/promises"; +import { StateGraph, MessagesZodState, START, END } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; +import { z } from "zod"; + +const llm = new ChatOpenAI({ model: "gpt-4o-mini" }); + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const graph = new StateGraph(State) + .addNode("chatbot", async (state: z.infer<typeof State>) => { + return { messages: [await llm.invoke(state.messages)] }; + }) + .addEdge(START, "chatbot") + .addEdge("chatbot", END) + .compile(); + +async function generateText(content: string) { + const stream = await graph.stream( + { messages: [{ type: "human", content }] }, + { streamMode: "values" } + ); + + for await (const event of stream) { + for (const value of Object.values(event)) { + console.log( + "Assistant:", + value.messages[value.messages.length - 1].content + ); + const lastMessage = event.messages.at(-1); + if (lastMessage?.getType() === "ai") { + console.log(`Assistant: ${lastMessage.text}`); + } + } +} + +const prompt = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +while (true) { + const human = await prompt.question("User: "); + if (["quit", "exit", "q"].includes(human.trim())) break; + await generateText(human || "What do you know about LangGraph?"); +} + +prompt.close(); +``` + +::: + ``` Assistant: LangGraph is a library designed to help build stateful multi-agent applications using language models. It provides tools for creating workflows and state machines to coordinate multiple AI agents or language model interactions. LangGraph is built on top of LangChain, leveraging its components while adding graph-based coordination capabilities. It's particularly useful for developing more complex, stateful AI applications that go beyond simple query-response interactions. +``` + +:::python + +``` Goodbye! ``` +::: + **Congratulations!** You've built your first chatbot using LangGraph. This bot can engage in basic conversation by taking user input and generating responses using an LLM. You can inspect a [LangSmith Trace](https://smith.langchain.com/public/7527e308-9502-4894-b347-f34385740d5a/r) for the call above. +:::python + Below is the full code for this tutorial: ```python @@ -207,8 +460,36 @@ graph_builder.add_edge("chatbot", END) graph = graph_builder.compile() ``` +::: + +:::js + +```typescript +import { StateGraph, START, END, MessagesZodState } from "@langchain/langgraph"; +import { z } from "zod"; +import { ChatOpenAI } from "@langchain/openai"; + +const llm = new ChatOpenAI({ + model: "gpt-4o", + temperature: 0, +}); + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const graph = new StateGraph(State); + // The first argument is the unique node name + // The second argument is the function or object that will be called whenever + // the node is used. + .addNode("chatbot", async (state) => { + return { messages: [await llm.invoke(state.messages)] }; + }); + .addEdge(START, "chatbot"); + .addEdge("chatbot", END) + .compile(); +``` + +::: + ## Next steps You may have noticed that the bot's knowledge is limited to what's in its training data. In the next part, we'll [add a web search tool](./2-add-tools.md) to expand the bot's knowledge and make it more capable. - - diff --git a/docs/docs/tutorials/get-started/2-add-tools.md b/docs/docs/tutorials/get-started/2-add-tools.md index 93f905309..2cfdb3db2 100644 --- a/docs/docs/tutorials/get-started/2-add-tools.md +++ b/docs/docs/tutorials/get-started/2-add-tools.md @@ -10,35 +10,84 @@ To handle queries that your chatbot can't answer "from memory", integrate a web Before you start this tutorial, ensure you have the following: +:::python + - An API key for the [Tavily Search Engine](https://python.langchain.com/docs/integrations/tools/tavily_search/). +::: + +:::js + +- An API key for the [Tavily Search Engine](https://js.langchain.com/docs/integrations/tools/tavily_search/). + +::: + ## 1. Install the search engine +:::python Install the requirements to use the [Tavily Search Engine](https://python.langchain.com/docs/integrations/tools/tavily_search/): ```bash pip install -U langchain-tavily ``` + +::: + +:::js +Install the requirements to use the [Tavily Search Engine](https://docs.tavily.com/): + +=== "npm" + + ```bash + npm install @langchain/tavily + ``` + +=== "yarn" + + ```bash + yarn add @langchain/tavily + ``` + +=== "pnpm" + + ```bash + pnpm add @langchain/tavily + ``` + +=== "bun" + + ```bash + bun add @langchain/tavily + ``` + +::: + ## 2. Configure your environment Configure your environment with your search engine API key: +:::python ```python -def _set_env(var: str): - if not os.environ.get(var): - os.environ[var] = getpass.getpass(f"{var}: ") +import os -_set_env("TAVILY_API_KEY") +os.environ["TAVILY_API_KEY"] = "tvly-..." +``` +::: + +:::js + +```typescript +process.env.TAVILY_API_KEY = "tvly-..."; ``` -``` -os.environ["TAVILY_API_KEY"]: "········" -``` +::: ## 3. Define the tool Define the web search tool: +:::python + ```python from langchain_tavily import TavilySearch @@ -47,8 +96,25 @@ tools = [tool] tool.invoke("What's a 'node' in LangGraph?") ``` +::: + +:::js + +```typescript +import { TavilySearch } from "@langchain/tavily"; + +const tool = new TavilySearch({ maxResults: 2 }); +const tools = [tool]; + +await tool.invoke({ query: "What's a 'node' in LangGraph?" }); +``` + +::: + The results are page summaries our chat bot can use to answer questions: +:::python + ``` {'query': "What's a 'node' in LangGraph?", 'follow_up_questions': None, @@ -67,12 +133,51 @@ The results are page summaries our chat bot can use to answer questions: 'response_time': 1.38} ``` +::: + +:::js + +```json +{ + "query": "What's a 'node' in LangGraph?", + "follow_up_questions": null, + "answer": null, + "images": [], + "results": [ + { + "url": "https://blog.langchain.dev/langgraph/", + "title": "LangGraph - LangChain Blog", + "content": "TL;DR: LangGraph is module built on top of LangChain to better enable creation of cyclical graphs, often needed for agent runtimes. This state is updated by nodes in the graph, which return operations to attributes of this state (in the form of a key-value store). After adding nodes, you can then add edges to create the graph. An example of this may be in the basic agent runtime, where we always want the model to be called after we call a tool. The state of this graph by default contains concepts that should be familiar to you if you've used LangChain agents: `input`, `chat_history`, `intermediate_steps` (and `agent_outcome` to represent the most recent agent outcome)", + "score": 0.7407191, + "raw_content": null + }, + { + "url": "https://medium.com/@cplog/introduction-to-langgraph-a-beginners-guide-14f9be027141", + "title": "Introduction to LangGraph: A Beginner's Guide - Medium", + "content": "* **Stateful Graph:** LangGraph revolves around the concept of a stateful graph, where each node in the graph represents a step in your computation, and the graph maintains a state that is passed around and updated as the computation progresses. LangGraph supports conditional edges, allowing you to dynamically determine the next node to execute based on the current state of the graph. Image 10: Introduction to AI Agent with LangChain and LangGraph: A Beginner’s Guide Image 18: How to build LLM Agent with LangGraph — StateGraph and Reducer Image 20: Simplest Graphs using LangGraph Framework Image 24: Building a ReAct Agent with Langgraph: A Step-by-Step Guide Image 28: Building an Agentic RAG with LangGraph: A Step-by-Step Guide", + "score": 0.65279555, + "raw_content": null + } + ], + "response_time": 1.34 +} +``` + +::: + ## 4. Define the graph +:::python For the `StateGraph` you created in the [first tutorial](./1-build-basic-chatbot.md), add `bind_tools` on the LLM. This lets the LLM know the correct JSON format to use if it wants to use the search engine. +::: + +:::js +For the `StateGraph` you created in the [first tutorial](./1-build-basic-chatbot.md), add `bindTools` on the LLM. This lets the LLM know the correct JSON format to use if it wants to use the search engine. +::: Let's first select our LLM: +:::python {% include-markdown "../../../snippets/chat_model_tabs.md" %} <!--- @@ -83,9 +188,23 @@ llm = init_chat_model("anthropic:claude-3-5-sonnet-latest") ``` --> +::: + +:::js + +```typescript +import { ChatAnthropic } from "@langchain/anthropic"; + +const llm = new ChatAnthropic({ model: "claude-3-5-sonnet-latest" }); +``` + +::: + We can now incorporate it into a `StateGraph`: -```python hl_lines="15" +:::python + +```python from typing import Annotated from typing_extensions import TypedDict @@ -108,9 +227,31 @@ def chatbot(state: State): graph_builder.add_node("chatbot", chatbot) ``` +::: + +:::js + +```typescript hl_lines="7-8" +import { StateGraph, MessagesZodState } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const chatbot = async (state: z.infer<typeof State>) => { + // Modification: tell the LLM which tools it can call + const llmWithTools = llm.bindTools(tools); + + return { messages: [await llmWithTools.invoke(state.messages)] }; +}; +``` + +::: + ## 5. Create a function to run the tools -Now, create a function to run the tools if they are called. Do this by adding the tools to a new node called`BasicToolNode` that checks the most recent message in the state and calls tools if the message contains `tool_calls`. It relies on the LLM's `tool_calling` support, which is available in Anthropic, OpenAI, Google Gemini, and a number of other LLM providers. +:::python + +Now, create a function to run the tools if they are called. Do this by adding the tools to a new node called `BasicToolNode` that checks the most recent message in the state and calls tools if the message contains `tool_calls`. It relies on the LLM's `tool_calling` support, which is available in Anthropic, OpenAI, Google Gemini, and a number of other LLM providers. ```python import json @@ -152,16 +293,80 @@ graph_builder.add_node("tools", tool_node) If you do not want to build this yourself in the future, you can use LangGraph's prebuilt [ToolNode](https://langchain-ai.github.io/langgraph/reference/agents/#langgraph.prebuilt.tool_node.ToolNode). +::: + +:::js + +Now, create a function to run the tools if they are called. Do this by adding the tools to a new node called `"tools"` that checks the most recent message in the state and calls tools if the message contains `tool_calls`. It relies on the LLM's tool calling support, which is available in Anthropic, OpenAI, Google Gemini, and a number of other LLM providers. + +```typescript +import type { StructuredToolInterface } from "@langchain/core/tools"; +import { isAIMessage, ToolMessage } from "@langchain/core/messages"; + +function createToolNode(tools: StructuredToolInterface[]) { + const toolByName: Record<string, StructuredToolInterface> = {}; + for (const tool of tools) { + toolByName[tool.name] = tool; + } + + return async (inputs: z.infer<typeof State>) => { + const { messages } = inputs; + if (!messages || messages.length === 0) { + throw new Error("No message found in input"); + } + + const message = messages.at(-1); + if (!message || !isAIMessage(message) || !message.tool_calls) { + throw new Error("Last message is not an AI message with tool calls"); + } + + const outputs: ToolMessage[] = []; + for (const toolCall of message.tool_calls) { + if (!toolCall.id) throw new Error("Tool call ID is required"); + + const tool = toolByName[toolCall.name]; + if (!tool) throw new Error(`Tool ${toolCall.name} not found`); + + const result = await tool.invoke(toolCall.args); + + outputs.push( + new ToolMessage({ + content: JSON.stringify(result), + name: toolCall.name, + tool_call_id: toolCall.id, + }) + ); + } + + return { messages: outputs }; + }; +} +``` + +!!! note + + If you do not want to build this yourself in the future, you can use LangGraph's prebuilt [ToolNode](https://langchain-ai.github.io/langgraphjs/reference/classes/langgraph_prebuilt.ToolNode.html). + +::: + ## 6. Define the `conditional_edges` -With the tool node added, now you can define the `conditional_edges`. +With the tool node added, now you can define the `conditional_edges`. **Edges** route the control flow from one node to the next. **Conditional edges** start from a single node and usually contain "if" statements to route to different nodes depending on the current graph state. These functions receive the current graph `state` and return a string or list of strings indicating which node(s) to call next. -Next, define a router function called `route_tools` that checks for `tool_calls` in the chatbot's output. Provide this function to the graph by calling `add_conditional_edges`, which tells the graph that whenever the `chatbot` node completes to check this function to see where to go next. +:::python +Next, define a router function called `route_tools` that checks for `tool_calls` in the chatbot's output. Provide this function to the graph by calling `add_conditional_edges`, which tells the graph that whenever the `chatbot` node completes to check this function to see where to go next. +::: + +:::js +Next, define a router function called `routeTools` that checks for `tool_calls` in the chatbot's output. Provide this function to the graph by calling `addConditionalEdges`, which tells the graph that whenever the `chatbot` node completes to check this function to see where to go next. +::: The condition will route to `tools` if tool calls are present and `END` if not. Because the condition can return `END`, you do not need to explicitly set a `finish_point` this time. +:::python + ```python def route_tools( state: State, @@ -201,10 +406,61 @@ graph = graph_builder.compile() !!! note - You can replace this with the prebuilt [tools_condition](https://langchain-ai.github.io/langgraph/reference/prebuilt/#tools_condition) to be more concise. + You can replace this with the prebuilt [tools_condition](https://langchain-ai.github.io/langgraph/reference/prebuilt/#tools_condition) to be more concise. + +::: + +:::js + +```typescript +import { END, START } from "@langchain/langgraph"; + +const routeTools = (state: z.infer<typeof State>) => { + /** + * Use as conditional edge to route to the ToolNode if the last message + * has tool calls. + */ + const lastMessage = state.messages.at(-1); + if ( + lastMessage && + isAIMessage(lastMessage) && + lastMessage.tool_calls?.length + ) { + return "tools"; + } + + /** Otherwise, route to the end. */ + return END; +}; + +const graph = new StateGraph(State) + .addNode("chatbot", chatbot) + + // The `routeTools` function returns "tools" if the chatbot asks to use a tool, and "END" if + // it is fine directly responding. This conditional routing defines the main agent loop. + .addNode("tools", createToolNode(tools)) + + // Start the graph with the chatbot + .addEdge(START, "chatbot") + + // The `routeTools` function returns "tools" if the chatbot asks to use a tool, and "END" if + // it is fine directly responding. + .addConditionalEdges("chatbot", routeTools, ["tools", END]) + + // Any time a tool is called, we need to return to the chatbot + .addEdge("tools", "chatbot") + .compile(); +``` + +!!! note + + You can replace this with the prebuilt [toolsCondition](https://langchain-ai.github.io/langgraphjs/reference/functions/langgraph_prebuilt.toolsCondition.html) to be more concise. + +::: ## 7. Visualize the graph (optional) +:::python You can visualize the graph using the `get_graph` method and one of the "draw" methods, like `draw_ascii` or `draw_png`. The `draw` methods each require additional dependencies. ```python @@ -217,12 +473,31 @@ except Exception: pass ``` +::: + +:::js +You can visualize the graph using the `getGraph` method and render the graph with the `drawMermaidPng` method. + +```typescript +import * as fs from "node:fs/promises"; + +const drawableGraph = await graph.getGraphAsync(); +const image = await drawableGraph.drawMermaidPng(); +const imageBuffer = new Uint8Array(await image.arrayBuffer()); + +await fs.writeFile("chatbot-with-tools.png", imageBuffer); +``` + +::: + ![chatbot-with-tools-diagram](chatbot-with-tools.png) ## 8. Ask the bot questions Now you can ask the chatbot questions outside its training data: +:::python + ```python def stream_graph_updates(user_input: str): for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}): @@ -245,7 +520,7 @@ while True: break ``` -``` +``` Assistant: [{'text': "To provide you with accurate and up-to-date information about LangGraph, I'll need to search for the latest details. Let me do that for you.", 'type': 'text'}, {'id': 'toolu_01Q588CszHaSvvP2MxRq9zRD', 'input': {'query': 'LangGraph AI tool information'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}] Assistant: [{"url": "https://www.langchain.com/langgraph", "content": "LangGraph sets the foundation for how we can build and scale AI workloads \u2014 from conversational agents, complex task automation, to custom LLM-backed experiences that 'just work'. The next chapter in building complex production-ready features with LLMs is agentic, and with LangGraph and LangSmith, LangChain delivers an out-of-the-box solution ..."}, {"url": "https://github.com/langchain-ai/langgraph", "content": "Overview. LangGraph is a library for building stateful, multi-actor applications with LLMs, used to create agent and multi-agent workflows. Compared to other LLM frameworks, it offers these core benefits: cycles, controllability, and persistence. LangGraph allows you to define flows that involve cycles, essential for most agentic architectures ..."}] Assistant: Based on the search results, I can provide you with information about LangGraph: @@ -276,18 +551,107 @@ Assistant: Based on the search results, I can provide you with information about LangGraph appears to be a significant tool in the evolving landscape of LLM-based application development, offering developers new ways to create more complex, stateful, and interactive AI systems. Goodbye! -Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings... ``` +::: + +:::js + +```typescript +import readline from "node:readline/promises"; + +const prompt = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +async function generateText(content: string) { + const stream = await graph.stream( + { messages: [{ type: "human", content }] }, + { streamMode: "values" } + ); + + for await (const event of stream) { + const lastMessage = event.messages.at(-1); + + if (lastMessage?.getType() === "ai" || lastMessage?.getType() === "tool") { + console.log(`Assistant: ${lastMessage?.text}`); + } + } +} + +while (true) { + const human = await prompt.question("User: "); + if (["quit", "exit", "q"].includes(human.trim())) break; + await generateText(human || "What do you know about LangGraph?"); +} + +prompt.close(); +``` + +``` +User: What do you know about LangGraph? +Assistant: I'll search for the latest information about LangGraph for you. +Assistant: [{"title":"Introduction to LangGraph: A Beginner's Guide - Medium","url":"https://medium.com/@cplog/introduction-to-langgraph-a-beginners-guide-14f9be027141","content":"..."}] +Assistant: Based on the search results, I can provide you with information about LangGraph: + +LangGraph is a library within the LangChain ecosystem designed for building stateful, multi-actor applications with Large Language Models (LLMs). Here are the key aspects: + +**Core Purpose:** +- LangGraph is specifically designed for creating agent and multi-agent workflows +- It provides a framework for defining, coordinating, and executing multiple LLM agents in a structured manner + +**Key Features:** +1. **Stateful Graph Architecture**: LangGraph revolves around a stateful graph where each node represents a step in computation, and the graph maintains state that is passed around and updated as the computation progresses + +2. **Conditional Edges**: It supports conditional edges, allowing you to dynamically determine the next node to execute based on the current state of the graph + +3. **Cycles**: Unlike other LLM frameworks, LangGraph allows you to define flows that involve cycles, which is essential for most agentic architectures + +4. **Controllability**: It offers enhanced control over the application flow + +5. **Persistence**: The library provides ways to maintain state and persistence in LLM-based applications + +**Use Cases:** +- Conversational agents +- Complex task automation +- Custom LLM-backed experiences +- Multi-agent systems that perform complex tasks + +**Benefits:** +LangGraph allows developers to focus on the high-level logic of their applications rather than the intricacies of agent coordination, making it easier to build complex, production-ready features with LLMs. + +This makes LangGraph a significant tool in the evolving landscape of LLM-based application development. +``` + +::: + ## 9. Use prebuilts For ease of use, adjust your code to replace the following with LangGraph prebuilt components. These have built in functionality like parallel API execution. +:::python + - `BasicToolNode` is replaced with the prebuilt [ToolNode](https://langchain-ai.github.io/langgraph/reference/prebuilt/#toolnode) - `route_tools` is replaced with the prebuilt [tools_condition](https://langchain-ai.github.io/langgraph/reference/prebuilt/#tools_condition) {% include-markdown "../../../snippets/chat_model_tabs.md" %} +<!--- +```python +from langchain.chat_models import init_chat_model + +llm = init_chat_model("anthropic:claude-3-5-sonnet-latest") +``` +--> + +<!--- +```python +from langchain.chat_models import init_chat_model + +llm = init_chat_model("anthropic:claude-3-5-sonnet-latest") +``` +--> ```python hl_lines="25 30" from typing import Annotated @@ -327,7 +691,46 @@ graph_builder.add_edge(START, "chatbot") graph = graph_builder.compile() ``` -**Congratulations!** You've created a conversational agent in LangGraph that can use a search engine to retrieve updated information when needed. Now it can handle a wider range of user queries. To inspect all the steps your agent just took, check out this [LangSmith trace](https://smith.langchain.com/public/4fbd7636-25af-4638-9587-5a02fdbb0172/r). +::: + +:::js + +- `createToolNode` is replaced with the prebuilt [ToolNode](https://langchain-ai.github.io/langgraphjs/reference/classes/langgraph_prebuilt.ToolNode.html) +- `routeTools` is replaced with the prebuilt [toolsCondition](https://langchain-ai.github.io/langgraphjs/reference/functions/langgraph_prebuilt.toolsCondition.html) + +```typescript +import { TavilySearch } from "@langchain/tavily"; +import { ChatOpenAI } from "@langchain/openai"; +import { StateGraph, START, MessagesZodState, END } from "@langchain/langgraph"; +import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const tools = [new TavilySearch({ maxResults: 2 })]; + +const llm = new ChatOpenAI({ model: "gpt-4o-mini" }).bindTools(tools); + +const graph = new StateGraph(State) + .addNode("chatbot", async (state) => ({ + messages: [await llm.invoke(state.messages)], + })) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile(); +``` + +::: + +**Congratulations!** You've created a conversational agent in LangGraph that can use a search engine to retrieve updated information when needed. Now it can handle a wider range of user queries. + +:::python + +To inspect all the steps your agent just took, check out this [LangSmith trace](https://smith.langchain.com/public/4fbd7636-25af-4638-9587-5a02fdbb0172/r). + +::: ## Next steps diff --git a/docs/docs/tutorials/get-started/3-add-memory.md b/docs/docs/tutorials/get-started/3-add-memory.md index 728ffba51..211f8ddfd 100644 --- a/docs/docs/tutorials/get-started/3-add-memory.md +++ b/docs/docs/tutorials/get-started/3-add-memory.md @@ -2,7 +2,7 @@ The chatbot can now [use tools](./2-add-tools.md) to answer user questions, but it does not remember the context of previous interactions. This limits its ability to have coherent, multi-turn conversations. -LangGraph solves this problem through **persistent checkpointing**. If you provide a `checkpointer` when compiling the graph and a `thread_id` when calling your graph, LangGraph automatically saves the state after each step. When you invoke the graph again using the same `thread_id`, the graph loads its saved state, allowing the chatbot to pick up where it left off. +LangGraph solves this problem through **persistent checkpointing**. If you provide a `checkpointer` when compiling the graph and a `thread_id` when calling your graph, LangGraph automatically saves the state after each step. When you invoke the graph again using the same `thread_id`, the graph loads its saved state, allowing the chatbot to pick up where it left off. We will see later that **checkpointing** is _much_ more powerful than simple chat memory - it lets you save and resume complex state at any time for error recovery, human-in-the-loop workflows, time travel interactions, and more. But first, let's add checkpointing to enable multi-turn conversations. @@ -10,47 +10,83 @@ We will see later that **checkpointing** is _much_ more powerful than simple cha This tutorial builds on [Add tools](./2-add-tools.md). -## 1. Create a `InMemorySaver` checkpointer +## 1. Create a `MemorySaver` checkpointer -Create a `InMemorySaver` checkpointer: +Create a `MemorySaver` checkpointer: -``` python -from langgraph.checkpoint.memory import InMemorySaver +:::python + +```python +from langgraph.checkpoint.memory import MemorySaver memory = InMemorySaver() ``` +::: + +:::js + +```typescript +import { MemorySaver } from "@langchain/langgraph"; + +const memory = new MemorySaver(); +``` + +::: + This is in-memory checkpointer, which is convenient for the tutorial. However, in a production application, you would likely change this to use `SqliteSaver` or `PostgresSaver` and connect a database. ## 2. Compile the graph Compile the graph with the provided checkpointer, which will checkpoint the `State` as the graph works through each node: -``` python +:::python + +```python graph = graph_builder.compile(checkpointer=memory) ``` -``` python -from IPython.display import Image, display +::: -try: - display(Image(graph.get_graph().draw_mermaid_png())) -except Exception: - # This requires some extra dependencies and is optional - pass +:::js + +```typescript hl_lines="7" +const graph = new StateGraph(State) + .addNode("chatbot", chatbot) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); ``` +::: + ## 3. Interact with your chatbot Now you can interact with your bot! -1. Pick a thread to use as the key for this conversation. +1. Pick a thread to use as the key for this conversation. + + :::python ```python config = {"configurable": {"thread_id": "1"}} ``` -2. Call your chatbot: + ::: + + :::js + + ```typescript + const config = { configurable: { thread_id: "1" } }; + ``` + + ::: + +2. Call your chatbot: + + :::python ```python user_input = "Hi there! My name is Will." @@ -74,14 +110,46 @@ Now you can interact with your bot! Hello Will! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to know or discuss? ``` - !!! note + !!! note The config was provided as the **second positional argument** when calling our graph. It importantly is _not_ nested within the graph inputs (`{'messages': []}`). + ::: + + :::js + + ```typescript + const userInput = "Hi there! My name is Will."; + + const events = await graph.stream( + { messages: [{ type: "human", content: userInput }] }, + { configurable: { thread_id: "1" }, streamMode: "values" } + ); + + for await (const event of events) { + const lastMessage = event.messages.at(-1); + console.log(`${lastMessage?.getType()}: ${lastMessage?.text}`); + } + ``` + + ``` + human: Hi there! My name is Will. + ai: Hello Will! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to know or discuss? + ``` + + !!! note + !!! note + + The config was provided as the **second parameter** when calling our graph. It importantly is _not_ nested within the graph inputs (`{"messages": []}`). + + ::: + ## 4. Ask a follow up question Ask a follow up question: +:::python + ```python user_input = "Remember my name?" @@ -104,10 +172,37 @@ Remember my name? Of course, I remember your name, Will. I always try to pay attention to important details that users share with me. Is there anything else you'd like to talk about or any questions you have? I'm here to help with a wide range of topics or tasks. ``` +::: + +:::js + +```typescript +const userInput2 = "Remember my name?"; + +const events2 = await graph.stream( + { messages: [{ type: "human", content: userInput2 }] }, + { configurable: { thread_id: "1" }, streamMode: "values" } +); + +for await (const event of events2) { + const lastMessage = event.messages.at(-1); + console.log(`${lastMessage?.getType()}: ${lastMessage?.text}`); +} +``` + +``` +human: Remember my name? +ai: Yes, your name is Will. How can I help you today? +``` + +::: + **Notice** that we aren't using an external list for memory: it's all handled by the checkpointer! You can inspect the full execution in this [LangSmith trace](https://smith.langchain.com/public/29ba22b5-6d40-4fbe-8d27-b369e3329c84/r) to see what's going on. Don't believe me? Try this using a different config. +:::python + ```python # The only difference is we change the `thread_id` here to "2" instead of "1" events = graph.stream( @@ -129,10 +224,36 @@ Remember my name? I apologize, but I don't have any previous context or memory of your name. As an AI assistant, I don't retain information from past conversations. Each interaction starts fresh. Could you please tell me your name so I can address you properly in this conversation? ``` +::: + +:::js + +```typescript hl_lines="3-4" +const events3 = await graph.stream( + { messages: [{ type: "human", content: userInput2 }] }, + // The only difference is we change the `thread_id` here to "2" instead of "1" + { configurable: { thread_id: "2" }, streamMode: "values" } +); + +for await (const event of events3) { + const lastMessage = event.messages.at(-1); + console.log(`${lastMessage?.getType()}: ${lastMessage?.text}`); +} +``` + +``` +human: Remember my name? +ai: I don't have the ability to remember personal information about users between interactions. However, I'm here to help you with any questions or topics you want to discuss! +``` + +::: + **Notice** that the **only** change we've made is to modify the `thread_id` in the config. See this call's [LangSmith trace](https://smith.langchain.com/public/51a62351-2f0a-4058-91cc-9996c5561428/r) for comparison. ## 5. Inspect the state +:::python + By now, we have made a few checkpoints across two different threads. But what goes into a checkpoint? To inspect a graph's `state` for a given config at any time, call `get_state(config)`. ```python @@ -148,12 +269,94 @@ StateSnapshot(values={'messages': [HumanMessage(content='Hi there! My name is Wi snapshot.next # (since the graph ended this turn, `next` is empty. If you fetch a state from within a graph invocation, next tells which node will execute next) ``` +::: + +:::js + +By now, we have made a few checkpoints across two different threads. But what goes into a checkpoint? To inspect a graph's `state` for a given config at any time, call `getState(config)`. + +```typescript +await graph.getState({ configurable: { thread_id: "1" } }); +``` + +```typescript +{ + values: { + messages: [ + HumanMessage { + "id": "32fabcef-b3b8-481f-8bcb-fd83399a5f8d", + "content": "Hi there! My name is Will.", + "additional_kwargs": {}, + "response_metadata": {} + }, + AIMessage { + "id": "chatcmpl-BrPbTsCJbVqBvXWySlYoTJvM75Kv8", + "content": "Hello Will! How can I assist you today?", + "additional_kwargs": {}, + "response_metadata": {}, + "tool_calls": [], + "invalid_tool_calls": [] + }, + HumanMessage { + "id": "561c3aad-f8fc-4fac-94a6-54269a220856", + "content": "Remember my name?", + "additional_kwargs": {}, + "response_metadata": {} + }, + AIMessage { + "id": "chatcmpl-BrPbU4BhhsUikGbW37hYuF5vvnnE2", + "content": "Yes, I remember your name, Will! How can I help you today?", + "additional_kwargs": {}, + "response_metadata": {}, + "tool_calls": [], + "invalid_tool_calls": [] + } + ] + }, + next: [], + tasks: [], + metadata: { + source: 'loop', + step: 4, + parents: {}, + thread_id: '1' + }, + config: { + configurable: { + thread_id: '1', + checkpoint_id: '1f05cccc-9bb6-6270-8004-1d2108bcec77', + checkpoint_ns: '' + } + }, + createdAt: '2025-07-09T13:58:27.607Z', + parentConfig: { + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1f05cccc-78fa-68d0-8003-ffb01a76b599' + } + } +} +``` + +```typescript +import * as assert from "node:assert"; + +// Since the graph ended this turn, `next` is empty. +// If you fetch a state from within a graph invocation, next tells which node will execute next) +assert.deepEqual(snapshot.next, []); +``` + +::: + The snapshot above contains the current state values, corresponding config, and the `next` node to process. In our case, the graph has reached an `END` state, so `next` is empty. **Congratulations!** Your chatbot can now maintain conversation state across sessions thanks to LangGraph's checkpointing system. This opens up exciting possibilities for more natural, contextual interactions. LangGraph's checkpointing even handles **arbitrarily complex graph states**, which is much more expressive and powerful than simple chat memory. - + Check out the code snippet below to review the graph from this tutorial: +:::python + {% include-markdown "../../../snippets/chat_model_tabs.md" %} <!--- @@ -204,6 +407,43 @@ memory = InMemorySaver() graph = graph_builder.compile(checkpointer=memory) ``` +::: + +:::js + +```typescript hl_lines="16 26" +import { END, MessagesZodState, START } from "@langchain/langgraph"; +import { ChatOpenAI } from "@langchain/openai"; +import { TavilySearch } from "@langchain/tavily"; + +import { MemorySaver } from "@langchain/langgraph"; +import { StateGraph } from "@langchain/langgraph"; +import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt"; +import { z } from "zod"; + +const State = z.object({ + messages: MessagesZodState.shape.messages, +}); + +const tools = [new TavilySearch({ maxResults: 2 })]; +const llm = new ChatOpenAI({ model: "gpt-4o-mini" }).bindTools(tools); +const memory = new MemorySaver(); + +async function generateText(content: string) { + +const graph = new StateGraph(State) + .addNode("chatbot", async (state) => ({ + messages: [await llm.invoke(state.messages)], + })) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); +``` + +::: + ## Next steps In the next tutorial, you will [add human-in-the-loop to the chatbot](./4-human-in-the-loop.md) to handle situations where it may need guidance or verification before proceeding. diff --git a/docs/docs/tutorials/get-started/4-human-in-the-loop.md b/docs/docs/tutorials/get-started/4-human-in-the-loop.md index cf1c5ba4a..e02daf926 100644 --- a/docs/docs/tutorials/get-started/4-human-in-the-loop.md +++ b/docs/docs/tutorials/get-started/4-human-in-the-loop.md @@ -2,7 +2,16 @@ Agents can be unreliable and may need human input to successfully accomplish tasks. Similarly, for some actions, you may want to require human approval before running to ensure that everything is running as intended. -LangGraph's [persistence](../../concepts/persistence.md) layer supports **human-in-the-loop** workflows, allowing execution to pause and resume based on user feedback. The primary interface to this functionality is the [`interrupt`](../../how-tos/human_in_the_loop/add-human-in-the-loop.md) function. Calling `interrupt` inside a node will pause execution. Execution can be resumed, together with new input from a human, by passing in a [Command](../../concepts/low_level.md#command). `interrupt` is ergonomically similar to Python's built-in `input()`, [with some caveats](../../how-tos/human_in_the_loop/add-human-in-the-loop.md). +LangGraph's [persistence](../../concepts/persistence.md) layer supports **human-in-the-loop** workflows, allowing execution to pause and resume based on user feedback. The primary interface to this functionality is the [`interrupt`](../../how-tos/human_in_the_loop/add-human-in-the-loop.md) function. Calling `interrupt` inside a node will pause execution. Execution can be resumed, together with new input from a human, by passing in a [Command](../../concepts/low_level.md#command). + +:::python +`interrupt` is ergonomically similar to Python's built-in `input()`, [with some caveats](../../how-tos/human_in_the_loop/add-human-in-the-loop.md). +::: + +:::js +`interrupt` is ergonomically similar to Node.js's built-in `readline.question()` function, [with some caveats](../../how-tos/human_in_the_loop/add-human-in-the-loop.md). +`interrupt` is ergonomically similar to Node.js's built-in `readline.question()` function, [with some caveats](../../how-tos/human_in_the_loop/add-human-in-the-loop.md). +::: !!! note @@ -14,6 +23,7 @@ Starting with the existing code from the [Add memory to the chatbot](./3-add-mem Let's first select a chat model: +:::python {% include-markdown "../../../snippets/chat_model_tabs.md" %} <!--- @@ -24,9 +34,22 @@ llm = init_chat_model("anthropic:claude-3-5-sonnet-latest") ``` --> +::: + +:::js + +```typescript +// Add your API key here +process.env.ANTHROPIC_API_KEY = "YOUR_API_KEY"; +``` + +::: + We can now incorporate it into our `StateGraph` with an additional tool: -``` python hl_lines="12 19 20 21 22 23" +:::python + +```python hl_lines="12 19 20 21 22 23" from typing import Annotated from langchain_tavily import TavilySearch @@ -76,6 +99,60 @@ graph_builder.add_edge("tools", "chatbot") graph_builder.add_edge(START, "chatbot") ``` +::: + +:::js + +```typescript hl_lines="1 7-19" +import { interrupt, MessagesZodState } from "@langchain/langgraph"; +import { ChatAnthropic } from "@langchain/anthropic"; +import { TavilySearch } from "@langchain/tavily"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const humanAssistance = tool( + async ({ query }) => { + const humanResponse = interrupt({ query }); + return humanResponse.data; + }, + { + name: "humanAssistance", + description: "Request assistance from a human.", + schema: z.object({ + query: z.string().describe("Human readable question for the human"), + }), + } +); + +const searchTool = new TavilySearch({ maxResults: 2 }); +const searchTool = new TavilySearch({ maxResults: 2 }); +const tools = [searchTool, humanAssistance]; + +const llmWithTools = new ChatAnthropic({ + model: "claude-3-5-sonnet-latest", +}).bindTools(tools); +const llmWithTools = new ChatAnthropic({ + model: "claude-3-5-sonnet-latest", +}).bindTools(tools); + +async function chatbot(state: z.infer<typeof MessagesZodState>) { +async function chatbot(state: z.infer<typeof MessagesZodState>) { + const message = await llmWithTools.invoke(state.messages); + + + // Because we will be interrupting during tool execution, + // we disable parallel tool calling to avoid repeating any + // tool invocations when we resume. + if (message.tool_calls && message.tool_calls.length > 1) { + throw new Error("Multiple tool calls not supported with interrupts"); + } + + return { messages: message }; +} +``` + +::: + !!! tip For more information and examples of human-in-the-loop workflows, see [Human-in-the-loop](../../concepts/human_in_the_loop.md). @@ -84,17 +161,48 @@ graph_builder.add_edge(START, "chatbot") We compile the graph with a checkpointer, as before: +:::python + ```python memory = InMemorySaver() graph = graph_builder.compile(checkpointer=memory) ``` +::: + +:::js + +```typescript hl_lines="3 11" +import { StateGraph, MemorySaver, START, END } from "@langchain/langgraph"; + +const memory = new MemorySaver(); + +const graph = new StateGraph(MessagesZodState) + .addNode("chatbot", chatbot) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); +const graph = new StateGraph(MessagesZodState) + .addNode("chatbot", chatbot) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); +``` + +::: + ## 3. Visualize the graph (optional) Visualizing the graph, you get the same layout as before – just with the added tool! -``` python +:::python + +```python from IPython.display import Image, display try: @@ -104,12 +212,34 @@ except Exception: pass ``` +::: + +:::js + +```typescript +import * as fs from "node:fs/promises"; +import * as fs from "node:fs/promises"; + +const drawableGraph = await graph.getGraphAsync(); +const drawableGraph = await graph.getGraphAsync(); +const image = await drawableGraph.drawMermaidPng(); +const imageBuffer = new Uint8Array(await image.arrayBuffer()); +const imageBuffer = new Uint8Array(await image.arrayBuffer()); + +await fs.writeFile("chatbot-with-tools.png", imageBuffer); +await fs.writeFile("chatbot-with-tools.png", imageBuffer); +``` + +::: + ![chatbot-with-tools-diagram](chatbot-with-tools.png) ## 4. Prompt the chatbot Now, prompt the chatbot with a question that will engage the new `human_assistance` tool: +:::python + ```python user_input = "I need some expert guidance for building an AI agent. Could you request assistance for me?" config = {"configurable": {"thread_id": "1"}} @@ -138,8 +268,71 @@ Tool Calls: query: A user is requesting expert guidance for building an AI agent. Could you please provide some expert advice or resources on this topic? ``` +::: + +:::js + +```typescript +import { isAIMessage } from "@langchain/core/messages"; + +const userInput = + "I need some expert guidance for building an AI agent. Could you request assistance for me?"; + +const events = await graph.stream( + { messages: [{ role: "user", content: userInput }] }, + { configurable: { thread_id: "1" }, streamMode: "values" } + { configurable: { thread_id: "1" }, streamMode: "values" } +); + +for await (const event of events) { + if ("messages" in event) { + const lastMessage = event.messages.at(-1); + console.log(`[${lastMessage?.getType()}]: ${lastMessage?.text}`); + + if ( + lastMessage && + isAIMessage(lastMessage) && + lastMessage.tool_calls?.length + ) { + const lastMessage = event.messages.at(-1); + console.log(`[${lastMessage?.getType()}]: ${lastMessage?.text}`); + + if ( + lastMessage && + isAIMessage(lastMessage) && + lastMessage.tool_calls?.length + ) { + console.log("Tool calls:", lastMessage.tool_calls); + } + } +} +``` + +``` +[human]: I need some expert guidance for building an AI agent. Could you request assistance for me? +[ai]: I'll help you request human assistance for guidance on building an AI agent. +[ai]: I'll help you request human assistance for guidance on building an AI agent. +Tool calls: [ + { + name: 'humanAssistance', + args: { + query: 'I would like expert guidance on building an AI agent. Could you please provide assistance with this topic?' + query: 'I would like expert guidance on building an AI agent. Could you please provide assistance with this topic?' + }, + id: 'toolu_01Bpxc8rFVMhSaRosS6b85Ts', + type: 'tool_call' + id: 'toolu_01Bpxc8rFVMhSaRosS6b85Ts', + type: 'tool_call' + } +] +``` + +::: + The chatbot generated a tool call, but then execution has been interrupted. If you inspect the graph state, you see that it stopped at the tools node: +:::python + ```python snapshot = graph.get_state(config) snapshot.next @@ -149,8 +342,27 @@ snapshot.next ('tools',) ``` +::: + +:::js + +```typescript +const snapshot = await graph.getState({ configurable: { thread_id: "1" } }); +snapshot.next; +const snapshot = await graph.getState({ configurable: { thread_id: "1" } }); +snapshot.next; +``` + +```json +["tools"] +``` + +::: + !!! info Additional information + :::python + Take a closer look at the `human_assistance` tool: ```python @@ -162,12 +374,57 @@ snapshot.next ``` Similar to Python's built-in `input()` function, calling `interrupt` inside the tool will pause execution. Progress is persisted based on the [checkpointer](../../concepts/persistence.md#checkpointer-libraries); so if it is persisting with Postgres, it can resume at any time as long as the database is alive. In this example, it is persisting with the in-memory checkpointer and can resume any time if the Python kernel is running. + ::: + + :::js + + Take a closer look at the `humanAssistance` tool: + + ```typescript hl_lines="3" + const humanAssistance = tool( + async ({ query }) => { + const humanResponse = interrupt({ query }); + return humanResponse.data; + }, + { + name: "humanAssistance", + description: "Request assistance from a human.", + schema: z.object({ + query: z.string().describe("Human readable question for the human"), + }), + }, + ); + + Take a closer look at the `humanAssistance` tool: + + ```typescript hl_lines="3" + const humanAssistance = tool( + async ({ query }) => { + const humanResponse = interrupt({ query }); + return humanResponse.data; + }, + { + name: "humanAssistance", + description: "Request assistance from a human.", + schema: z.object({ + query: z.string().describe("Human readable question for the human"), + }), + }, + ); + ``` + + Calling `interrupt` inside the tool will pause execution. Progress is persisted based on the [checkpointer](../../concepts/persistence.md#checkpointer-libraries); so if it is persisting with Postgres, it can resume at any time as long as the database is alive. In this example, it is persisting with the in-memory checkpointer and can resume any time if the JavaScript runtime is running. + ::: ## 5. Resume execution -To resume execution, pass a [`Command`](../../concepts/low_level.md#command) object containing data expected by the tool. The format of this data can be customized based on needs. For this example, use a dict with a key `"data"`: +To resume execution, pass a [`Command`](../../concepts/low_level.md#command) object containing data expected by the tool. The format of this data can be customized based on needs. -``` python +:::python + +For this example, use a dict with a key `"data"`: + +```python human_response = ( "We, the experts are here to help! We'd recommend you check out LangGraph to build your agent." " It's much more reliable and extensible than simple autonomous agents." @@ -215,12 +472,67 @@ If you'd like more specific information about LangGraph or have any questions ab Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings... ``` +::: + +:::js +For this example, use an object with a key `"data"`: + +```typescript +import { Command } from "@langchain/langgraph"; + +const humanResponse = + "We, the experts are here to help! We'd recommend you check out LangGraph to build your agent." + + " It's much more reliable and extensible than simple autonomous agents."; +(" It's much more reliable and extensible than simple autonomous agents."); + +const humanCommand = new Command({ resume: { data: humanResponse } }); + +const resumeEvents = await graph.stream(humanCommand, { + configurable: { thread_id: "1" }, + streamMode: "values", +}); +const resumeEvents = await graph.stream(humanCommand, { + configurable: { thread_id: "1" }, + streamMode: "values", +}); + +for await (const event of resumeEvents) { + if ("messages" in event) { + const lastMessage = event.messages.at(-1); + console.log(`[${lastMessage?.getType()}]: ${lastMessage?.text}`); + const lastMessage = event.messages.at(-1); + console.log(`[${lastMessage?.getType()}]: ${lastMessage?.text}`); + } +} +``` + +``` +[tool]: We, the experts are here to help! We'd recommend you check out LangGraph to build your agent. It's much more reliable and extensible than simple autonomous agents. +[ai]: Thank you for your patience. I've received some expert advice regarding your request for guidance on building an AI agent. Here's what the experts have suggested: + +The experts recommend that you look into LangGraph for building your AI agent. They mention that LangGraph is a more reliable and extensible option compared to simple autonomous agents. + +LangGraph is likely a framework or library designed specifically for creating AI agents with advanced capabilities. Here are a few points to consider based on this recommendation: + +1. Reliability: The experts emphasize that LangGraph is more reliable than simpler autonomous agent approaches. This could mean it has better stability, error handling, or consistent performance. + +2. Extensibility: LangGraph is described as more extensible, which suggests that it probably offers a flexible architecture that allows you to easily add new features or modify existing ones as your agent's requirements evolve. + +3. Advanced capabilities: Given that it's recommended over "simple autonomous agents," LangGraph likely provides more sophisticated tools and techniques for building complex AI agents. + +... +``` + +::: + The input has been received and processed as a tool message. Review this call's [LangSmith trace](https://smith.langchain.com/public/9f0f87e3-56a7-4dde-9c76-b71675624e91/r) to see the exact work that was done in the above call. Notice that the state is loaded in the first step so that our chatbot can continue where it left off. **Congratulations!** You've used an `interrupt` to add human-in-the-loop execution to your chatbot, allowing for human oversight and intervention when needed. This opens up the potential UIs you can create with your AI systems. Since you have already added a **checkpointer**, as long as the underlying persistence layer is running, the graph can be paused **indefinitely** and resumed at any time as if nothing had happened. Check out the code snippet below to review the graph from this tutorial: +:::python + {% include-markdown "../../../snippets/chat_model_tabs.md" %} ```python @@ -272,6 +584,117 @@ memory = InMemorySaver() graph = graph_builder.compile(checkpointer=memory) ``` +::: + +:::js + +```typescript +import { + interrupt, + MessagesZodState, + StateGraph, + MemorySaver, + START, + END, +} from "@langchain/langgraph"; +import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt"; +import { isAIMessage } from "@langchain/core/messages"; +import { ChatAnthropic } from "@langchain/anthropic"; +import { TavilySearch } from "@langchain/tavily"; +import { + interrupt, + MessagesZodState, + StateGraph, + MemorySaver, + START, + END, +} from "@langchain/langgraph"; +import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt"; +import { isAIMessage } from "@langchain/core/messages"; +import { ChatAnthropic } from "@langchain/anthropic"; +import { TavilySearch } from "@langchain/tavily"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const humanAssistance = tool( + async ({ query }) => { + const humanResponse = interrupt({ query }); + return humanResponse.data; + }, + { + name: "humanAssistance", + description: "Request assistance from a human.", + schema: z.object({ + query: z.string().describe("Human readable question for the human"), + }), + } +); +const humanAssistance = tool( + async ({ query }) => { + const humanResponse = interrupt({ query }); + return humanResponse.data; + }, + { + name: "humanAssistance", + description: "Request assistance from a human.", + schema: z.object({ + query: z.string().describe("Human readable question for the human"), + }), + } +); + +const searchTool = new TavilySearch({ maxResults: 2 }); +const searchTool = new TavilySearch({ maxResults: 2 }); +const tools = [searchTool, humanAssistance]; + +const llmWithTools = new ChatAnthropic({ + model: "claude-3-5-sonnet-latest", +}).bindTools(tools); +const llmWithTools = new ChatAnthropic({ + model: "claude-3-5-sonnet-latest", +}).bindTools(tools); + +const chatbot = async (state: z.infer<typeof MessagesZodState>) => { +const chatbot = async (state: z.infer<typeof MessagesZodState>) => { + const message = await llmWithTools.invoke(state.messages); + + // Because we will be interrupting during tool execution, + // we disable parallel tool calling to avoid repeating any + // tool invocations when we resume. + + // Because we will be interrupting during tool execution, + // we disable parallel tool calling to avoid repeating any + // tool invocations when we resume. + if (message.tool_calls && message.tool_calls.length > 1) { + throw new Error("Multiple tool calls not supported with interrupts"); + } + + return { messages: message }; + + return { messages: message }; +}; + +const memory = new MemorySaver(); + +const graph = new StateGraph(MessagesZodState) + .addNode("chatbot", chatbot) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); + +const graph = new StateGraph(MessagesZodState) + .addNode("chatbot", chatbot) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); +``` + +::: + ## Next steps -So far, the tutorial examples have relied on a simple state with one entry: a list of messages. You can go far with this simple state, but if you want to define complex behavior without relying on the message list, you can [add additional fields to the state](./5-customize-state.md). \ No newline at end of file +So far, the tutorial examples have relied on a simple state with one entry: a list of messages. You can go far with this simple state, but if you want to define complex behavior without relying on the message list, you can [add additional fields to the state](./5-customize-state.md). diff --git a/docs/docs/tutorials/get-started/5-customize-state.md b/docs/docs/tutorials/get-started/5-customize-state.md index 8be1a9246..7b2b0515f 100644 --- a/docs/docs/tutorials/get-started/5-customize-state.md +++ b/docs/docs/tutorials/get-started/5-customize-state.md @@ -10,6 +10,8 @@ In this tutorial, you will add additional fields to the state to define complex Update the chatbot to research the birthday of an entity by adding `name` and `birthday` keys to the state: +:::python + ```python from typing import Annotated @@ -26,13 +28,34 @@ class State(TypedDict): birthday: str ``` +::: + +:::js + +```typescript +import { MessagesZodState } from "@langchain/langgraph"; +import { z } from "zod"; + +const State = z.object({ + messages: MessagesZodState.shape.messages, + // highlight-next-line + name: z.string(), + // highlight-next-line + birthday: z.string(), +}); +``` + +::: + Adding this information to the state makes it easily accessible by other graph nodes (like a downstream node that stores or processes the information), as well as the graph's persistence layer. ## 2. Update the state inside the tool +:::python + Now, populate the state keys inside of the `human_assistance` tool. This allows a human to review the information before it is stored in the state. Use [`Command`](../../concepts/low_level.md#using-inside-tools) to issue a state update from inside the tool. -``` python +```python from langchain_core.messages import ToolMessage from langchain_core.tools import InjectedToolCallId, tool @@ -76,10 +99,78 @@ def human_assistance( return Command(update=state_update) ``` +::: + +:::js + +Now, populate the state keys inside of the `humanAssistance` tool. This allows a human to review the information before it is stored in the state. Use [`Command`](../../concepts/low_level.md#using-inside-tools) to issue a state update from inside the tool. + +```typescript +import { tool } from "@langchain/core/tools"; +import { ToolMessage } from "@langchain/core/messages"; +import { Command, interrupt } from "@langchain/langgraph"; + +const humanAssistance = tool( + async (input, config) => { + // Note that because we are generating a ToolMessage for a state update, + // we generally require the ID of the corresponding tool call. + // This is available in the tool's config. + const toolCallId = config?.toolCall?.id as string | undefined; + if (!toolCallId) throw new Error("Tool call ID is required"); + + const humanResponse = await interrupt({ + question: "Is this correct?", + name: input.name, + birthday: input.birthday, + }); + + // We explicitly update the state with a ToolMessage inside the tool. + const stateUpdate = (() => { + // If the information is correct, update the state as-is. + if (humanResponse.correct?.toLowerCase().startsWith("y")) { + return { + name: input.name, + birthday: input.birthday, + messages: [ + new ToolMessage({ content: "Correct", tool_call_id: toolCallId }), + ], + }; + } + + // Otherwise, receive information from the human reviewer. + return { + name: humanResponse.name || input.name, + birthday: humanResponse.birthday || input.birthday, + messages: [ + new ToolMessage({ + content: `Made a correction: ${JSON.stringify(humanResponse)}`, + tool_call_id: toolCallId, + }), + ], + }; + })(); + + // We return a Command object in the tool to update our state. + return new Command({ update: stateUpdate }); + }, + { + name: "humanAssistance", + description: "Request assistance from a human.", + schema: z.object({ + name: z.string().describe("The name of the entity"), + birthday: z.string().describe("The birthday/release date of the entity"), + }), + } +); +``` + +::: + The rest of the graph stays the same. ## 3. Prompt the chatbot +:::python Prompt the chatbot to look up the "birthday" of the LangGraph library and direct the chatbot to reach out to the `human_assistance` tool once it has the required information. By setting `name` and `birthday` in the arguments for the tool, you force the chatbot to generate proposals for these fields. ```python @@ -99,6 +190,51 @@ for event in events: event["messages"][-1].pretty_print() ``` +::: + +:::js +Prompt the chatbot to look up the "birthday" of the LangGraph library and direct the chatbot to reach out to the `humanAssistance` tool once it has the required information. By setting `name` and `birthday` in the arguments for the tool, you force the chatbot to generate proposals for these fields. + +```typescript +import { isAIMessage } from "@langchain/core/messages"; + +const userInput = + "Can you look up when LangGraph was released? " + + "When you have the answer, use the humanAssistance tool for review."; + +const events = await graph.stream( + { messages: [{ role: "user", content: userInput }] }, + { configurable: { thread_id: "1" }, streamMode: "values" } +); + +for await (const event of events) { + if ("messages" in event) { + const lastMessage = event.messages.at(-1); + + console.log( + "=".repeat(32), + `${lastMessage?.getType()} Message`, + "=".repeat(32) + ); + console.log(lastMessage?.text); + + if ( + lastMessage && + isAIMessage(lastMessage) && + lastMessage.tool_calls?.length + ) { + console.log("Tool Calls:"); + for (const call of lastMessage.tool_calls) { + console.log(` ${call.name} (${call.id})`); + console.log(` Args: ${JSON.stringify(call.args)}`); + } + } + } +} +``` + +::: + ``` ================================ Human Message ================================= @@ -126,12 +262,20 @@ Tool Calls: birthday: 2023-01-01 ``` +:::python We've hit the `interrupt` in the `human_assistance` tool again. +::: + +:::js +We've hit the `interrupt` in the `humanAssistance` tool again. +::: ## 4. Add human assistance The chatbot failed to identify the correct date, so supply it with information: +:::python + ```python human_command = Command( resume={ @@ -146,6 +290,53 @@ for event in events: event["messages"][-1].pretty_print() ``` +::: + +:::js + +```typescript +import { Command } from "@langchain/langgraph"; + +const humanCommand = new Command({ + resume: { + name: "LangGraph", + birthday: "Jan 17, 2024", + }, +}); + +const resumeEvents = await graph.stream(humanCommand, { + configurable: { thread_id: "1" }, + streamMode: "values", +}); + +for await (const event of resumeEvents) { + if ("messages" in event) { + const lastMessage = event.messages.at(-1); + + console.log( + "=".repeat(32), + `${lastMessage?.getType()} Message`, + "=".repeat(32) + ); + console.log(lastMessage?.text); + + if ( + lastMessage && + isAIMessage(lastMessage) && + lastMessage.tool_calls?.length + ) { + console.log("Tool Calls:"); + for (const call of lastMessage.tool_calls) { + console.log(` ${call.name} (${call.id})`); + console.log(` Args: ${JSON.stringify(call.args)}`); + } + } + } +} +``` + +::: + ``` ================================== Ai Message ================================== @@ -175,6 +366,8 @@ It's worth noting that LangGraph had been in development and use for some time b Note that these fields are now reflected in the state: +:::python + ```python snapshot = graph.get_state(config) @@ -185,13 +378,34 @@ snapshot = graph.get_state(config) {'name': 'LangGraph', 'birthday': 'Jan 17, 2024'} ``` +::: + +:::js + +```typescript +const snapshot = await graph.getState(config); + +const relevantState = Object.fromEntries( + Object.entries(snapshot.values).filter(([k]) => + ["name", "birthday"].includes(k) + ) +); +``` + +``` +{ name: 'LangGraph', birthday: 'Jan 17, 2024' } +``` + +::: + This makes them easily accessible to downstream nodes (e.g., a node that further processes or stores the information). ## 5. Manually update the state +:::python LangGraph gives a high degree of control over the application state. For instance, at any point (including when interrupted), you can manually override a key using `graph.update_state`: -``` python +```python graph.update_state(config, {"name": "LangGraph (library)"}) ``` @@ -201,11 +415,36 @@ graph.update_state(config, {"name": "LangGraph (library)"}) 'checkpoint_id': '1efd4ec5-cf69-6352-8006-9278f1730162'}} ``` +::: + +:::js +LangGraph gives a high degree of control over the application state. For instance, at any point (including when interrupted), you can manually override a key using `graph.updateState`: + +```typescript +await graph.updateState( + { configurable: { thread_id: "1" } }, + { name: "LangGraph (library)" } +); +``` + +```typescript +{ + configurable: { + thread_id: '1', + checkpoint_ns: '', + checkpoint_id: '1efd4ec5-cf69-6352-8006-9278f1730162' + } +} +``` + +::: + ## 6. View the new value +:::python If you call `graph.get_state`, you can see the new value is reflected: -``` python +```python snapshot = graph.get_state(config) {k: v for k, v in snapshot.values.items() if k in ("name", "birthday")} @@ -215,12 +454,35 @@ snapshot = graph.get_state(config) {'name': 'LangGraph (library)', 'birthday': 'Jan 17, 2024'} ``` +::: + +:::js +If you call `graph.getState`, you can see the new value is reflected: + +```typescript +const updatedSnapshot = await graph.getState(config); + +const updatedRelevantState = Object.fromEntries( + Object.entries(updatedSnapshot.values).filter(([k]) => + ["name", "birthday"].includes(k) + ) +); +``` + +```typescript +{ name: 'LangGraph (library)', birthday: 'Jan 17, 2024' } +``` + +::: + Manual state updates will [generate a trace](https://smith.langchain.com/public/7ebb7827-378d-49fe-9f6c-5df0e90086c8/r) in LangSmith. If desired, they can also be used to [control human-in-the-loop workflows](../../how-tos/human_in_the_loop/add-human-in-the-loop.md). Use of the `interrupt` function is generally recommended instead, as it allows data to be transmitted in a human-in-the-loop interaction independently of state updates. **Congratulations!** You've added custom keys to the state to facilitate a more complex workflow, and learned how to generate state updates from inside tools. Check out the code snippet below to review the graph from this tutorial: +:::python + {% include-markdown "../../../snippets/chat_model_tabs.md" %} <!--- @@ -305,7 +567,111 @@ memory = InMemorySaver() graph = graph_builder.compile(checkpointer=memory) ``` +::: + +:::js + +```typescript +import { + Command, + interrupt, + MessagesZodState, + MemorySaver, + StateGraph, + END, + START, +} from "@langchain/langgraph"; +import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt"; +import { ChatAnthropic } from "@langchain/anthropic"; +import { TavilySearch } from "@langchain/tavily"; +import { ToolMessage } from "@langchain/core/messages"; +import { tool } from "@langchain/core/tools"; +import { z } from "zod"; + +const State = z.object({ + messages: MessagesZodState.shape.messages, + name: z.string(), + birthday: z.string(), +}); + +const humanAssistance = tool( + async (input, config) => { + // Note that because we are generating a ToolMessage for a state update, we + // generally require the ID of the corresponding tool call. This is available + // in the tool's config. + const toolCallId = config?.toolCall?.id as string | undefined; + if (!toolCallId) throw new Error("Tool call ID is required"); + + const humanResponse = await interrupt({ + question: "Is this correct?", + name: input.name, + birthday: input.birthday, + }); + + // We explicitly update the state with a ToolMessage inside the tool. + const stateUpdate = (() => { + // If the information is correct, update the state as-is. + if (humanResponse.correct?.toLowerCase().startsWith("y")) { + return { + name: input.name, + birthday: input.birthday, + messages: [ + new ToolMessage({ content: "Correct", tool_call_id: toolCallId }), + ], + }; + } + + // Otherwise, receive information from the human reviewer. + return { + name: humanResponse.name || input.name, + birthday: humanResponse.birthday || input.birthday, + messages: [ + new ToolMessage({ + content: `Made a correction: ${JSON.stringify(humanResponse)}`, + tool_call_id: toolCallId, + }), + ], + }; + })(); + + // We return a Command object in the tool to update our state. + return new Command({ update: stateUpdate }); + }, + { + name: "humanAssistance", + description: "Request assistance from a human.", + schema: z.object({ + name: z.string().describe("The name of the entity"), + birthday: z.string().describe("The birthday/release date of the entity"), + }), + } +); + +const searchTool = new TavilySearch({ maxResults: 2 }); + +const tools = [searchTool, humanAssistance]; +const llmWithTools = new ChatAnthropic({ + model: "claude-3-5-sonnet-latest", +}).bindTools(tools); + +const memory = new MemorySaver(); + +const chatbot = async (state: z.infer<typeof State>) => { + const message = await llmWithTools.invoke(state.messages); + return { messages: message }; +}; + +const graph = new StateGraph(State) + .addNode("chatbot", chatbot) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); +``` + +::: + ## Next steps -There's one more concept to review before finishing the LangGraph basics tutorials: connecting `checkpointing` and `state updates` to [time travel](./6-time-travel.md). - +There's one more concept to review before finishing the LangGraph basics tutorials: connecting `checkpointing` and `state updates` to [time travel](./6-time-travel.md). diff --git a/docs/docs/tutorials/get-started/6-time-travel.md b/docs/docs/tutorials/get-started/6-time-travel.md index eabb46e4f..f4340c0b4 100644 --- a/docs/docs/tutorials/get-started/6-time-travel.md +++ b/docs/docs/tutorials/get-started/6-time-travel.md @@ -4,7 +4,7 @@ In a typical chatbot workflow, the user interacts with the bot one or more times What if you want a user to be able to start from a previous response and explore a different outcome? Or what if you want users to be able to rewind your chatbot's work to fix mistakes or try a different strategy, something that is common in applications like autonomous software engineers? -You can create these types of experiences using LangGraph's built-in **time travel** functionality. +You can create these types of experiences using LangGraph's built-in **time travel** functionality. !!! note @@ -12,7 +12,15 @@ You can create these types of experiences using LangGraph's built-in **time trav ## 1. Rewind your graph +:::python Rewind your graph by fetching a checkpoint using the graph's `get_state_history` method. You can then resume execution at this previous point in time. +::: + +:::js +Rewind your graph by fetching a checkpoint using the graph's `getStateHistory` method. You can then resume execution at this previous point in time. +::: + +:::python {% include-markdown "../../../snippets/chat_model_tabs.md" %} @@ -64,11 +72,49 @@ memory = InMemorySaver() graph = graph_builder.compile(checkpointer=memory) ``` +::: + +:::js + +```typescript +import { + StateGraph, + START, + END, + MessagesZodState, + MemorySaver, +} from "@langchain/langgraph"; +import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt"; +import { TavilySearch } from "@langchain/tavily"; +import { ChatOpenAI } from "@langchain/openai"; +import { z } from "zod"; + +const State = z.object({ messages: MessagesZodState.shape.messages }); + +const tools = [new TavilySearch({ maxResults: 2 })]; +const llmWithTools = new ChatOpenAI({ model: "gpt-4o-mini" }).bindTools(tools); +const memory = new MemorySaver(); + +const graph = new StateGraph(State) + .addNode("chatbot", async (state) => ({ + messages: [await llmWithTools.invoke(state.messages)], + })) + .addNode("tools", new ToolNode(tools)) + .addConditionalEdges("chatbot", toolsCondition, ["tools", END]) + .addEdge("tools", "chatbot") + .addEdge(START, "chatbot") + .compile({ checkpointer: memory }); +``` + +::: + ## 2. Add steps Add steps to your graph. Every step will be checkpointed in its state history: -``` python +:::python + +```python config = {"configurable": {"thread_id": "1"}} events = graph.stream( { @@ -159,7 +205,7 @@ Tool Calls: ================================= Tool Message ================================= Name: tavily_search_results_json -[{"url": "https://towardsdatascience.com/building-autonomous-multi-tool-agents-with-gemini-2-0-and-langgraph-ad3d7bd5e79d", "content": "Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph | by Youness Mansar | Jan, 2025 | Towards Data Science Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph A practical tutorial with full code examples for building and running multi-tool agents Towards Data Science LLMs are remarkable — they can memorize vast amounts of information, answer general knowledge questions, write code, generate stories, and even fix your grammar. In this tutorial, we are going to build a simple LLM agent that is equipped with four tools that it can use to answer a user’s question. This Agent will have the following specifications: Follow Published in Towards Data Science --------------------------------- Your home for data science and AI. Follow Follow Follow"}, {"url": "https://github.com/anmolaman20/Tools_and_Agents", "content": "GitHub - anmolaman20/Tools_and_Agents: This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository serves as a comprehensive guide for building AI-powered agents using Langchain and Langgraph. It provides hands-on examples, practical tutorials, and resources for developers and AI enthusiasts to master building intelligent systems and workflows. AI Agent Development: Gain insights into creating intelligent systems that think, reason, and adapt in real time. This repository is ideal for AI practitioners, developers exploring language models, or anyone interested in building intelligent systems. This repository provides resources for building AI agents using Langchain and Langgraph."}] +[{"url": "https://towardsdatascience.com/building-autonomous-multi-tool-agents-with-gemini-2-0-and-langgraph-ad3d7bd5e79d", "content": "Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph | by Youness Mansar | Jan, 2025 | Towards Data Science Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph A practical tutorial with full code examples for building and running multi-tool agents Towards Data Science LLMs are remarkable — they can memorize vast amounts of information, answer general knowledge questions, write code, generate stories, and even fix your grammar. In this tutorial, we are going to build a simple LLM agent that is equipped with four tools that it can use to answer a user's question. This Agent will have the following specifications: Follow Published in Towards Data Science --------------------------------- Your home for data science and AI. Follow Follow Follow"}, {"url": "https://github.com/anmolaman20/Tools_and_Agents", "content": "GitHub - anmolaman20/Tools_and_Agents: This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository serves as a comprehensive guide for building AI-powered agents using Langchain and Langgraph. It provides hands-on examples, practical tutorials, and resources for developers and AI enthusiasts to master building intelligent systems and workflows. AI Agent Development: Gain insights into creating intelligent systems that think, reason, and adapt in real time. This repository is ideal for AI practitioners, developers exploring language models, or anyone interested in building intelligent systems. This repository provides resources for building AI agents using Langchain and Langgraph."}] ================================== Ai Message ================================== Great idea! Building an autonomous agent with LangGraph is definitely an exciting project. Based on the latest information I've found, here are some insights and tips for building autonomous agents with LangGraph: @@ -177,11 +223,140 @@ Building an autonomous agent is an iterative process, so be prepared to refine a Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings... ``` +::: + +:::js + +```typescript +import { randomUUID } from "node:crypto"; +const threadId = randomUUID(); + +let iter = 0; + +for (const userInput of [ + "I'm learning LangGraph. Could you do some research on it for me?", + "Ya that's helpful. Maybe I'll build an autonomous agent with it!", +]) { + iter += 1; + + console.log(`\n--- Conversation Turn ${iter} ---\n`); + const events = await graph.stream( + { messages: [{ role: "user", content: userInput }] }, + { configurable: { thread_id: threadId }, streamMode: "values" } + ); + + for await (const event of events) { + if ("messages" in event) { + const lastMessage = event.messages.at(-1); + + console.log( + "=".repeat(32), + `${lastMessage?.getType()} Message`, + "=".repeat(32) + ); + console.log(lastMessage?.text); + } + } +} +``` + +``` +--- Conversation Turn 1 --- + +================================ human Message ================================ +I'm learning LangGraph.js. Could you do some research on it for me? +================================ ai Message ================================ +I'll search for information about LangGraph.js for you. +================================ tool Message ================================ +{ + "query": "LangGraph.js framework TypeScript langchain what is it tutorial guide", + "follow_up_questions": null, + "answer": null, + "images": [], + "results": [ + { + "url": "https://techcommunity.microsoft.com/blog/educatordeveloperblog/an-absolute-beginners-guide-to-langgraph-js/4212496", + "title": "An Absolute Beginner's Guide to LangGraph.js", + "content": "(...)", + "score": 0.79369855, + "raw_content": null + }, + { + "url": "https://langchain-ai.github.io/langgraphjs/", + "title": "LangGraph.js", + "content": "(...)", + "score": 0.78154784, + "raw_content": null + } + ], + "response_time": 2.37 +} +================================ ai Message ================================ +Let me provide you with an overview of LangGraph.js based on the search results: + +LangGraph.js is a JavaScript/TypeScript library that's part of the LangChain ecosystem, specifically designed for creating and managing complex LLM (Large Language Model) based workflows. Here are the key points about LangGraph.js: + +1. Purpose: +- It's a low-level orchestration framework for building controllable agents +- Particularly useful for creating agentic workflows where LLMs decide the course of action based on current state +- Helps model workflows as graphs with nodes and edges + +(...) + +--- Conversation Turn 2 --- + +================================ human Message ================================ +Ya that's helpful. Maybe I'll build an autonomous agent with it! +================================ ai Message ================================ +Let me search for specific information about building autonomous agents with LangGraph.js. +================================ tool Message ================================ +{ + "query": "how to build autonomous agents with LangGraph.js examples tutorial react agent", + "follow_up_questions": null, + "answer": null, + "images": [], + "results": [ + { + "url": "https://ai.google.dev/gemini-api/docs/langgraph-example", + "title": "ReAct agent from scratch with Gemini 2.5 and LangGraph", + "content": "(...)", + "score": 0.7602419, + "raw_content": null + }, + { + "url": "https://www.youtube.com/watch?v=ZfjaIshGkmk", + "title": "Build Autonomous AI Agents with ReAct and LangGraph Tools", + "content": "(...)", + "score": 0.7471924, + "raw_content": null + } + ], + "response_time": 1.98 +} +================================ ai Message ================================ +Based on the search results, I can provide you with a practical overview of how to build an autonomous agent with LangGraph.js. Here's what you need to know: + +1. Basic Structure for Building an Agent: +- LangGraph.js provides a ReAct (Reason + Act) pattern implementation +- The basic components include: + - State management for conversation history + - Nodes for different actions + - Edges for decision-making flow + - Tools for specific functionalities + +(...) + +``` + +::: + ## 3. Replay the full state history Now that you have added steps to the chatbot, you can `replay` the full state history to see everything that occurred. -``` python +:::python + +```python to_replay = None for state in graph.get_state_history(config): print("Num Messages: ", len(state.values["messages"]), "Next: ", state.next) @@ -214,11 +389,73 @@ Num Messages: 0 Next: ('__start__',) -------------------------------------------------------------------------------- ``` -Checkpoints are saved for every step of the graph. This __spans invocations__ so you can rewind across a full thread's history. +::: + +:::js + +```typescript +import type { StateSnapshot } from "@langchain/langgraph"; + +let toReplay: StateSnapshot | undefined; +for await (const state of graph.getStateHistory({ + configurable: { thread_id: threadId }, +})) { + console.log( + `Num Messages: ${state.values.messages.length}, Next: ${JSON.stringify( + state.next + )}` + ); + console.log("-".repeat(80)); + if (state.values.messages.length === 6) { + // We are somewhat arbitrarily selecting a specific state based on the number of chat messages in the state. + toReplay = state; + } +} +``` + +``` +Num Messages: 8 Next: [] +-------------------------------------------------------------------------------- +Num Messages: 7 Next: ["chatbot"] +-------------------------------------------------------------------------------- +Num Messages: 6 Next: ["tools"] +-------------------------------------------------------------------------------- +Num Messages: 7, Next: ["chatbot"] +-------------------------------------------------------------------------------- +Num Messages: 6, Next: ["tools"] +-------------------------------------------------------------------------------- +Num Messages: 5, Next: ["chatbot"] +-------------------------------------------------------------------------------- +Num Messages: 4, Next: ["__start__"] +-------------------------------------------------------------------------------- +Num Messages: 4, Next: [] +-------------------------------------------------------------------------------- +Num Messages: 3, Next: ["chatbot"] +-------------------------------------------------------------------------------- +Num Messages: 2, Next: ["tools"] +-------------------------------------------------------------------------------- +Num Messages: 1, Next: ["chatbot"] +-------------------------------------------------------------------------------- +Num Messages: 0, Next: ["__start__"] +-------------------------------------------------------------------------------- +``` + +::: + +Checkpoints are saved for every step of the graph. This **spans invocations** so you can rewind across a full thread's history. ## Resume from a checkpoint +:::python + Resume from the `to_replay` state, which is after the `chatbot` node in the second graph invocation. Resuming from this point will call the **action** node next. +::: + +:::js +Resume from the `toReplay` state, which is after a specific node in one of the graph invocations. Resuming from this point will call the next scheduled node. +::: + +:::python ```python print(to_replay.next) @@ -230,12 +467,37 @@ print(to_replay.config) {'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1efd43e3-0c1f-6c4e-8006-891877d65740'}} ``` +::: + +:::js + +Resume from the `toReplay` state, which is after the `chatbot` node in one of the graph invocations. Resuming from this point will call the next scheduled node. + +```typescript +console.log(toReplay.next); +console.log(toReplay.config); +``` + +``` +["tools"] +{ + configurable: { + thread_id: "007708b8-ea9b-4ff7-a7ad-3843364dbf75", + checkpoint_ns: "", + checkpoint_id: "1efd43e3-0c1f-6c4e-8006-891877d65740" + } +} +``` + +::: + ## 4. Load a state from a moment-in-time +:::python + The checkpoint's `to_replay.config` contains a `checkpoint_id` timestamp. Providing this `checkpoint_id` value tells LangGraph's checkpointer to **load** the state from that moment in time. - -``` python +```python # The `checkpoint_id` in the `to_replay.config` corresponds to a state we've persisted to our checkpointer. for event in graph.stream(None, to_replay.config, stream_mode="values"): if "messages" in event: @@ -254,19 +516,16 @@ Tool Calls: ================================= Tool Message ================================= Name: tavily_search_results_json -[{"url": "https://towardsdatascience.com/building-autonomous-multi-tool-agents-with-gemini-2-0-and-langgraph-ad3d7bd5e79d", "content": "Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph | by Youness Mansar | Jan, 2025 | Towards Data Science Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph A practical tutorial with full code examples for building and running multi-tool agents Towards Data Science LLMs are remarkable — they can memorize vast amounts of information, answer general knowledge questions, write code, generate stories, and even fix your grammar. In this tutorial, we are going to build a simple LLM agent that is equipped with four tools that it can use to answer a user’s question. This Agent will have the following specifications: Follow Published in Towards Data Science --------------------------------- Your home for data science and AI. Follow Follow Follow"}, {"url": "https://github.com/anmolaman20/Tools_and_Agents", "content": "GitHub - anmolaman20/Tools_and_Agents: This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository serves as a comprehensive guide for building AI-powered agents using Langchain and Langgraph. It provides hands-on examples, practical tutorials, and resources for developers and AI enthusiasts to master building intelligent systems and workflows. AI Agent Development: Gain insights into creating intelligent systems that think, reason, and adapt in real time. This repository is ideal for AI practitioners, developers exploring language models, or anyone interested in building intelligent systems. This repository provides resources for building AI agents using Langchain and Langgraph."}] +[{"url": "https://towardsdatascience.com/building-autonomous-multi-tool-agents-with-gemini-2-0-and-langgraph-ad3d7bd5e79d", "content": "Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph | by Youness Mansar | Jan, 2025 | Towards Data Science Building Autonomous Multi-Tool Agents with Gemini 2.0 and LangGraph A practical tutorial with full code examples for building and running multi-tool agents Towards Data Science LLMs are remarkable — they can memorize vast amounts of information, answer general knowledge questions, write code, generate stories, and even fix your grammar. In this tutorial, we are going to build a simple LLM agent that is equipped with four tools that it can use to answer a user's question. This Agent will have the following specifications: Follow Published in Towards Data Science --------------------------------- Your home for data science and AI. Follow Follow Follow"}, {"url": "https://github.com/anmolaman20/Tools_and_Agents", "content": "GitHub - anmolaman20/Tools_and_Agents: This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository provides resources for building AI agents using Langchain and Langgraph. This repository serves as a comprehensive guide for building AI-powered agents using Langchain and Langgraph. It provides hands-on examples, practical tutorials, and resources for developers and AI enthusiasts to master building intelligent systems and workflows. AI Agent Development: Gain insights into creating intelligent systems that think, reason, and adapt in real time. This repository is ideal for AI practitioners, developers exploring language models, or anyone interested in building intelligent systems. This repository provides resources for building AI agents using Langchain and Langgraph."}] ================================== Ai Message ================================== -Great idea! Building an autonomous agent with LangGraph is indeed an excellent way to apply and deepen your understanding of the technology. Based on the search results, I can provide you with some insights and resources to help you get started: +Great idea! Building an autonomous agent with LangGraph is definitely an exciting project. Based on the latest information I've found, here are some insights and tips for building autonomous agents with LangGraph: -1. Multi-Tool Agents: - LangGraph is well-suited for building autonomous agents that can use multiple tools. This allows your agent to have a variety of capabilities and choose the appropriate tool based on the task at hand. +1. Multi-Tool Agents: LangGraph is particularly well-suited for creating autonomous agents that can use multiple tools. This allows your agent to have a diverse set of capabilities and choose the right tool for each task. -2. Integration with Large Language Models (LLMs): - There's a tutorial that specifically mentions using Gemini 2.0 (Google's LLM) with LangGraph to build autonomous agents. This suggests that LangGraph can be integrated with various LLMs, giving you flexibility in choosing the language model that best fits your needs. +2. Integration with Large Language Models (LLMs): You can combine LangGraph with powerful LLMs like Gemini 2.0 to create more intelligent and capable agents. The LLM can serve as the "brain" of your agent, making decisions and generating responses. -3. Practical Tutorials: - There are tutorials available that provide full code examples for building and running multi-tool agents. These can be invaluable as you start your project, giving you a concrete starting point and demonstrating best practices. +3. Workflow Management: LangGraph excels at managing complex, multi-step AI workflows. This is crucial for autonomous agents that need to break down tasks into smaller steps and execute them in the right order. ... Remember, building an autonomous agent is an iterative process. Start simple and gradually increase complexity as you become more comfortable with LangGraph and its capabilities. @@ -275,7 +534,83 @@ Would you like more information on any specific aspect of building your autonomo Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings... ``` -The graph resumed execution from the `action` node. You can tell this is the case since the first value printed above is the response from our search engine tool. +The graph resumed execution from the `tools` node. You can tell this is the case since the first value printed above is the response from our search engine tool. +::: + +:::js + +The checkpoint's `toReplay.config` contains a `checkpoint_id` timestamp. Providing this `checkpoint_id` value tells LangGraph's checkpointer to **load** the state from that moment in time. + +```typescript +// The `checkpoint_id` in the `toReplay.config` corresponds to a state we've persisted to our checkpointer. +for await (const event of await graph.stream(null, { + ...toReplay?.config, + streamMode: "values", +})) { + if ("messages" in event) { + const lastMessage = event.messages.at(-1); + + console.log( + "=".repeat(32), + `${lastMessage?.getType()} Message`, + "=".repeat(32) + ); + console.log(lastMessage?.text); + } +} +``` + +``` +================================ ai Message ================================ +Let me search for specific information about building autonomous agents with LangGraph.js. +================================ tool Message ================================ +{ + "query": "how to build autonomous agents with LangGraph.js examples tutorial", + "follow_up_questions": null, + "answer": null, + "images": [], + "results": [ + { + "url": "https://www.mongodb.com/developer/languages/typescript/build-javascript-ai-agent-langgraphjs-mongodb/", + "title": "Build a JavaScript AI Agent With LangGraph.js and MongoDB", + "content": "(...)", + "score": 0.7672197, + "raw_content": null + }, + { + "url": "https://medium.com/@lorevanoudenhove/how-to-build-ai-agents-with-langgraph-a-step-by-step-guide-5d84d9c7e832", + "title": "How to Build AI Agents with LangGraph: A Step-by-Step Guide", + "content": "(...)", + "score": 0.7407191, + "raw_content": null + } + ], + "response_time": 0.82 +} +================================ ai Message ================================ +Based on the search results, I can share some practical information about building autonomous agents with LangGraph.js. Here are some concrete examples and approaches: + +1. Example HR Assistant Agent: +- Can handle HR-related queries using employee information +- Features include: + - Starting and continuing conversations + - Looking up information using vector search + - Persisting conversation state using checkpoints + - Managing threaded conversations + +2. Energy Savings Calculator Agent: +- Functions as a lead generation tool for solar panel sales +- Capabilities include: + - Calculating potential energy savings + - Handling multi-step conversations + - Processing user inputs for personalized estimates + - Managing conversation state + +(...) +``` + +The graph resumed execution from the `tools` node. You can tell this is the case since the first value printed above is the response from our search engine tool. +::: **Congratulations!** You've now used time-travel checkpoint traversal in LangGraph. Being able to rewind and explore alternative paths opens up a world of possibilities for debugging, experimentation, and interactive applications. @@ -285,4 +620,4 @@ Take your LangGraph journey further by exploring deployment and advanced feature - **[LangGraph Server quickstart](../../tutorials/langgraph-platform/local-server.md)**: Launch a LangGraph server locally and interact with it using the REST API and LangGraph Studio Web UI. - **[LangGraph Platform quickstart](../../cloud/quick_start.md)**: Deploy your LangGraph app using LangGraph Platform. -- **[LangGraph Platform concepts](../../concepts/langgraph_platform.md)**: Understand the foundational concepts of the LangGraph Platform. \ No newline at end of file +- **[LangGraph Platform concepts](../../concepts/langgraph_platform.md)**: Understand the foundational concepts of the LangGraph Platform. diff --git a/docs/docs/tutorials/langgraph-platform/local-server.md b/docs/docs/tutorials/langgraph-platform/local-server.md index 70c9478c6..f749f248d 100644 --- a/docs/docs/tutorials/langgraph-platform/local-server.md +++ b/docs/docs/tutorials/langgraph-platform/local-server.md @@ -10,57 +10,69 @@ Before you begin, ensure you have the following: ## 1. Install the LangGraph CLI -=== "Python server" +:::python - Python >= 3.11 is required. +```shell +# Python >= 3.11 is required. - ```shell - pip install --upgrade "langgraph-cli[inmem]" - ``` +pip install --upgrade "langgraph-cli[inmem]" +``` -=== "Node server" +::: - ```shell - npx @langchain/langgraph-cli - ``` +:::js + +```shell +npx @langchain/langgraph-cli +``` + +::: ## 2. Create a LangGraph app 🌱 -Create a new app from the [`new-langgraph-project-python` template](https://github.com/langchain-ai/new-langgraph-project) or [`new-langgraph-project-js` template](https://github.com/langchain-ai/new-langgraphjs-project). This template demonstrates a single-node application you can extend with your own logic. +:::python +Create a new app from the [`new-langgraph-project-python` template](https://github.com/langchain-ai/new-langgraph-project). This template demonstrates a single-node application you can extend with your own logic. -=== "Python server" - - ```shell - langgraph new path/to/your/app --template new-langgraph-project-python - ``` - -=== "Node server" - - ```shell - langgraph new path/to/your/app --template new-langgraph-project-js - ``` +```shell +langgraph new path/to/your/app --template new-langgraph-project-python +``` !!! tip "Additional templates" If you use `langgraph new` without specifying a template, you will be presented with an interactive menu that will allow you to choose from a list of available templates. +::: + +:::js +Create a new app from the [`new-langgraph-project-js` template](https://github.com/langchain-ai/new-langgraphjs-project). This template demonstrates a single-node application you can extend with your own logic. + +```shell +npm create langgraph +``` + +::: + ## 3. Install dependencies In the root of your new LangGraph app, install the dependencies in `edit` mode so your local changes are used by the server: -=== "Python server" +:::python - ```shell - cd path/to/your/app - pip install -e . - ``` +```shell +cd path/to/your/app +pip install -e . +``` -=== "Node server" +::: - ```shell - cd path/to/your/app - yarn install - ``` +:::js + +```shell +cd path/to/your/app +npm install +``` + +::: ## 4. Create a `.env` file @@ -74,17 +86,21 @@ LANGSMITH_API_KEY=lsv2... Start the LangGraph API server locally: -=== "Python server" +:::python - ```shell - langgraph dev - ``` +```shell +langgraph dev +``` -=== "Node server" +::: - ```shell - npx @langchain/langgraph-cli dev - ``` +:::js + +```shell +npx @langchain/langgraph-cli dev +``` + +::: Sample output: @@ -120,6 +136,7 @@ For a LangGraph Server running on a custom host/port, update the baseURL paramet ## 7. Test the API +:::python === "Python SDK (async)" 1. Install the LangGraph Python SDK: @@ -185,7 +202,29 @@ For a LangGraph Server running on a custom host/port, update the baseURL paramet print("\n\n") ``` +=== "Rest API" + ```bash + curl -s --request POST \ + --url "http://localhost:2024/runs/stream" \ + --header 'Content-Type: application/json' \ + --data "{ + \"assistant_id\": \"agent\", + \"input\": { + \"messages\": [ + { + \"role\": \"human\", + \"content\": \"What is LangGraph?\" + } + ] + }, + \"stream_mode\": \"messages-tuple\" + }" + ``` + +::: + +:::js === "Javascript SDK" 1. Install the LangGraph JS SDK: @@ -242,6 +281,8 @@ For a LangGraph Server running on a custom host/port, update the baseURL paramet }" ``` +::: + ## Next steps Now that you have a LangGraph app running locally, take your journey further by exploring deployment and advanced features: @@ -249,5 +290,13 @@ Now that you have a LangGraph app running locally, take your journey further by - [Deployment quickstart](../../cloud/quick_start.md): Deploy your LangGraph app using LangGraph Platform. - [LangGraph Platform overview](../../concepts/langgraph_platform.md): Learn about foundational LangGraph Platform concepts. - [LangGraph Server API Reference](../../cloud/reference/api/api_ref.html): Explore the LangGraph Server API documentation. + +:::python + - [Python SDK Reference](../../cloud/reference/sdk/python_sdk_ref.md): Explore the Python SDK API Reference. + ::: + +:::js + - [JS/TS SDK Reference](../../cloud/reference/sdk/js_ts_sdk_ref.md): Explore the JS/TS SDK API Reference. + ::: diff --git a/docs/docs/tutorials/multi_agent/agent_supervisor.md b/docs/docs/tutorials/multi_agent/agent_supervisor.md index 71335c8db..7e12a7222 100644 --- a/docs/docs/tutorials/multi_agent/agent_supervisor.md +++ b/docs/docs/tutorials/multi_agent/agent_supervisor.md @@ -306,7 +306,7 @@ Name: math_agent ## 2. Create supervisor with `langgraph-supervisor` -To implement out multi-agent system, we will use [`create_supervisor`][langgraph_supervisor.supervisor.create_supervisor] from the prebuilt `langgraph-supervisor` library: +To implement out multi-agent system, we will use @[`create_supervisor`][create_supervisor] from the prebuilt `langgraph-supervisor` library: ```python from langgraph_supervisor import create_supervisor @@ -478,7 +478,7 @@ assign_to_math_agent = create_handoff_tool( ### Create supervisor agent -Then, let's create the supervisor agent with the handoff tools we just defined. We will use the prebuilt [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent]: +Then, let's create the supervisor agent with the handoff tools we just defined. We will use the prebuilt @[`create_react_agent`][create_react_agent]: ```python supervisor_agent = create_react_agent( @@ -654,7 +654,7 @@ Name: tavily_search !!! important You can see that the supervisor system appends **all** of the individual agent messages (i.e., their internal tool-calling loop) to the full message history. This means that on every supervisor turn, supervisor agent sees this full history. If you want more control over: - * **how inputs are passed to agents**: you can use LangGraph [`Send()`][langgraph.types.Send] primitive to directly send data to the worker agents during the handoff. See the [task delegation](#4-create-delegation-tasks) example below + * **how inputs are passed to agents**: you can use LangGraph @[`Send()`][Send] primitive to directly send data to the worker agents during the handoff. See the [task delegation](#4-create-delegation-tasks) example below * **how agent outputs are added**: you can control how much of the agent's internal message history is added to the overall supervisor message history by wrapping the agent in a separate node function: ```python @@ -742,7 +742,7 @@ supervisor_with_description = ( ``` !!! note - We're using [`Send()`][langgraph.types.Send] primitive in the `handoff_tool`. This means that instead of receiving the full `supervisor` graph state as input, each worker agent only sees the contents of the `Send` payload. In this example, we're sending the task description as a single "human" message. + We're using @[`Send()`][Send] primitive in the `handoff_tool`. This means that instead of receiving the full `supervisor` graph state as input, each worker agent only sees the contents of the `Send` payload. In this example, we're sending the task description as a single "human" message. Let's now running it with the same input query: diff --git a/docs/docs/tutorials/workflows.md b/docs/docs/tutorials/workflows.md index 8be5cf07b..06a6f7129 100644 --- a/docs/docs/tutorials/workflows.md +++ b/docs/docs/tutorials/workflows.md @@ -1,10 +1,11 @@ --- search: - boost: 2 + boost: 2 --- + # Workflows and Agents -This guide reviews common patterns for agentic systems. In describing these systems, it can be useful to make a distinction between "workflows" and "agents". One way to think about this difference is nicely explained in [Anthropic's](https://python.langchain.com/docs/integrations/providers/anthropic/) `Building Effective Agents` blog post: +This guide reviews common patterns for agentic systems. In describing these systems, it can be useful to make a distinction between "workflows" and "agents". One way to think about this difference is nicely explained in Anthropic's `Building Effective Agents` blog post: > Workflows are systems where LLMs and tools are orchestrated through predefined code paths. > Agents, on the other hand, are systems where LLMs dynamically direct their own processes and tool usage, maintaining control over how they accomplish tasks. @@ -17,12 +18,13 @@ When building agents and workflows, LangGraph offers a number of benefits includ ## Set up +:::python You can use [any chat model](https://python.langchain.com/docs/integrations/chat/) that supports structured outputs and tool calling. Below, we show the process of installing the packages, setting API keys, and testing structured outputs / tool calling for Anthropic. ??? "Install dependencies" ```bash - pip install langchain_core langchain-anthropic langgraph + pip install langchain_core langchain-anthropic langgraph ``` Initialize an LLM @@ -43,12 +45,36 @@ _set_env("ANTHROPIC_API_KEY") llm = ChatAnthropic(model="claude-3-5-sonnet-latest") ``` -## Building Blocks: The Augmented LLM +::: -LLM have augmentations that support building workflows and agents. These include [structured outputs](https://python.langchain.com/docs/concepts/structured_outputs/) and [tool calling](https://python.langchain.com/docs/concepts/tool_calling/), as shown in this image from the Anthropic blog on `Building Effective Agents`: +:::js +You can use [any chat model](https://js.langchain.com/docs/integrations/chat/) that supports structured outputs and tool calling. Below, we show the process of installing the packages, setting API keys, and testing structured outputs / tool calling for Anthropic. + +??? "Install dependencies" + + ```bash + npm install @langchain/core @langchain/anthropic @langchain/langgraph + ``` + +Initialize an LLM + +```typescript +import { ChatAnthropic } from "@langchain/anthropic"; + +process.env.ANTHROPIC_API_KEY = "YOUR_API_KEY"; + +const llm = new ChatAnthropic({ model: "claude-3-5-sonnet-latest" }); +``` + +::: + +## Building Blocks: The Augmented LLM + +LLM have augmentations that support building workflows and agents. These include structured outputs and tool calling, as shown in this image from the Anthropic blog on `Building Effective Agents`: ![augmented_llm.png](./workflows/img/augmented_llm.png) +:::python ```python # Schema for structured output @@ -81,13 +107,64 @@ msg = llm_with_tools.invoke("What is 2 times 3?") msg.tool_calls ``` +::: + +:::js + +```typescript +import { z } from "zod"; +import { tool } from "@langchain/core/tools"; + +// Schema for structured output +const SearchQuery = z.object({ + search_query: z.string().describe("Query that is optimized web search."), + justification: z + .string() + .describe("Why this query is relevant to the user's request."), +}); + +// Augment the LLM with schema for structured output +const structuredLlm = llm.withStructuredOutput(SearchQuery); + +// Invoke the augmented LLM +const output = await structuredLlm.invoke( + "How does Calcium CT score relate to high cholesterol?" +); + +// Define a tool +const multiply = tool( + async ({ a, b }: { a: number; b: number }) => { + return a * b; + }, + { + name: "multiply", + description: "Multiply two numbers", + schema: z.object({ + a: z.number(), + b: z.number(), + }), + } +); + +// Augment the LLM with tools +const llmWithTools = llm.bindTools([multiply]); + +// Invoke the LLM with input that triggers the tool call +const msg = await llmWithTools.invoke("What is 2 times 3?"); + +// Get the tool call +console.log(msg.tool_calls); +``` + +::: + ## Prompt chaining -In prompt chaining, each LLM call processes the output of the previous one. +In prompt chaining, each LLM call processes the output of the previous one. -As noted in the Anthropic blog on `Building Effective Agents`: +As noted in the Anthropic blog on `Building Effective Agents`: -> Prompt chaining decomposes a task into a sequence of steps, where each LLM call processes the output of the previous one. You can add programmatic checks (see "gate” in the diagram below) on any intermediate steps to ensure that the process is still on track. +> Prompt chaining decomposes a task into a sequence of steps, where each LLM call processes the output of the previous one. You can add programmatic checks (see "gate" in the diagram below) on any intermediate steps to ensure that the process is still on track. > When to use this workflow: This workflow is ideal for situations where the task can be easily and cleanly decomposed into fixed subtasks. The main goal is to trade off latency for higher accuracy, by making each LLM call an easier task. @@ -95,6 +172,7 @@ As noted in the Anthropic blog on `Building Effective Agents`: === "Graph API" + :::python ```python from typing_extensions import TypedDict from langgraph.graph import StateGraph, START, END @@ -187,9 +265,94 @@ As noted in the Anthropic blog on `Building Effective Agents`: **LangChain Academy** See our lesson on Prompt Chaining [here](https://github.com/langchain-ai/langchain-academy/blob/main/module-1/chain.ipynb). + ::: + + :::js + ```typescript + import { StateGraph, START, END } from "@langchain/langgraph"; + import { z } from "zod"; + + // Graph state + const State = z.object({ + topic: z.string(), + joke: z.string().optional(), + improved_joke: z.string().optional(), + final_joke: z.string().optional(), + }); + + // Nodes + const generateJoke = async (state: z.infer<typeof State>) => { + // First LLM call to generate initial joke + const msg = await llm.invoke(`Write a short joke about ${state.topic}`); + return { joke: msg.content }; + }; + + const checkPunchline = (state: z.infer<typeof State>) => { + // Gate function to check if the joke has a punchline + // Simple check - does the joke contain "?" or "!" + if (state.joke && (state.joke.includes("?") || state.joke.includes("!"))) { + return "Pass"; + } + return "Fail"; + }; + + const improveJoke = async (state: z.infer<typeof State>) => { + // Second LLM call to improve the joke + const msg = await llm.invoke(`Make this joke funnier by adding wordplay: ${state.joke}`); + return { improved_joke: msg.content }; + }; + + const polishJoke = async (state: z.infer<typeof State>) => { + // Third LLM call for final polish + const msg = await llm.invoke(`Add a surprising twist to this joke: ${state.improved_joke}`); + return { final_joke: msg.content }; + }; + + // Build workflow + const workflow = new StateGraph(State) + .addNode("generate_joke", generateJoke) + .addNode("improve_joke", improveJoke) + .addNode("polish_joke", polishJoke) + .addEdge(START, "generate_joke") + .addConditionalEdges( + "generate_joke", + checkPunchline, + { "Fail": "improve_joke", "Pass": END } + ) + .addEdge("improve_joke", "polish_joke") + .addEdge("polish_joke", END); + + // Compile + const chain = workflow.compile(); + + // Show workflow + import * as fs from "node:fs/promises"; + const drawableGraph = await chain.getGraphAsync(); + const image = await drawableGraph.drawMermaidPng(); + const imageBuffer = new Uint8Array(await image.arrayBuffer()); + await fs.writeFile("workflow.png", imageBuffer); + + // Invoke + const state = await chain.invoke({ topic: "cats" }); + console.log("Initial joke:"); + console.log(state.joke); + console.log("\n--- --- ---\n"); + if (state.improved_joke) { + console.log("Improved joke:"); + console.log(state.improved_joke); + console.log("\n--- --- ---\n"); + + console.log("Final joke:"); + console.log(state.final_joke); + } else { + console.log("Joke failed quality gate - no punchline detected!"); + } + ``` + ::: === "Functional API" + :::python ```python from langgraph.func import entrypoint, task @@ -243,12 +406,64 @@ As noted in the Anthropic blog on `Building Effective Agents`: **LangSmith Trace** https://smith.langchain.com/public/332fa4fc-b6ca-416e-baa3-161625e69163/r + ::: -## Parallelization + :::js + ```typescript + import { entrypoint, task } from "@langchain/langgraph"; + + // Tasks + const generateJoke = task("generate_joke", async (topic: string) => { + // First LLM call to generate initial joke + const msg = await llm.invoke(`Write a short joke about ${topic}`); + return msg.content; + }); + + const checkPunchline = (joke: string) => { + // Gate function to check if the joke has a punchline + // Simple check - does the joke contain "?" or "!" + if (joke.includes("?") || joke.includes("!")) { + return "Pass"; + } + return "Fail"; + }; + + const improveJoke = task("improve_joke", async (joke: string) => { + // Second LLM call to improve the joke + const msg = await llm.invoke(`Make this joke funnier by adding wordplay: ${joke}`); + return msg.content; + }); + + const polishJoke = task("polish_joke", async (joke: string) => { + // Third LLM call for final polish + const msg = await llm.invoke(`Add a surprising twist to this joke: ${joke}`); + return msg.content; + }); + + const promptChainingWorkflow = entrypoint("promptChainingWorkflow", async (topic: string) => { + const originalJoke = await generateJoke(topic); + if (checkPunchline(originalJoke) === "Pass") { + return originalJoke; + } + + const improvedJoke = await improveJoke(originalJoke); + return await polishJoke(improvedJoke); + }); + + // Invoke + const stream = await promptChainingWorkflow.stream("cats", { streamMode: "updates" }); + for await (const step of stream) { + console.log(step); + console.log("\n"); + } + ``` + ::: + +## Parallelization With parallelization, LLMs work simultaneously on a task: ->LLMs can sometimes work simultaneously on a task and have their outputs aggregated programmatically. This workflow, parallelization, manifests in two key variations: Sectioning: Breaking a task into independent subtasks run in parallel. Voting: Running the same task multiple times to get diverse outputs. +> LLMs can sometimes work simultaneously on a task and have their outputs aggregated programmatically. This workflow, parallelization, manifests in two key variations: Sectioning: Breaking a task into independent subtasks run in parallel. Voting: Running the same task multiple times to get diverse outputs. > When to use this workflow: Parallelization is effective when the divided subtasks can be parallelized for speed, or when multiple perspectives or attempts are needed for higher confidence results. For complex tasks with multiple considerations, LLMs generally perform better when each consideration is handled by a separate LLM call, allowing focused attention on each specific aspect. @@ -256,6 +471,7 @@ With parallelization, LLMs work simultaneously on a task: === "Graph API" + :::python ```python # Graph state class State(TypedDict): @@ -338,9 +554,72 @@ With parallelization, LLMs work simultaneously on a task: **LangChain Academy** See our lesson on parallelization [here](https://github.com/langchain-ai/langchain-academy/blob/main/module-1/simple-graph.ipynb). + ::: + + :::js + ```typescript + // Graph state + const State = z.object({ + topic: z.string(), + joke: z.string().optional(), + story: z.string().optional(), + poem: z.string().optional(), + combined_output: z.string().optional(), + }); + + // Nodes + const callLlm1 = async (state: z.infer<typeof State>) => { + // First LLM call to generate initial joke + const msg = await llm.invoke(`Write a joke about ${state.topic}`); + return { joke: msg.content }; + }; + + const callLlm2 = async (state: z.infer<typeof State>) => { + // Second LLM call to generate story + const msg = await llm.invoke(`Write a story about ${state.topic}`); + return { story: msg.content }; + }; + + const callLlm3 = async (state: z.infer<typeof State>) => { + // Third LLM call to generate poem + const msg = await llm.invoke(`Write a poem about ${state.topic}`); + return { poem: msg.content }; + }; + + const aggregator = (state: z.infer<typeof State>) => { + // Combine the joke and story into a single output + let combined = `Here's a story, joke, and poem about ${state.topic}!\n\n`; + combined += `STORY:\n${state.story}\n\n`; + combined += `JOKE:\n${state.joke}\n\n`; + combined += `POEM:\n${state.poem}`; + return { combined_output: combined }; + }; + + // Build workflow + const parallelBuilder = new StateGraph(State) + .addNode("call_llm_1", callLlm1) + .addNode("call_llm_2", callLlm2) + .addNode("call_llm_3", callLlm3) + .addNode("aggregator", aggregator) + .addEdge(START, "call_llm_1") + .addEdge(START, "call_llm_2") + .addEdge(START, "call_llm_3") + .addEdge("call_llm_1", "aggregator") + .addEdge("call_llm_2", "aggregator") + .addEdge("call_llm_3", "aggregator") + .addEdge("aggregator", END); + + const parallelWorkflow = parallelBuilder.compile(); + + // Invoke + const state = await parallelWorkflow.invoke({ topic: "cats" }); + console.log(state.combined_output); + ``` + ::: === "Functional API" + :::python ```python @task def call_llm_1(topic: str): @@ -393,10 +672,63 @@ With parallelization, LLMs work simultaneously on a task: **LangSmith Trace** https://smith.langchain.com/public/623d033f-e814-41e9-80b1-75e6abb67801/r + ::: + + :::js + ```typescript + const callLlm1 = task("call_llm_1", async (topic: string) => { + // First LLM call to generate initial joke + const msg = await llm.invoke(`Write a joke about ${topic}`); + return msg.content; + }); + + const callLlm2 = task("call_llm_2", async (topic: string) => { + // Second LLM call to generate story + const msg = await llm.invoke(`Write a story about ${topic}`); + return msg.content; + }); + + const callLlm3 = task("call_llm_3", async (topic: string) => { + // Third LLM call to generate poem + const msg = await llm.invoke(`Write a poem about ${topic}`); + return msg.content; + }); + + const aggregator = task("aggregator", (topic: string, joke: string, story: string, poem: string) => { + // Combine the joke and story into a single output + let combined = `Here's a story, joke, and poem about ${topic}!\n\n`; + combined += `STORY:\n${story}\n\n`; + combined += `JOKE:\n${joke}\n\n`; + combined += `POEM:\n${poem}`; + return combined; + }); + + // Build workflow + const parallelWorkflow = entrypoint("parallelWorkflow", async (topic: string) => { + const jokeFut = callLlm1(topic); + const storyFut = callLlm2(topic); + const poemFut = callLlm3(topic); + + return await aggregator( + topic, + await jokeFut, + await storyFut, + await poemFut + ); + }); + + // Invoke + const stream = await parallelWorkflow.stream("cats", { streamMode: "updates" }); + for await (const step of stream) { + console.log(step); + console.log("\n"); + } + ``` + ::: ## Routing -Routing classifies an input and directs it to a followup task. As noted in the Anthropic blog on `Building Effective Agents`: +Routing classifies an input and directs it to a followup task. As noted in the Anthropic blog on `Building Effective Agents`: > Routing classifies an input and directs it to a specialized followup task. This workflow allows for separation of concerns, and building more specialized prompts. Without this workflow, optimizing for one kind of input can hurt performance on other inputs. @@ -404,9 +736,9 @@ Routing classifies an input and directs it to a followup task. As noted in the A ![routing.png](./workflows/img/routing.png) - === "Graph API" + :::python ```python from typing_extensions import Literal from langchain_core.messages import HumanMessage, SystemMessage @@ -527,9 +859,99 @@ Routing classifies an input and directs it to a followup task. As noted in the A **Examples** [Here](https://langchain-ai.github.io/langgraph/tutorials/rag/langgraph_adaptive_rag_local/) is RAG workflow that routes questions. See our video [here](https://www.youtube.com/watch?v=bq1Plo2RhYI). + ::: + + :::js + ```typescript + import { SystemMessage, HumanMessage } from "@langchain/core/messages"; + + // Schema for structured output to use as routing logic + const Route = z.object({ + step: z.enum(["poem", "story", "joke"]).describe("The next step in the routing process"), + }); + + // Augment the LLM with schema for structured output + const router = llm.withStructuredOutput(Route); + + // State + const State = z.object({ + input: z.string(), + decision: z.string().optional(), + output: z.string().optional(), + }); + + // Nodes + const llmCall1 = async (state: z.infer<typeof State>) => { + // Write a story + const result = await llm.invoke(state.input); + return { output: result.content }; + }; + + const llmCall2 = async (state: z.infer<typeof State>) => { + // Write a joke + const result = await llm.invoke(state.input); + return { output: result.content }; + }; + + const llmCall3 = async (state: z.infer<typeof State>) => { + // Write a poem + const result = await llm.invoke(state.input); + return { output: result.content }; + }; + + const llmCallRouter = async (state: z.infer<typeof State>) => { + // Route the input to the appropriate node + const decision = await router.invoke([ + new SystemMessage("Route the input to story, joke, or poem based on the user's request."), + new HumanMessage(state.input), + ]); + + return { decision: decision.step }; + }; + + // Conditional edge function to route to the appropriate node + const routeDecision = (state: z.infer<typeof State>) => { + // Return the node name you want to visit next + if (state.decision === "story") { + return "llm_call_1"; + } else if (state.decision === "joke") { + return "llm_call_2"; + } else if (state.decision === "poem") { + return "llm_call_3"; + } + }; + + // Build workflow + const routerBuilder = new StateGraph(State) + .addNode("llm_call_1", llmCall1) + .addNode("llm_call_2", llmCall2) + .addNode("llm_call_3", llmCall3) + .addNode("llm_call_router", llmCallRouter) + .addEdge(START, "llm_call_router") + .addConditionalEdges( + "llm_call_router", + routeDecision, + { + "llm_call_1": "llm_call_1", + "llm_call_2": "llm_call_2", + "llm_call_3": "llm_call_3", + } + ) + .addEdge("llm_call_1", END) + .addEdge("llm_call_2", END) + .addEdge("llm_call_3", END); + + const routerWorkflow = routerBuilder.compile(); + + // Invoke + const state = await routerWorkflow.invoke({ input: "Write me a joke about cats" }); + console.log(state.output); + ``` + ::: === "Functional API" + :::python ```python from typing_extensions import Literal from pydantic import BaseModel @@ -604,20 +1026,87 @@ Routing classifies an input and directs it to a followup task. As noted in the A **LangSmith Trace** https://smith.langchain.com/public/5e2eb979-82dd-402c-b1a0-a8cceaf2a28a/r + ::: + + :::js + ```typescript + import { SystemMessage, HumanMessage } from "@langchain/core/messages"; + + // Schema for structured output to use as routing logic + const Route = z.object({ + step: z.enum(["poem", "story", "joke"]).describe( + "The next step in the routing process" + ), + }); + + // Augment the LLM with schema for structured output + const router = llm.withStructuredOutput(Route); + + const llmCall1 = task("llm_call_1", async (input: string) => { + // Write a story + const result = await llm.invoke(input); + return result.content; + }); + + const llmCall2 = task("llm_call_2", async (input: string) => { + // Write a joke + const result = await llm.invoke(input); + return result.content; + }); + + const llmCall3 = task("llm_call_3", async (input: string) => { + // Write a poem + const result = await llm.invoke(input); + return result.content; + }); + + const llmCallRouter = async (input: string) => { + // Route the input to the appropriate node + const decision = await router.invoke([ + new SystemMessage("Route the input to story, joke, or poem based on the user's request."), + new HumanMessage(input), + ]); + return decision.step; + }; + + // Create workflow + const routerWorkflow = entrypoint("routerWorkflow", async (input: string) => { + const nextStep = await llmCallRouter(input); + + let llmCall: typeof llmCall1; + if (nextStep === "story") { + llmCall = llmCall1; + } else if (nextStep === "joke") { + llmCall = llmCall2; + } else if (nextStep === "poem") { + llmCall = llmCall3; + } + + return await llmCall(input); + }); + + // Invoke + const stream = await routerWorkflow.stream("Write me a joke about cats", { streamMode: "updates" }); + for await (const step of stream) { + console.log(step); + console.log("\n"); + } + ``` + ::: ## Orchestrator-Worker -With orchestrator-worker, an orchestrator breaks down a task and delegates each sub-task to workers. As noted in the Anthropic blog on `Building Effective Agents`: +With orchestrator-worker, an orchestrator breaks down a task and delegates each sub-task to workers. As noted in the Anthropic blog on `Building Effective Agents`: > In the orchestrator-workers workflow, a central LLM dynamically breaks down tasks, delegates them to worker LLMs, and synthesizes their results. -> When to use this workflow: This workflow is well-suited for complex tasks where you can’t predict the subtasks needed (in coding, for example, the number of files that need to be changed and the nature of the change in each file likely depend on the task). Whereas it’s topographically similar, the key difference from parallelization is its flexibility—subtasks aren't pre-defined, but determined by the orchestrator based on the specific input. +> When to use this workflow: This workflow is well-suited for complex tasks where you can't predict the subtasks needed (in coding, for example, the number of files that need to be changed and the nature of the change in each file likely depend on the task). Whereas it's topographically similar, the key difference from parallelization is its flexibility—subtasks aren't pre-defined, but determined by the orchestrator based on the specific input. ![worker.png](./workflows/img/worker.png) - === "Graph API" + :::python ```python from typing import Annotated, List import operator @@ -763,10 +1252,120 @@ With orchestrator-worker, an orchestrator breaks down a task and delegates each **Examples** [Here](https://github.com/langchain-ai/report-mAIstro) is a project that uses orchestrator-worker for report planning and writing. See our video [here](https://www.youtube.com/watch?v=wSxZ7yFbbas). + ::: + :::js + ```typescript + import "@langchain/langgraph/zod"; + + // Schema for structured output to use in planning + const Section = z.object({ + name: z.string().describe("Name for this section of the report."), + description: z.string().describe("Brief overview of the main topics and concepts to be covered in this section."), + }); + + const Sections = z.object({ + sections: z.array(Section).describe("Sections of the report."), + }); + + // Augment the LLM with schema for structured output + const planner = llm.withStructuredOutput(Sections); + ``` + + **Creating Workers in LangGraph** + + Because orchestrator-worker workflows are common, LangGraph **has the `Send` API to support this**. It lets you dynamically create worker nodes and send each one a specific input. Each worker has its own state, and all worker outputs are written to a *shared state key* that is accessible to the orchestrator graph. This gives the orchestrator access to all worker output and allows it to synthesize them into a final output. As you can see below, we iterate over a list of sections and `Send` each to a worker node. See further documentation [here](../how-tos/map-reduce/) and [here](../concepts/low_level/#send). + + ```typescript + import { withLangGraph } from "@langchain/langgraph/zod"; + import { Send } from "@langchain/langgraph"; + + // Graph state + const State = z.object({ + topic: z.string(), // Report topic + sections: z.array(Section).optional(), // List of report sections + // All workers write to this key + completed_sections: withLangGraph(z.array(z.string()), { + reducer: { + fn: (x, y) => x.concat(y), + }, + default: () => [], + }), + final_report: z.string().optional(), // Final report + }); + + // Worker state + const WorkerState = z.object({ + section: Section, + completed_sections: withLangGraph(z.array(z.string()), { + reducer: { + fn: (x, y) => x.concat(y), + }, + default: () => [], + }), + }); + + // Nodes + const orchestrator = async (state: z.infer<typeof State>) => { + // Orchestrator that generates a plan for the report + const reportSections = await planner.invoke([ + new SystemMessage("Generate a plan for the report."), + new HumanMessage(`Here is the report topic: ${state.topic}`), + ]); + + return { sections: reportSections.sections }; + }; + + const llmCall = async (state: z.infer<typeof WorkerState>) => { + // Worker writes a section of the report + const section = await llm.invoke([ + new SystemMessage( + "Write a report section following the provided name and description. Include no preamble for each section. Use markdown formatting." + ), + new HumanMessage( + `Here is the section name: ${state.section.name} and description: ${state.section.description}` + ), + ]); + + // Write the updated section to completed sections + return { completed_sections: [section.content] }; + }; + + const synthesizer = (state: z.infer<typeof State>) => { + // Synthesize full report from sections + const completedSections = state.completed_sections; + const completedReportSections = completedSections.join("\n\n---\n\n"); + return { final_report: completedReportSections }; + }; + + // Conditional edge function to create llm_call workers + const assignWorkers = (state: z.infer<typeof State>) => { + // Assign a worker to each section in the plan + return state.sections!.map((s) => new Send("llm_call", { section: s })); + }; + + // Build workflow + const orchestratorWorkerBuilder = new StateGraph(State) + .addNode("orchestrator", orchestrator) + .addNode("llm_call", llmCall) + .addNode("synthesizer", synthesizer) + .addEdge(START, "orchestrator") + .addConditionalEdges("orchestrator", assignWorkers, ["llm_call"]) + .addEdge("llm_call", "synthesizer") + .addEdge("synthesizer", END); + + // Compile the workflow + const orchestratorWorker = orchestratorWorkerBuilder.compile(); + + // Invoke + const state = await orchestratorWorker.invoke({ topic: "Create a report on LLM scaling laws" }); + console.log(state.final_report); + ``` + ::: === "Functional API" + :::python ```python from typing import List @@ -848,19 +1447,75 @@ With orchestrator-worker, an orchestrator breaks down a task and delegates each **LangSmith Trace** https://smith.langchain.com/public/75a636d0-6179-4a12-9836-e0aa571e87c5/r + ::: + + :::js + ```typescript + // Schema for structured output to use in planning + const Section = z.object({ + name: z.string().describe("Name for this section of the report."), + description: z.string().describe("Brief overview of the main topics and concepts to be covered in this section."), + }); + + const Sections = z.object({ + sections: z.array(Section).describe("Sections of the report."), + }); + + // Augment the LLM with schema for structured output + const planner = llm.withStructuredOutput(Sections); + + const orchestrator = task("orchestrator", async (topic: string) => { + // Orchestrator that generates a plan for the report + const reportSections = await planner.invoke([ + new SystemMessage("Generate a plan for the report."), + new HumanMessage(`Here is the report topic: ${topic}`), + ]); + return reportSections.sections; + }); + + const llmCall = task("llm_call", async (section: z.infer<typeof Section>) => { + // Worker writes a section of the report + const result = await llm.invoke([ + new SystemMessage("Write a report section."), + new HumanMessage( + `Here is the section name: ${section.name} and description: ${section.description}` + ), + ]); + return result.content; + }); + + const synthesizer = task("synthesizer", (completedSections: string[]) => { + // Synthesize full report from sections + const finalReport = completedSections.join("\n\n---\n\n"); + return finalReport; + }); + + const orchestratorWorker = entrypoint("orchestratorWorker", async (topic: string) => { + const sections = await orchestrator(topic); + const sectionFutures = sections.map((section) => llmCall(section)); + const finalReport = await synthesizer( + await Promise.all(sectionFutures) + ); + return finalReport; + }); + + // Invoke + const report = await orchestratorWorker.invoke("Create a report on LLM scaling laws"); + console.log(report); + ``` + ::: ## Evaluator-optimizer In the evaluator-optimizer workflow, one LLM call generates a response while another provides evaluation and feedback in a loop: -> In the evaluator-optimizer workflow, one LLM call generates a response while another provides evaluation and feedback in a loop. - > When to use this workflow: This workflow is particularly effective when we have clear evaluation criteria, and when iterative refinement provides measurable value. The two signs of good fit are, first, that LLM responses can be demonstrably improved when a human articulates their feedback; and second, that the LLM can provide such feedback. This is analogous to the iterative writing process a human writer might go through when producing a polished document. ![evaluator_optimizer.png](./workflows/img/evaluator_optimizer.png) === "Graph API" + :::python ```python # Graph state class State(TypedDict): @@ -955,9 +1610,84 @@ In the evaluator-optimizer workflow, one LLM call generates a response while ano [Here](https://github.com/langchain-ai/local-deep-researcher) is an assistant that uses evaluator-optimizer to improve a report. See our video [here](https://www.youtube.com/watch?v=XGuTzHoqlj8). [Here](https://langchain-ai.github.io/langgraph/tutorials/rag/langgraph_adaptive_rag_local/) is a RAG workflow that grades answers for hallucinations or errors. See our video [here](https://www.youtube.com/watch?v=bq1Plo2RhYI). + ::: + + :::js + ```typescript + // Graph state + const State = z.object({ + joke: z.string().optional(), + topic: z.string(), + feedback: z.string().optional(), + funny_or_not: z.string().optional(), + }); + + // Schema for structured output to use in evaluation + const Feedback = z.object({ + grade: z.enum(["funny", "not funny"]).describe("Decide if the joke is funny or not."), + feedback: z.string().describe("If the joke is not funny, provide feedback on how to improve it."), + }); + + // Augment the LLM with schema for structured output + const evaluator = llm.withStructuredOutput(Feedback); + + // Nodes + const llmCallGenerator = async (state: z.infer<typeof State>) => { + // LLM generates a joke + let msg; + if (state.feedback) { + msg = await llm.invoke( + `Write a joke about ${state.topic} but take into account the feedback: ${state.feedback}` + ); + } else { + msg = await llm.invoke(`Write a joke about ${state.topic}`); + } + return { joke: msg.content }; + }; + + const llmCallEvaluator = async (state: z.infer<typeof State>) => { + // LLM evaluates the joke + const grade = await evaluator.invoke(`Grade the joke ${state.joke}`); + return { funny_or_not: grade.grade, feedback: grade.feedback }; + }; + + // Conditional edge function to route back to joke generator or end + const routeJoke = (state: z.infer<typeof State>) => { + // Route back to joke generator or end based upon feedback from the evaluator + if (state.funny_or_not === "funny") { + return "Accepted"; + } else if (state.funny_or_not === "not funny") { + return "Rejected + Feedback"; + } + }; + + // Build workflow + const optimizerBuilder = new StateGraph(State) + .addNode("llm_call_generator", llmCallGenerator) + .addNode("llm_call_evaluator", llmCallEvaluator) + .addEdge(START, "llm_call_generator") + .addEdge("llm_call_generator", "llm_call_evaluator") + .addConditionalEdges( + "llm_call_evaluator", + routeJoke, + { + "Accepted": END, + "Rejected + Feedback": "llm_call_generator", + } + ); + + // Compile the workflow + const optimizerWorkflow = optimizerBuilder.compile(); + + // Invoke + const state = await optimizerWorkflow.invoke({ topic: "Cats" }); + console.log(state.joke); + ``` + ::: === "Functional API" + :::python ```python # Schema for structured output to use in evaluation class Feedback(BaseModel): @@ -1013,6 +1743,58 @@ In the evaluator-optimizer workflow, one LLM call generates a response while ano **LangSmith Trace** https://smith.langchain.com/public/f66830be-4339-4a6b-8a93-389ce5ae27b4/r + ::: + + :::js + ```typescript + // Schema for structured output to use in evaluation + const Feedback = z.object({ + grade: z.enum(["funny", "not funny"]).describe("Decide if the joke is funny or not."), + feedback: z.string().describe("If the joke is not funny, provide feedback on how to improve it."), + }); + + // Augment the LLM with schema for structured output + const evaluator = llm.withStructuredOutput(Feedback); + + // Nodes + const llmCallGenerator = task("llm_call_generator", async (topic: string, feedback?: string) => { + // LLM generates a joke + if (feedback) { + const msg = await llm.invoke( + `Write a joke about ${topic} but take into account the feedback: ${feedback}` + ); + return msg.content; + } else { + const msg = await llm.invoke(`Write a joke about ${topic}`); + return msg.content; + } + }); + + const llmCallEvaluator = task("llm_call_evaluator", async (joke: string) => { + // LLM evaluates the joke + const feedback = await evaluator.invoke(`Grade the joke ${joke}`); + return feedback; + }); + + const optimizerWorkflow = entrypoint("optimizerWorkflow", async (topic: string) => { + let feedback; + while (true) { + const joke = await llmCallGenerator(topic, feedback?.feedback); + feedback = await llmCallEvaluator(joke); + if (feedback.grade === "funny") { + return joke; + } + } + }); + + // Invoke + const stream = await optimizerWorkflow.stream("Cats", { streamMode: "updates" }); + for await (const step of stream) { + console.log(step); + console.log("\n"); + } + ``` + ::: ## Agent @@ -1020,10 +1802,11 @@ Agents are typically implemented as an LLM performing actions (via tool-calling) > Agents can handle sophisticated tasks, but their implementation is often straightforward. They are typically just LLMs using tools based on environmental feedback in a loop. It is therefore crucial to design toolsets and their documentation clearly and thoughtfully. -> When to use agents: Agents can be used for open-ended problems where it’s difficult or impossible to predict the required number of steps, and where you can’t hardcode a fixed path. The LLM will potentially operate for many turns, and you must have some level of trust in its decision-making. Agents' autonomy makes them ideal for scaling tasks in trusted environments. +> When to use agents: Agents can be used for open-ended problems where it's difficult or impossible to predict the required number of steps, and where you can't hardcode a fixed path. The LLM will potentially operate for many turns, and you must have some level of trust in its decision-making. Agents' autonomy makes them ideal for scaling tasks in trusted environments. ![agent.png](./workflows/img/agent.png) +:::python ```python from langchain_core.tools import tool @@ -1069,8 +1852,67 @@ tools_by_name = {tool.name: tool for tool in tools} llm_with_tools = llm.bind_tools(tools) ``` +::: + +:::js + +```typescript +import { tool } from "@langchain/core/tools"; + +// Define tools +const multiply = tool( + async ({ a, b }: { a: number; b: number }) => { + return a * b; + }, + { + name: "multiply", + description: "Multiply a and b.", + schema: z.object({ + a: z.number().describe("first int"), + b: z.number().describe("second int"), + }), + } +); + +const add = tool( + async ({ a, b }: { a: number; b: number }) => { + return a + b; + }, + { + name: "add", + description: "Adds a and b.", + schema: z.object({ + a: z.number().describe("first int"), + b: z.number().describe("second int"), + }), + } +); + +const divide = tool( + async ({ a, b }: { a: number; b: number }) => { + return a / b; + }, + { + name: "divide", + description: "Divide a and b.", + schema: z.object({ + a: z.number().describe("first int"), + b: z.number().describe("second int"), + }), + } +); + +// Augment the LLM with tools +const tools = [add, multiply, divide]; +const toolsByName = Object.fromEntries(tools.map((tool) => [tool.name, tool])); +const llmWithTools = llm.bindTools(tools); +``` + +::: + === "Graph API" + :::python ```python from langgraph.graph import MessagesState from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage @@ -1164,9 +2006,70 @@ llm_with_tools = llm.bind_tools(tools) **Examples** [Here](https://github.com/langchain-ai/memory-agent) is a project that uses a tool calling agent to create / store long-term memories. + ::: + + :::js + ```typescript + import { MessagesZodState, ToolNode } from "@langchain/langgraph/prebuilt"; + import { SystemMessage, HumanMessage, ToolMessage, isAIMessage } from "@langchain/core/messages"; + + // Nodes + const llmCall = async (state: z.infer<typeof MessagesZodState>) => { + // LLM decides whether to call a tool or not + const response = await llmWithTools.invoke([ + new SystemMessage( + "You are a helpful assistant tasked with performing arithmetic on a set of inputs." + ), + ...state.messages, + ]); + return { messages: [response] }; + }; + + const toolNode = new ToolNode(tools); + + // Conditional edge function to route to the tool node or end + const shouldContinue = (state: z.infer<typeof MessagesZodState>) => { + // Decide if we should continue the loop or stop + const messages = state.messages; + const lastMessage = messages[messages.length - 1]; + // If the LLM makes a tool call, then perform an action + if (isAIMessage(lastMessage) && lastMessage.tool_calls?.length) { + return "Action"; + } + // Otherwise, we stop (reply to the user) + return END; + }; + + // Build workflow + const agentBuilder = new StateGraph(MessagesZodState) + .addNode("llm_call", llmCall) + .addNode("environment", toolNode) + .addEdge(START, "llm_call") + .addConditionalEdges( + "llm_call", + shouldContinue, + { + "Action": "environment", + [END]: END, + } + ) + .addEdge("environment", "llm_call"); + + // Compile the agent + const agent = agentBuilder.compile(); + + // Invoke + const messages = [new HumanMessage("Add 3 and 4.")]; + const result = await agent.invoke({ messages }); + for (const m of result.messages) { + console.log(`${m.getType()}: ${m.content}`); + } + ``` + ::: === "Functional API" + :::python ```python from langgraph.graph import add_messages from langchain_core.messages import ( @@ -1226,10 +2129,75 @@ llm_with_tools = llm.bind_tools(tools) **LangSmith Trace** https://smith.langchain.com/public/42ae8bf9-3935-4504-a081-8ddbcbfc8b2e/r + ::: + + :::js + ```typescript + import { addMessages } from "@langchain/langgraph"; + import { + SystemMessage, + HumanMessage, + BaseMessage, + ToolCall, + } from "@langchain/core/messages"; + + const callLlm = task("call_llm", async (messages: BaseMessage[]) => { + // LLM decides whether to call a tool or not + return await llmWithTools.invoke([ + new SystemMessage( + "You are a helpful assistant tasked with performing arithmetic on a set of inputs." + ), + ...messages, + ]); + }); + + const callTool = task("call_tool", async (toolCall: ToolCall) => { + // Performs the tool call + const tool = toolsByName[toolCall.name]; + return await tool.invoke(toolCall); + }); + + const agent = entrypoint("agent", async (messages: BaseMessage[]) => { + let currentMessages = messages; + let llmResponse = await callLlm(currentMessages); + + while (true) { + if (!llmResponse.tool_calls?.length) { + break; + } + + // Execute tools + const toolResults = await Promise.all( + llmResponse.tool_calls.map((toolCall) => callTool(toolCall)) + ); + + // Append to message list + currentMessages = addMessages(currentMessages, [ + llmResponse, + ...toolResults, + ]); + + // Call model again + llmResponse = await callLlm(currentMessages); + } + + return llmResponse; + }); + + // Invoke + const messages = [new HumanMessage("Add 3 and 4.")]; + const stream = await agent.stream(messages, { streamMode: "updates" }); + for await (const chunk of stream) { + console.log(chunk); + console.log("\n"); + } + ``` + ::: #### Pre-built -LangGraph also provides a **pre-built method** for creating an agent as defined above (using the [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] function): +:::python +LangGraph also provides a **pre-built method** for creating an agent as defined above (using the @[`create_react_agent`][create_react_agent] function): https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/ @@ -1254,6 +2222,28 @@ for m in messages["messages"]: **LangSmith Trace** https://smith.langchain.com/public/abab6a44-29f6-4b97-8164-af77413e494d/r +::: + +:::js +LangGraph also provides a **pre-built method** for creating an agent as defined above (using the @[`createReactAgent`][create_react_agent] function): + +```typescript +import { createReactAgent } from "@langchain/langgraph/prebuilt"; + +// Pass in: +// (1) the augmented LLM with tools +// (2) the tools list (which is used to create the tool node) +const preBuiltAgent = createReactAgent({ llm, tools }); + +// Invoke +const messages = [new HumanMessage("Add 3 and 4.")]; +const result = await preBuiltAgent.invoke({ messages }); +for (const m of result.messages) { + console.log(`${m.getType()}: ${m.content}`); +} +``` + +::: ## What LangGraph provides @@ -1271,7 +2261,6 @@ LangGraph persistence layer supports conversational (short-term) memory and long LangGraph provides several ways to stream workflow / agent outputs or intermediate state. See [Module 3 of LangChain Academy](https://github.com/langchain-ai/langchain-academy/blob/main/module-3/streaming-interruption.ipynb). - ### Deployment LangGraph provides an easy on-ramp for deployment, observability, and evaluation. See [module 6](https://github.com/langchain-ai/langchain-academy/tree/main/module-6) of LangChain Academy. diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 506572963..603c7383c 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -52,7 +52,6 @@ theme: plugins: - search: separator: '[\s\u200b\-,:!=\[\]()"`/]+|\.(?!\d)|&[lg]t;' - - autorefs - tags - include-markdown - mkdocstrings: diff --git a/docs/package.json b/docs/package.json index abcdef39c..70f7ea02e 100644 --- a/docs/package.json +++ b/docs/package.json @@ -8,9 +8,9 @@ "dependencies": { "@langchain/core": "^0.3.38", "@langchain/openai": "^0.4.2", + "he": "^1.2.0", "msgpack-lite": "^0.1.26", - "nock": "^14.0.1", - "he": "^1.2.0" + "nock": "^14.0.1" }, "devDependencies": { "@tsconfig/recommended": "^1.0.8", @@ -18,4 +18,4 @@ "@types/nock": "^11.1.0", "@types/node": "^22.13.1" } -} +} \ No newline at end of file diff --git a/docs/pyproject.toml b/docs/pyproject.toml index 0f1a931db..4b33ebf97 100644 --- a/docs/pyproject.toml +++ b/docs/pyproject.toml @@ -30,7 +30,6 @@ docs = [ "langchain-mcp-adapters", "langchain-ollama", "mkdocs", - "mkdocs-autorefs", "mkdocstrings", "mkdocstrings-python", "mkdocs-minify-plugin", diff --git a/docs/stats.yml b/docs/stats.yml index 1e8abb034..752684f19 100644 --- a/docs/stats.yml +++ b/docs/stats.yml @@ -1,58 +1,109 @@ # This file is auto-generated. Do not edit. - description: Tenacious tool calling built on LangGraph. + language: python + monorepo_path: null name: trustcall repo: hinthornw/trustcall weekly_downloads: -12345 - description: A streamlined research system built inspired on STORM and built on LangGraph. + language: python + monorepo_path: null name: breeze-agent repo: andrestorres123/breeze-agent weekly_downloads: -12345 - description: Build supervisor multi-agent systems with LangGraph. + language: python + monorepo_path: null name: langgraph-supervisor repo: langchain-ai/langgraph-supervisor-py weekly_downloads: -12345 - description: Build agents that learn and adapt from interactions over time. + language: python + monorepo_path: null name: langmem repo: langchain-ai/langmem weekly_downloads: -12345 - description: Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph agents. + language: python + monorepo_path: null name: langchain-mcp-adapters repo: langchain-ai/langchain-mcp-adapters weekly_downloads: -12345 - description: Open source assistant for iterative web research and report writing. + language: python + monorepo_path: null name: open-deep-research repo: langchain-ai/open_deep_research weekly_downloads: -12345 - description: Build swarm-style multi-agent systems using LangGraph. + language: python + monorepo_path: null name: langgraph-swarm repo: langchain-ai/langgraph-swarm-py weekly_downloads: -12345 - description: A taxonomy generator for unstructured data + language: python + monorepo_path: null name: delve-taxonomy-generator repo: andrestorres123/delve weekly_downloads: -12345 - description: Enable researcher to build scientific workflows easily with simplified interface. + language: python + monorepo_path: null name: nodeology repo: xyin-anl/Nodeology weekly_downloads: -12345 - description: Build LangGraph agents with large numbers of tools. + language: python + monorepo_path: null name: langgraph-bigtool repo: langchain-ai/langgraph-bigtool weekly_downloads: -12345 - description: An AI-powered data science team of agents to help you perform common data science tasks 10X faster. + language: python + monorepo_path: null name: ai-data-science-team repo: business-science/ai-data-science-team weekly_downloads: -12345 - description: LangGraph agent that runs a reflection step. + language: python + monorepo_path: null name: langgraph-reflection repo: langchain-ai/langgraph-reflection weekly_downloads: -12345 - description: LangGraph implementation of CodeAct agent that generates and executes code instead of tool calling. + language: python + monorepo_path: null name: langgraph-codeact repo: langchain-ai/langgraph-codeact weekly_downloads: -12345 +- description: Make Anthropic Model Context Protocol (MCP) tools compatible with LangGraph + agents. + language: js + monorepo_path: null + name: '@langchain/mcp-adapters' + repo: langchain-ai/langchainjs + weekly_downloads: -12345 +- description: Build supervisor multi-agent systems with LangGraph + language: js + monorepo_path: libs/langgraph-supervisor + name: '@langchain/langgraph-supervisor' + repo: langchain-ai/langgraphjs + weekly_downloads: -12345 +- description: Build multi-agent swarms with LangGraph + language: js + monorepo_path: libs/langgraph-swarm + name: '@langchain/langgraph-swarm' + repo: langchain-ai/langgraphjs + weekly_downloads: -12345 +- description: Build computer use agents with LangGraph + language: js + monorepo_path: libs/langgraph-cua + name: '@langchain/langgraph-cua' + repo: langchain-ai/langgraphjs + weekly_downloads: -12345 diff --git a/docs/uv.lock b/docs/uv.lock index 11eb9f695..7eefd925a 100644 --- a/docs/uv.lock +++ b/docs/uv.lock @@ -2337,7 +2337,7 @@ wheels = [ [[package]] name = "langgraph" -version = "0.6.0a1" +version = "0.6.0" source = { editable = "../libs/langgraph" } dependencies = [ { name = "langchain-core" }, @@ -2466,7 +2466,7 @@ dev = [ [[package]] name = "langgraph-checkpoint-sqlite" -version = "2.0.10" +version = "2.0.11" source = { editable = "../libs/checkpoint-sqlite" } dependencies = [ { name = "aiosqlite" }, @@ -2523,7 +2523,6 @@ docs = [ { name = "markdown-callouts" }, { name = "markdown-include" }, { name = "mkdocs" }, - { name = "mkdocs-autorefs" }, { name = "mkdocs-exclude" }, { name = "mkdocs-git-committers-plugin-2" }, { name = "mkdocs-include-markdown-plugin" }, @@ -2595,7 +2594,6 @@ docs = [ { name = "markdown-callouts" }, { name = "markdown-include" }, { name = "mkdocs" }, - { name = "mkdocs-autorefs" }, { name = "mkdocs-exclude" }, { name = "mkdocs-git-committers-plugin-2" }, { name = "mkdocs-include-markdown-plugin", specifier = ">=7.1.6" }, @@ -2643,7 +2641,7 @@ test = [ [[package]] name = "langgraph-prebuilt" -version = "0.5.2" +version = "0.6.0" source = { editable = "../libs/prebuilt" } dependencies = [ { name = "langchain-core" }, @@ -2674,7 +2672,7 @@ dev = [ [[package]] name = "langgraph-sdk" -version = "0.2.0a1" +version = "0.2.0" source = { editable = "../libs/sdk-py" } dependencies = [ { name = "httpx" }, diff --git a/docs/yarn.lock b/docs/yarn.lock index 92ae0cca9..8c5607112 100644 --- a/docs/yarn.lock +++ b/docs/yarn.lock @@ -1,584 +1,813 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 +# This file is generated by running "yarn install" inside your project. +# Manual changes might be lost - proceed with caution! +__metadata: + version: 8 + cacheKey: 10c0 -"@cfworker/json-schema@^4.0.2": - version "4.1.1" - resolved "https://registry.yarnpkg.com/@cfworker/json-schema/-/json-schema-4.1.1.tgz#4a2a3947ee9fa7b7c24be981422831b8674c3be6" - integrity sha512-gAmrUZSGtKc3AiBL71iNWxDsyUC5uMaKKGdvzYsBoTW/xi42JQHl7eKV2OYzCUqvc+D2RCcf7EXY2iCyFIk6og== +"@cfworker/json-schema@npm:^4.0.2": + version: 4.1.1 + resolution: "@cfworker/json-schema@npm:4.1.1" + checksum: 10c0/b5253486d346b7de6feec9c73954f612b11019dacb9023d710a5666df2f5fc145dd88b6b913c88726c6d97e2e258a515fa2cab177f58b18da6bac3738cbc4739 + languageName: node + linkType: hard -"@langchain/core@^0.3.38": - version "0.3.38" - resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.3.38.tgz#e0675d978d5141c720d9a2e143550d4411afa3be" - integrity sha512-o7mowk/0oIsYsPxRAJ3TKX6OG674HqcaNRged0sxaTegLAMyZDBDRXEAt3qoe5UfkHnqXAggDLjNVDhpMwECmg== +"@langchain/core@npm:^0.3.38": + version: 0.3.38 + resolution: "@langchain/core@npm:0.3.38" dependencies: - "@cfworker/json-schema" "^4.0.2" - ansi-styles "^5.0.0" - camelcase "6" - decamelize "1.2.0" - js-tiktoken "^1.0.12" - langsmith ">=0.2.8 <0.4.0" - mustache "^4.2.0" - p-queue "^6.6.2" - p-retry "4" - uuid "^10.0.0" - zod "^3.22.4" - zod-to-json-schema "^3.22.3" + "@cfworker/json-schema": "npm:^4.0.2" + ansi-styles: "npm:^5.0.0" + camelcase: "npm:6" + decamelize: "npm:1.2.0" + js-tiktoken: "npm:^1.0.12" + langsmith: "npm:>=0.2.8 <0.4.0" + mustache: "npm:^4.2.0" + p-queue: "npm:^6.6.2" + p-retry: "npm:4" + uuid: "npm:^10.0.0" + zod: "npm:^3.22.4" + zod-to-json-schema: "npm:^3.22.3" + checksum: 10c0/3b2f042f6550cb818a33b0649110c9ef7f645b0bc23507d2d5a63b98dd4dcd28692d4a6ead70a936e4d46dd39224cc74e4bfb0389b124a183f29bbdf0b069ab0 + languageName: node + linkType: hard -"@langchain/openai@^0.4.2": - version "0.4.2" - resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.4.2.tgz#1259bf56c4948ed2301d366e2fe945c29dfb53bc" - integrity sha512-Cuj7qbVcycALTP0aqZuPpEc7As8cwiGaU21MhXRyZFs+dnWxKYxZ1Q1z4kcx6cYkq/I+CNwwmk+sP+YruU73Aw== +"@langchain/openai@npm:^0.4.2": + version: 0.4.2 + resolution: "@langchain/openai@npm:0.4.2" dependencies: - js-tiktoken "^1.0.12" - openai "^4.77.0" - zod "^3.22.4" - zod-to-json-schema "^3.22.3" + js-tiktoken: "npm:^1.0.12" + openai: "npm:^4.77.0" + zod: "npm:^3.22.4" + zod-to-json-schema: "npm:^3.22.3" + peerDependencies: + "@langchain/core": ">=0.3.29 <0.4.0" + checksum: 10c0/0a17803c9a74e3b95f77a86e45705e32f0a76fdc9bbdb6d17f64ff6fa052356bc7067f0dccb57479087f236434f4aebf6731618c616331cab3bf48bf55a376c3 + languageName: node + linkType: hard -"@mswjs/interceptors@^0.37.3": - version "0.37.6" - resolved "https://registry.yarnpkg.com/@mswjs/interceptors/-/interceptors-0.37.6.tgz#2635319b7a81934e1ef1b5593ef7910347e2b761" - integrity sha512-wK+5pLK5XFmgtH3aQ2YVvA3HohS3xqV/OxuVOdNx9Wpnz7VE/fnC+e1A7ln6LFYeck7gOJ/dsZV6OLplOtAJ2w== +"@mswjs/interceptors@npm:^0.37.3": + version: 0.37.6 + resolution: "@mswjs/interceptors@npm:0.37.6" dependencies: - "@open-draft/deferred-promise" "^2.2.0" - "@open-draft/logger" "^0.3.0" - "@open-draft/until" "^2.0.0" - is-node-process "^1.2.0" - outvariant "^1.4.3" - strict-event-emitter "^0.5.1" + "@open-draft/deferred-promise": "npm:^2.2.0" + "@open-draft/logger": "npm:^0.3.0" + "@open-draft/until": "npm:^2.0.0" + is-node-process: "npm:^1.2.0" + outvariant: "npm:^1.4.3" + strict-event-emitter: "npm:^0.5.1" + checksum: 10c0/74f52c09c84fcbba9f1a06e462aa25b1567cf078ed27d396c76a8059c002fa9c361e711dcada0ac2aad4298f247d8e236a4fcc861c08ddf6e2ce0889368596fd + languageName: node + linkType: hard -"@open-draft/deferred-promise@^2.2.0": - version "2.2.0" - resolved "https://registry.yarnpkg.com/@open-draft/deferred-promise/-/deferred-promise-2.2.0.tgz#4a822d10f6f0e316be4d67b4d4f8c9a124b073bd" - integrity sha512-CecwLWx3rhxVQF6V4bAgPS5t+So2sTbPgAzafKkVizyi7tlwpcFpdFqq+wqF2OwNBmqFuu6tOyouTuxgpMfzmA== +"@open-draft/deferred-promise@npm:^2.2.0": + version: 2.2.0 + resolution: "@open-draft/deferred-promise@npm:2.2.0" + checksum: 10c0/eafc1b1d0fc8edb5e1c753c5e0f3293410b40dde2f92688211a54806d4136887051f39b98c1950370be258483deac9dfd17cf8b96557553765198ef2547e4549 + languageName: node + linkType: hard -"@open-draft/logger@^0.3.0": - version "0.3.0" - resolved "https://registry.yarnpkg.com/@open-draft/logger/-/logger-0.3.0.tgz#2b3ab1242b360aa0adb28b85f5d7da1c133a0954" - integrity sha512-X2g45fzhxH238HKO4xbSr7+wBS8Fvw6ixhTDuvLd5mqh6bJJCFAPwU9mPDxbcrRtfxv4u5IHCEH77BmxvXmmxQ== +"@open-draft/logger@npm:^0.3.0": + version: 0.3.0 + resolution: "@open-draft/logger@npm:0.3.0" dependencies: - is-node-process "^1.2.0" - outvariant "^1.4.0" + is-node-process: "npm:^1.2.0" + outvariant: "npm:^1.4.0" + checksum: 10c0/90010647b22e9693c16258f4f9adb034824d1771d3baa313057b9a37797f571181005bc50415a934eaf7c891d90ff71dcd7a9d5048b0b6bb438f31bef2c7c5c1 + languageName: node + linkType: hard -"@open-draft/until@^2.0.0": - version "2.1.0" - resolved "https://registry.yarnpkg.com/@open-draft/until/-/until-2.1.0.tgz#0acf32f470af2ceaf47f095cdecd40d68666efda" - integrity sha512-U69T3ItWHvLwGg5eJ0n3I62nWuE6ilHlmz7zM0npLBRvPRd7e6NYmg54vvRtP5mZG7kZqZCFVdsTWo7BPtBujg== +"@open-draft/until@npm:^2.0.0": + version: 2.1.0 + resolution: "@open-draft/until@npm:2.1.0" + checksum: 10c0/61d3f99718dd86bb393fee2d7a785f961dcaf12f2055f0c693b27f4d0cd5f7a03d498a6d9289773b117590d794a43cd129366fd8e99222e4832f67b1653d54cf + languageName: node + linkType: hard -"@tsconfig/recommended@^1.0.8": - version "1.0.8" - resolved "https://registry.yarnpkg.com/@tsconfig/recommended/-/recommended-1.0.8.tgz#16483d57b56bbbd32b8c3af0eff1a40c32d006fa" - integrity sha512-TotjFaaXveVUdsrXCdalyF6E5RyG6+7hHHQVZonQtdlk1rJZ1myDIvPUUKPhoYv+JAzThb2lQJh9+9ZfF46hsA== +"@tsconfig/recommended@npm:^1.0.8": + version: 1.0.8 + resolution: "@tsconfig/recommended@npm:1.0.8" + checksum: 10c0/bd6517e3f69cf96108ab8b7d2ee70a7e64ee457bb72326524acdef6e2219813b298654e9aa57ce2f8899901c9b8fd66388b036b9ca0aa062952a83adb59bec17 + languageName: node + linkType: hard -"@types/msgpack-lite@^0.1.11": - version "0.1.11" - resolved "https://registry.yarnpkg.com/@types/msgpack-lite/-/msgpack-lite-0.1.11.tgz#f618e1fc469577f65f36c474ff3309407afef174" - integrity sha512-cdCZS/gw+jIN22I4SUZUFf1ZZfVv5JM1//Br/MuZcI373sxiy3eSSoiyLu0oz+BPatTbGGGBO5jrcvd0siCdTQ== +"@types/msgpack-lite@npm:^0.1.11": + version: 0.1.11 + resolution: "@types/msgpack-lite@npm:0.1.11" dependencies: - "@types/node" "*" + "@types/node": "npm:*" + checksum: 10c0/d51a47a20ef5ff9b8b61d33ca3d10c992bbf10c4d4dbcbb7d1f1f9cdb2c8c1a302de36b00e1f11ef954b1bc4730add11b610996cd6ee767624b2bdd572e7b647 + languageName: node + linkType: hard -"@types/nock@^11.1.0": - version "11.1.0" - resolved "https://registry.yarnpkg.com/@types/nock/-/nock-11.1.0.tgz#0a8c1056a31ba32a959843abccf99626dd90a538" - integrity sha512-jI/ewavBQ7X5178262JQR0ewicPAcJhXS/iFaNJl0VHLfyosZ/kwSrsa6VNQNSO8i9d8SqdRgOtZSOKJ/+iNMw== +"@types/nock@npm:^11.1.0": + version: 11.1.0 + resolution: "@types/nock@npm:11.1.0" dependencies: - nock "*" + nock: "npm:*" + checksum: 10c0/d13596983b909b86c03d031220a478a4a4759a006586c02d2b6bbb7751386df04026223ccbe66289d0d4edaf5b66c7a401c62999a5e4ec3c2a242f5ec1a0433b + languageName: node + linkType: hard -"@types/node-fetch@^2.6.4": - version "2.6.12" - resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.12.tgz#8ab5c3ef8330f13100a7479e2cd56d3386830a03" - integrity sha512-8nneRWKCg3rMtF69nLQJnOYUcbafYeFSjqkw3jCRLsqkWFlHaoQrr5mXmofFGOx3DKn7UfmBMyov8ySvLRVldA== +"@types/node-fetch@npm:^2.6.4": + version: 2.6.12 + resolution: "@types/node-fetch@npm:2.6.12" dependencies: - "@types/node" "*" - form-data "^4.0.0" + "@types/node": "npm:*" + form-data: "npm:^4.0.0" + checksum: 10c0/7693acad5499b7df2d1727d46cff092a63896dc04645f36b973dd6dd754a59a7faba76fcb777bdaa35d80625c6a9dd7257cca9c401a4bab03b04480cda7fd1af + languageName: node + linkType: hard -"@types/node@*", "@types/node@^22.13.1": - version "22.13.1" - resolved "https://registry.yarnpkg.com/@types/node/-/node-22.13.1.tgz#a2a3fefbdeb7ba6b89f40371842162fac0934f33" - integrity sha512-jK8uzQlrvXqEU91UxiK5J7pKHyzgnI1Qnl0QDHIgVGuolJhRb9EEl28Cj9b3rGR8B2lhFCtvIm5os8lFnO/1Ew== +"@types/node@npm:*, @types/node@npm:^22.13.1": + version: 22.13.1 + resolution: "@types/node@npm:22.13.1" dependencies: - undici-types "~6.20.0" + undici-types: "npm:~6.20.0" + checksum: 10c0/d4e56d41d8bd53de93da2651c0a0234e330bd7b1b6d071b1a94bd3b5ee2d9f387519e739c52a15c1faa4fb9d97e825b848421af4b2e50e6518011e7adb4a34b7 + languageName: node + linkType: hard -"@types/node@^18.11.18": - version "18.19.75" - resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.75.tgz#be932799d1ab40779ffd16392a2b2300f81b565d" - integrity sha512-UIksWtThob6ZVSyxcOqCLOUNg/dyO1Qvx4McgeuhrEtHTLFTf7BBhEazaE4K806FGTPtzd/2sE90qn4fVr7cyw== +"@types/node@npm:^18.11.18": + version: 18.19.75 + resolution: "@types/node@npm:18.19.75" dependencies: - undici-types "~5.26.4" + undici-types: "npm:~5.26.4" + checksum: 10c0/6a78833071d23dcd4010507d0a232da1cb6e939eb5b62023a01ab5f91eecb90223bda3e34aa536f02cd5c3bdf7962c754b7e2a051a8224aed5886788fce88fbf + languageName: node + linkType: hard -"@types/retry@0.12.0": - version "0.12.0" - resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d" - integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA== +"@types/retry@npm:0.12.0": + version: 0.12.0 + resolution: "@types/retry@npm:0.12.0" + checksum: 10c0/7c5c9086369826f569b83a4683661557cab1361bac0897a1cefa1a915ff739acd10ca0d62b01071046fe3f5a3f7f2aec80785fe283b75602dc6726781ea3e328 + languageName: node + linkType: hard -"@types/uuid@^10.0.0": - version "10.0.0" - resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-10.0.0.tgz#e9c07fe50da0f53dc24970cca94d619ff03f6f6d" - integrity sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ== +"@types/uuid@npm:^10.0.0": + version: 10.0.0 + resolution: "@types/uuid@npm:10.0.0" + checksum: 10c0/9a1404bf287164481cb9b97f6bb638f78f955be57c40c6513b7655160beb29df6f84c915aaf4089a1559c216557dc4d2f79b48d978742d3ae10b937420ddac60 + languageName: node + linkType: hard -abort-controller@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" - integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg== +"abort-controller@npm:^3.0.0": + version: 3.0.0 + resolution: "abort-controller@npm:3.0.0" dependencies: - event-target-shim "^5.0.0" + event-target-shim: "npm:^5.0.0" + checksum: 10c0/90ccc50f010250152509a344eb2e71977fbf8db0ab8f1061197e3275ddf6c61a41a6edfd7b9409c664513131dd96e962065415325ef23efa5db931b382d24ca5 + languageName: node + linkType: hard -agentkeepalive@^4.2.1: - version "4.6.0" - resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.6.0.tgz#35f73e94b3f40bf65f105219c623ad19c136ea6a" - integrity sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ== +"agentkeepalive@npm:^4.2.1": + version: 4.6.0 + resolution: "agentkeepalive@npm:4.6.0" dependencies: - humanize-ms "^1.2.1" + humanize-ms: "npm:^1.2.1" + checksum: 10c0/235c182432f75046835b05f239708107138a40103deee23b6a08caee5136873709155753b394ec212e49e60e94a378189562cb01347765515cff61b692c69187 + languageName: node + linkType: hard -ansi-styles@^4.1.0: - version "4.3.0" - resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.3.0.tgz#edd803628ae71c04c85ae7a0906edad34b648937" - integrity sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg== +"ansi-styles@npm:^4.1.0": + version: 4.3.0 + resolution: "ansi-styles@npm:4.3.0" dependencies: - color-convert "^2.0.1" + color-convert: "npm:^2.0.1" + checksum: 10c0/895a23929da416f2bd3de7e9cb4eabd340949328ab85ddd6e484a637d8f6820d485f53933446f5291c3b760cbc488beb8e88573dd0f9c7daf83dccc8fe81b041 + languageName: node + linkType: hard -ansi-styles@^5.0.0: - version "5.2.0" - resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-5.2.0.tgz#07449690ad45777d1924ac2abb2fc8895dba836b" - integrity sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA== +"ansi-styles@npm:^5.0.0": + version: 5.2.0 + resolution: "ansi-styles@npm:5.2.0" + checksum: 10c0/9c4ca80eb3c2fb7b33841c210d2f20807f40865d27008d7c3f707b7f95cab7d67462a565e2388ac3285b71cb3d9bb2173de8da37c57692a362885ec34d6e27df + languageName: node + linkType: hard -asynckit@^0.4.0: - version "0.4.0" - resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" - integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== +"asynckit@npm:^0.4.0": + version: 0.4.0 + resolution: "asynckit@npm:0.4.0" + checksum: 10c0/d73e2ddf20c4eb9337e1b3df1a0f6159481050a5de457c55b14ea2e5cb6d90bb69e004c9af54737a5ee0917fcf2c9e25de67777bbe58261847846066ba75bc9d + languageName: node + linkType: hard -base64-js@^1.5.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a" - integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA== +"base64-js@npm:^1.5.1": + version: 1.5.1 + resolution: "base64-js@npm:1.5.1" + checksum: 10c0/f23823513b63173a001030fae4f2dabe283b99a9d324ade3ad3d148e218134676f1ee8568c877cd79ec1c53158dcf2d2ba527a97c606618928ba99dd930102bf + languageName: node + linkType: hard -call-bind-apply-helpers@^1.0.1, call-bind-apply-helpers@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz#4b5428c222be985d79c3d82657479dbe0b59b2d6" - integrity sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ== +"call-bind-apply-helpers@npm:^1.0.1, call-bind-apply-helpers@npm:^1.0.2": + version: 1.0.2 + resolution: "call-bind-apply-helpers@npm:1.0.2" dependencies: - es-errors "^1.3.0" - function-bind "^1.1.2" + es-errors: "npm:^1.3.0" + function-bind: "npm:^1.1.2" + checksum: 10c0/47bd9901d57b857590431243fea704ff18078b16890a6b3e021e12d279bbf211d039155e27d7566b374d49ee1f8189344bac9833dec7a20cdec370506361c938 + languageName: node + linkType: hard -camelcase@6: - version "6.3.0" - resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a" - integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== +"camelcase@npm:6": + version: 6.3.0 + resolution: "camelcase@npm:6.3.0" + checksum: 10c0/0d701658219bd3116d12da3eab31acddb3f9440790c0792e0d398f0a520a6a4058018e546862b6fba89d7ae990efaeb97da71e1913e9ebf5a8b5621a3d55c710 + languageName: node + linkType: hard -chalk@^4.1.2: - version "4.1.2" - resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" - integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA== +"chalk@npm:^4.1.2": + version: 4.1.2 + resolution: "chalk@npm:4.1.2" dependencies: - ansi-styles "^4.1.0" - supports-color "^7.1.0" + ansi-styles: "npm:^4.1.0" + supports-color: "npm:^7.1.0" + checksum: 10c0/4a3fef5cc34975c898ffe77141450f679721df9dde00f6c304353fa9c8b571929123b26a0e4617bde5018977eb655b31970c297b91b63ee83bb82aeb04666880 + languageName: node + linkType: hard -color-convert@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3" - integrity sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ== +"color-convert@npm:^2.0.1": + version: 2.0.1 + resolution: "color-convert@npm:2.0.1" dependencies: - color-name "~1.1.4" + color-name: "npm:~1.1.4" + checksum: 10c0/37e1150172f2e311fe1b2df62c6293a342ee7380da7b9cfdba67ea539909afbd74da27033208d01d6d5cfc65ee7868a22e18d7e7648e004425441c0f8a15a7d7 + languageName: node + linkType: hard -color-name@~1.1.4: - version "1.1.4" - resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" - integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== +"color-name@npm:~1.1.4": + version: 1.1.4 + resolution: "color-name@npm:1.1.4" + checksum: 10c0/a1a3f914156960902f46f7f56bc62effc6c94e84b2cae157a526b1c1f74b677a47ec602bf68a61abfa2b42d15b7c5651c6dbe72a43af720bc588dff885b10f95 + languageName: node + linkType: hard -combined-stream@^1.0.8: - version "1.0.8" - resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" - integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== +"combined-stream@npm:^1.0.8": + version: 1.0.8 + resolution: "combined-stream@npm:1.0.8" dependencies: - delayed-stream "~1.0.0" + delayed-stream: "npm:~1.0.0" + checksum: 10c0/0dbb829577e1b1e839fa82b40c07ffaf7de8a09b935cadd355a73652ae70a88b4320db322f6634a4ad93424292fa80973ac6480986247f1734a1137debf271d5 + languageName: node + linkType: hard -console-table-printer@^2.12.1: - version "2.12.1" - resolved "https://registry.yarnpkg.com/console-table-printer/-/console-table-printer-2.12.1.tgz#4a9646537a246a6d8de57075d4fae1e08abae267" - integrity sha512-wKGOQRRvdnd89pCeH96e2Fn4wkbenSP6LMHfjfyNLMbGuHEFbMqQNuxXqd0oXG9caIOQ1FTvc5Uijp9/4jujnQ== +"console-table-printer@npm:^2.12.1": + version: 2.12.1 + resolution: "console-table-printer@npm:2.12.1" dependencies: - simple-wcswidth "^1.0.1" + simple-wcswidth: "npm:^1.0.1" + checksum: 10c0/8f28e9c0ae5df77f5d60da3da002ecd95ebe1812b0b9e0a6d2795c81b5121b39774f32506bccf68830a838ca4d8fbb2ab8824e729dba2c5e30cdeb9df4dd5f2b + languageName: node + linkType: hard -decamelize@1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" - integrity sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA== +"decamelize@npm:1.2.0": + version: 1.2.0 + resolution: "decamelize@npm:1.2.0" + checksum: 10c0/85c39fe8fbf0482d4a1e224ef0119db5c1897f8503bcef8b826adff7a1b11414972f6fef2d7dec2ee0b4be3863cf64ac1439137ae9e6af23a3d8dcbe26a5b4b2 + languageName: node + linkType: hard -delayed-stream@~1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" - integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== +"delayed-stream@npm:~1.0.0": + version: 1.0.0 + resolution: "delayed-stream@npm:1.0.0" + checksum: 10c0/d758899da03392e6712f042bec80aa293bbe9e9ff1b2634baae6a360113e708b91326594c8a486d475c69d6259afb7efacdc3537bfcda1c6c648e390ce601b19 + languageName: node + linkType: hard -dunder-proto@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/dunder-proto/-/dunder-proto-1.0.1.tgz#d7ae667e1dc83482f8b70fd0f6eefc50da30f58a" - integrity sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A== +"docs@workspace:.": + version: 0.0.0-use.local + resolution: "docs@workspace:." dependencies: - call-bind-apply-helpers "^1.0.1" - es-errors "^1.3.0" - gopd "^1.2.0" + "@langchain/core": "npm:^0.3.38" + "@langchain/openai": "npm:^0.4.2" + "@tsconfig/recommended": "npm:^1.0.8" + "@types/msgpack-lite": "npm:^0.1.11" + "@types/nock": "npm:^11.1.0" + "@types/node": "npm:^22.13.1" + he: "npm:^1.2.0" + msgpack-lite: "npm:^0.1.26" + nock: "npm:^14.0.1" + languageName: unknown + linkType: soft -es-define-property@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/es-define-property/-/es-define-property-1.0.1.tgz#983eb2f9a6724e9303f61addf011c72e09e0b0fa" - integrity sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g== - -es-errors@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/es-errors/-/es-errors-1.3.0.tgz#05f75a25dab98e4fb1dcd5e1472c0546d5057c8f" - integrity sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw== - -es-object-atoms@^1.0.0, es-object-atoms@^1.1.1: - version "1.1.1" - resolved "https://registry.yarnpkg.com/es-object-atoms/-/es-object-atoms-1.1.1.tgz#1c4f2c4837327597ce69d2ca190a7fdd172338c1" - integrity sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA== +"dunder-proto@npm:^1.0.1": + version: 1.0.1 + resolution: "dunder-proto@npm:1.0.1" dependencies: - es-errors "^1.3.0" + call-bind-apply-helpers: "npm:^1.0.1" + es-errors: "npm:^1.3.0" + gopd: "npm:^1.2.0" + checksum: 10c0/199f2a0c1c16593ca0a145dbf76a962f8033ce3129f01284d48c45ed4e14fea9bbacd7b3610b6cdc33486cef20385ac054948fefc6272fcce645c09468f93031 + languageName: node + linkType: hard -es-set-tostringtag@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz#f31dbbe0c183b00a6d26eb6325c810c0fd18bd4d" - integrity sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA== +"es-define-property@npm:^1.0.1": + version: 1.0.1 + resolution: "es-define-property@npm:1.0.1" + checksum: 10c0/3f54eb49c16c18707949ff25a1456728c883e81259f045003499efba399c08bad00deebf65cccde8c0e07908c1a225c9d472b7107e558f2a48e28d530e34527c + languageName: node + linkType: hard + +"es-errors@npm:^1.3.0": + version: 1.3.0 + resolution: "es-errors@npm:1.3.0" + checksum: 10c0/0a61325670072f98d8ae3b914edab3559b6caa980f08054a3b872052640d91da01d38df55df797fcc916389d77fc92b8d5906cf028f4db46d7e3003abecbca85 + languageName: node + linkType: hard + +"es-object-atoms@npm:^1.0.0, es-object-atoms@npm:^1.1.1": + version: 1.1.1 + resolution: "es-object-atoms@npm:1.1.1" dependencies: - es-errors "^1.3.0" - get-intrinsic "^1.2.6" - has-tostringtag "^1.0.2" - hasown "^2.0.2" + es-errors: "npm:^1.3.0" + checksum: 10c0/65364812ca4daf48eb76e2a3b7a89b3f6a2e62a1c420766ce9f692665a29d94fe41fe88b65f24106f449859549711e4b40d9fb8002d862dfd7eb1c512d10be0c + languageName: node + linkType: hard -event-lite@^0.1.1: - version "0.1.3" - resolved "https://registry.yarnpkg.com/event-lite/-/event-lite-0.1.3.tgz#3dfe01144e808ac46448f0c19b4ab68e403a901d" - integrity sha512-8qz9nOz5VeD2z96elrEKD2U433+L3DWdUdDkOINLGOJvx1GsMBbMn0aCeu28y8/e85A6mCigBiFlYMnTBEGlSw== - -event-target-shim@^5.0.0: - version "5.0.1" - resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" - integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ== - -eventemitter3@^4.0.4: - version "4.0.7" - resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f" - integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== - -form-data-encoder@1.7.2: - version "1.7.2" - resolved "https://registry.yarnpkg.com/form-data-encoder/-/form-data-encoder-1.7.2.tgz#1f1ae3dccf58ed4690b86d87e4f57c654fbab040" - integrity sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A== - -form-data@^4.0.0: - version "4.0.4" - resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.4.tgz#784cdcce0669a9d68e94d11ac4eea98088edd2c4" - integrity sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow== +"es-set-tostringtag@npm:^2.1.0": + version: 2.1.0 + resolution: "es-set-tostringtag@npm:2.1.0" dependencies: - asynckit "^0.4.0" - combined-stream "^1.0.8" - es-set-tostringtag "^2.1.0" - hasown "^2.0.2" - mime-types "^2.1.12" + es-errors: "npm:^1.3.0" + get-intrinsic: "npm:^1.2.6" + has-tostringtag: "npm:^1.0.2" + hasown: "npm:^2.0.2" + checksum: 10c0/ef2ca9ce49afe3931cb32e35da4dcb6d86ab02592cfc2ce3e49ced199d9d0bb5085fc7e73e06312213765f5efa47cc1df553a6a5154584b21448e9fb8355b1af + languageName: node + linkType: hard -formdata-node@^4.3.2: - version "4.4.1" - resolved "https://registry.yarnpkg.com/formdata-node/-/formdata-node-4.4.1.tgz#23f6a5cb9cb55315912cbec4ff7b0f59bbd191e2" - integrity sha512-0iirZp3uVDjVGt9p49aTaqjk84TrglENEDuqfdlZQ1roC9CWlPk6Avf8EEnZNcAqPonwkG35x4n3ww/1THYAeQ== +"event-lite@npm:^0.1.1": + version: 0.1.3 + resolution: "event-lite@npm:0.1.3" + checksum: 10c0/68d11a1e9001d713d673866fe07f6c310fa9054fc0a936dd5eacc37a793aa6b3331ddb1d85dbcb88ddbe6b04944566a0f1c5b515118e1ec2e640ffcb30858b3f + languageName: node + linkType: hard + +"event-target-shim@npm:^5.0.0": + version: 5.0.1 + resolution: "event-target-shim@npm:5.0.1" + checksum: 10c0/0255d9f936215fd206156fd4caa9e8d35e62075d720dc7d847e89b417e5e62cf1ce6c9b4e0a1633a9256de0efefaf9f8d26924b1f3c8620cffb9db78e7d3076b + languageName: node + linkType: hard + +"eventemitter3@npm:^4.0.4": + version: 4.0.7 + resolution: "eventemitter3@npm:4.0.7" + checksum: 10c0/5f6d97cbcbac47be798e6355e3a7639a84ee1f7d9b199a07017f1d2f1e2fe236004d14fa5dfaeba661f94ea57805385e326236a6debbc7145c8877fbc0297c6b + languageName: node + linkType: hard + +"form-data-encoder@npm:1.7.2": + version: 1.7.2 + resolution: "form-data-encoder@npm:1.7.2" + checksum: 10c0/56553768037b6d55d9de524f97fe70555f0e415e781cb56fc457a68263de3d40fadea2304d4beef2d40b1a851269bd7854e42c362107071892cb5238debe9464 + languageName: node + linkType: hard + +"form-data@npm:^4.0.0": + version: 4.0.4 + resolution: "form-data@npm:4.0.4" dependencies: - node-domexception "1.0.0" - web-streams-polyfill "4.0.0-beta.3" + asynckit: "npm:^0.4.0" + combined-stream: "npm:^1.0.8" + es-set-tostringtag: "npm:^2.1.0" + hasown: "npm:^2.0.2" + mime-types: "npm:^2.1.12" + checksum: 10c0/373525a9a034b9d57073e55eab79e501a714ffac02e7a9b01be1c820780652b16e4101819785e1e18f8d98f0aee866cc654d660a435c378e16a72f2e7cac9695 + languageName: node + linkType: hard -function-bind@^1.1.2: - version "1.1.2" - resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.2.tgz#2c02d864d97f3ea6c8830c464cbd11ab6eab7a1c" - integrity sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA== - -get-intrinsic@^1.2.6: - version "1.3.0" - resolved "https://registry.yarnpkg.com/get-intrinsic/-/get-intrinsic-1.3.0.tgz#743f0e3b6964a93a5491ed1bffaae054d7f98d01" - integrity sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ== +"formdata-node@npm:^4.3.2": + version: 4.4.1 + resolution: "formdata-node@npm:4.4.1" dependencies: - call-bind-apply-helpers "^1.0.2" - es-define-property "^1.0.1" - es-errors "^1.3.0" - es-object-atoms "^1.1.1" - function-bind "^1.1.2" - get-proto "^1.0.1" - gopd "^1.2.0" - has-symbols "^1.1.0" - hasown "^2.0.2" - math-intrinsics "^1.1.0" + node-domexception: "npm:1.0.0" + web-streams-polyfill: "npm:4.0.0-beta.3" + checksum: 10c0/74151e7b228ffb33b565cec69182694ad07cc3fdd9126a8240468bb70a8ba66e97e097072b60bcb08729b24c7ce3fd3e0bd7f1f80df6f9f662b9656786e76f6a + languageName: node + linkType: hard -get-proto@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/get-proto/-/get-proto-1.0.1.tgz#150b3f2743869ef3e851ec0c49d15b1d14d00ee1" - integrity sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g== +"function-bind@npm:^1.1.2": + version: 1.1.2 + resolution: "function-bind@npm:1.1.2" + checksum: 10c0/d8680ee1e5fcd4c197e4ac33b2b4dce03c71f4d91717292785703db200f5c21f977c568d28061226f9b5900cbcd2c84463646134fd5337e7925e0942bc3f46d5 + languageName: node + linkType: hard + +"get-intrinsic@npm:^1.2.6": + version: 1.3.0 + resolution: "get-intrinsic@npm:1.3.0" dependencies: - dunder-proto "^1.0.1" - es-object-atoms "^1.0.0" + call-bind-apply-helpers: "npm:^1.0.2" + es-define-property: "npm:^1.0.1" + es-errors: "npm:^1.3.0" + es-object-atoms: "npm:^1.1.1" + function-bind: "npm:^1.1.2" + get-proto: "npm:^1.0.1" + gopd: "npm:^1.2.0" + has-symbols: "npm:^1.1.0" + hasown: "npm:^2.0.2" + math-intrinsics: "npm:^1.1.0" + checksum: 10c0/52c81808af9a8130f581e6a6a83e1ba4a9f703359e7a438d1369a5267a25412322f03dcbd7c549edaef0b6214a0630a28511d7df0130c93cfd380f4fa0b5b66a + languageName: node + linkType: hard -gopd@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.2.0.tgz#89f56b8217bdbc8802bd299df6d7f1081d7e51a1" - integrity sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg== - -has-flag@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b" - integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ== - -has-symbols@^1.0.3, has-symbols@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/has-symbols/-/has-symbols-1.1.0.tgz#fc9c6a783a084951d0b971fe1018de813707a338" - integrity sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ== - -has-tostringtag@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/has-tostringtag/-/has-tostringtag-1.0.2.tgz#2cdc42d40bef2e5b4eeab7c01a73c54ce7ab5abc" - integrity sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw== +"get-proto@npm:^1.0.1": + version: 1.0.1 + resolution: "get-proto@npm:1.0.1" dependencies: - has-symbols "^1.0.3" + dunder-proto: "npm:^1.0.1" + es-object-atoms: "npm:^1.0.0" + checksum: 10c0/9224acb44603c5526955e83510b9da41baf6ae73f7398875fba50edc5e944223a89c4a72b070fcd78beb5f7bdda58ecb6294adc28f7acfc0da05f76a2399643c + languageName: node + linkType: hard -hasown@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/hasown/-/hasown-2.0.2.tgz#003eaf91be7adc372e84ec59dc37252cedb80003" - integrity sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ== +"gopd@npm:^1.2.0": + version: 1.2.0 + resolution: "gopd@npm:1.2.0" + checksum: 10c0/50fff1e04ba2b7737c097358534eacadad1e68d24cccee3272e04e007bed008e68d2614f3987788428fd192a5ae3889d08fb2331417e4fc4a9ab366b2043cead + languageName: node + linkType: hard + +"has-flag@npm:^4.0.0": + version: 4.0.0 + resolution: "has-flag@npm:4.0.0" + checksum: 10c0/2e789c61b7888d66993e14e8331449e525ef42aac53c627cc53d1c3334e768bcb6abdc4f5f0de1478a25beec6f0bd62c7549058b7ac53e924040d4f301f02fd1 + languageName: node + linkType: hard + +"has-symbols@npm:^1.0.3, has-symbols@npm:^1.1.0": + version: 1.1.0 + resolution: "has-symbols@npm:1.1.0" + checksum: 10c0/dde0a734b17ae51e84b10986e651c664379018d10b91b6b0e9b293eddb32f0f069688c841fb40f19e9611546130153e0a2a48fd7f512891fb000ddfa36f5a20e + languageName: node + linkType: hard + +"has-tostringtag@npm:^1.0.2": + version: 1.0.2 + resolution: "has-tostringtag@npm:1.0.2" dependencies: - function-bind "^1.1.2" + has-symbols: "npm:^1.0.3" + checksum: 10c0/a8b166462192bafe3d9b6e420a1d581d93dd867adb61be223a17a8d6dad147aa77a8be32c961bb2f27b3ef893cae8d36f564ab651f5e9b7938ae86f74027c48c + languageName: node + linkType: hard -he@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f" - integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw== - -humanize-ms@^1.2.1: - version "1.2.1" - resolved "https://registry.yarnpkg.com/humanize-ms/-/humanize-ms-1.2.1.tgz#c46e3159a293f6b896da29316d8b6fe8bb79bbed" - integrity sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ== +"hasown@npm:^2.0.2": + version: 2.0.2 + resolution: "hasown@npm:2.0.2" dependencies: - ms "^2.0.0" + function-bind: "npm:^1.1.2" + checksum: 10c0/3769d434703b8ac66b209a4cca0737519925bbdb61dd887f93a16372b14694c63ff4e797686d87c90f08168e81082248b9b028bad60d4da9e0d1148766f56eb9 + languageName: node + linkType: hard -ieee754@^1.1.8: - version "1.2.1" - resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" - integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== +"he@npm:^1.2.0": + version: 1.2.0 + resolution: "he@npm:1.2.0" + bin: + he: bin/he + checksum: 10c0/a27d478befe3c8192f006cdd0639a66798979dfa6e2125c6ac582a19a5ebfec62ad83e8382e6036170d873f46e4536a7e795bf8b95bf7c247f4cc0825ccc8c17 + languageName: node + linkType: hard -int64-buffer@^0.1.9: - version "0.1.10" - resolved "https://registry.yarnpkg.com/int64-buffer/-/int64-buffer-0.1.10.tgz#277b228a87d95ad777d07c13832022406a473423" - integrity sha512-v7cSY1J8ydZ0GyjUHqF+1bshJ6cnEVLo9EnjB8p+4HDRPZc9N5jjmvUV7NvEsqQOKyH0pmIBFWXVQbiS0+OBbA== - -is-node-process@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/is-node-process/-/is-node-process-1.2.0.tgz#ea02a1b90ddb3934a19aea414e88edef7e11d134" - integrity sha512-Vg4o6/fqPxIjtxgUH5QLJhwZ7gW5diGCVlXpuUfELC62CuxM1iHcRe51f2W1FDy04Ai4KJkagKjx3XaqyfRKXw== - -isarray@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" - integrity sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ== - -js-tiktoken@^1.0.12: - version "1.0.18" - resolved "https://registry.yarnpkg.com/js-tiktoken/-/js-tiktoken-1.0.18.tgz#aaf68dda155bc693e6f0a572b9a359d569cb53df" - integrity sha512-hFYx4xYf6URgcttcGvGuOBJhTxPYZ2R5eIesqCaNRJmYH8sNmsfTeWg4yu//7u1VD/qIUkgKJTpGom9oHXmB4g== +"humanize-ms@npm:^1.2.1": + version: 1.2.1 + resolution: "humanize-ms@npm:1.2.1" dependencies: - base64-js "^1.5.1" + ms: "npm:^2.0.0" + checksum: 10c0/f34a2c20161d02303c2807badec2f3b49cbfbbb409abd4f95a07377ae01cfe6b59e3d15ac609cffcd8f2521f0eb37b7e1091acf65da99aa2a4f1ad63c21e7e7a + languageName: node + linkType: hard -json-stringify-safe@^5.0.1: - version "5.0.1" - resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" - integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA== +"ieee754@npm:^1.1.8": + version: 1.2.1 + resolution: "ieee754@npm:1.2.1" + checksum: 10c0/b0782ef5e0935b9f12883a2e2aa37baa75da6e66ce6515c168697b42160807d9330de9a32ec1ed73149aea02e0d822e572bca6f1e22bdcbd2149e13b050b17bb + languageName: node + linkType: hard -"langsmith@>=0.2.8 <0.4.0": - version "0.3.7" - resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.3.7.tgz#c29362f78ea2872252a60a680d6adb8b67e18b74" - integrity sha512-wakN1hxGkm1JR2PpAV7fiT7oC99LKcgxiuUrYGZWPbuj7Y8EPF19F7VNr4B+hA219bfaeWTa4Lxy2YrtPSKnQA== +"int64-buffer@npm:^0.1.9": + version: 0.1.10 + resolution: "int64-buffer@npm:0.1.10" + checksum: 10c0/22688f6d1f4db11eaacbf8e7f0b80a23690c29d023987302c367f8c071a53b84fa1cef6f8db0a347e9326f94ff76aa3529e8e9964e99d37fc675f5dcd835ee50 + languageName: node + linkType: hard + +"is-node-process@npm:^1.2.0": + version: 1.2.0 + resolution: "is-node-process@npm:1.2.0" + checksum: 10c0/5b24fda6776d00e42431d7bcd86bce81cb0b6cabeb944142fe7b077a54ada2e155066ad06dbe790abdb397884bdc3151e04a9707b8cd185099efbc79780573ed + languageName: node + linkType: hard + +"isarray@npm:^1.0.0": + version: 1.0.0 + resolution: "isarray@npm:1.0.0" + checksum: 10c0/18b5be6669be53425f0b84098732670ed4e727e3af33bc7f948aac01782110eb9a18b3b329c5323bcdd3acdaae547ee077d3951317e7f133bff7105264b3003d + languageName: node + linkType: hard + +"js-tiktoken@npm:^1.0.12": + version: 1.0.18 + resolution: "js-tiktoken@npm:1.0.18" dependencies: - "@types/uuid" "^10.0.0" - chalk "^4.1.2" - console-table-printer "^2.12.1" - p-queue "^6.6.2" - p-retry "4" - semver "^7.6.3" - uuid "^10.0.0" + base64-js: "npm:^1.5.1" + checksum: 10c0/de2f82d41d49702d42bb417dfc9dc1ce3801d5f04ec0ac73a6f06db5aa3dadfcc13871b30447c28e0b80e37fe76a5052a7c195af657707e7c6753110881d2a26 + languageName: node + linkType: hard -math-intrinsics@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/math-intrinsics/-/math-intrinsics-1.1.0.tgz#a0dd74be81e2aa5c2f27e65ce283605ee4e2b7f9" - integrity sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g== +"json-stringify-safe@npm:^5.0.1": + version: 5.0.1 + resolution: "json-stringify-safe@npm:5.0.1" + checksum: 10c0/7dbf35cd0411d1d648dceb6d59ce5857ec939e52e4afc37601aa3da611f0987d5cee5b38d58329ceddf3ed48bd7215229c8d52059ab01f2444a338bf24ed0f37 + languageName: node + linkType: hard -mime-db@1.52.0: - version "1.52.0" - resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" - integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== - -mime-types@^2.1.12: - version "2.1.35" - resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" - integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== +"langsmith@npm:>=0.2.8 <0.4.0": + version: 0.3.7 + resolution: "langsmith@npm:0.3.7" dependencies: - mime-db "1.52.0" + "@types/uuid": "npm:^10.0.0" + chalk: "npm:^4.1.2" + console-table-printer: "npm:^2.12.1" + p-queue: "npm:^6.6.2" + p-retry: "npm:4" + semver: "npm:^7.6.3" + uuid: "npm:^10.0.0" + peerDependencies: + openai: "*" + peerDependenciesMeta: + openai: + optional: true + checksum: 10c0/68ada1d5120376467bbf7edca17b0629f3d5a2588c91d2396a372b69217e3de960487f1c4109c36e38e0ee6a467d5f81e4b59d8f3312e480af5bb01007d179f3 + languageName: node + linkType: hard -ms@^2.0.0: - version "2.1.3" - resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" - integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== +"math-intrinsics@npm:^1.1.0": + version: 1.1.0 + resolution: "math-intrinsics@npm:1.1.0" + checksum: 10c0/7579ff94e899e2f76ab64491d76cf606274c874d8f2af4a442c016bd85688927fcfca157ba6bf74b08e9439dc010b248ce05b96cc7c126a354c3bae7fcb48b7f + languageName: node + linkType: hard -msgpack-lite@^0.1.26: - version "0.1.26" - resolved "https://registry.yarnpkg.com/msgpack-lite/-/msgpack-lite-0.1.26.tgz#dd3c50b26f059f25e7edee3644418358e2a9ad89" - integrity sha512-SZ2IxeqZ1oRFGo0xFGbvBJWMp3yLIY9rlIJyxy8CGrwZn1f0ZK4r6jV/AM1r0FZMDUkWkglOk/eeKIL9g77Nxw== +"mime-db@npm:1.52.0": + version: 1.52.0 + resolution: "mime-db@npm:1.52.0" + checksum: 10c0/0557a01deebf45ac5f5777fe7740b2a5c309c6d62d40ceab4e23da9f821899ce7a900b7ac8157d4548ddbb7beffe9abc621250e6d182b0397ec7f10c7b91a5aa + languageName: node + linkType: hard + +"mime-types@npm:^2.1.12": + version: 2.1.35 + resolution: "mime-types@npm:2.1.35" dependencies: - event-lite "^0.1.1" - ieee754 "^1.1.8" - int64-buffer "^0.1.9" - isarray "^1.0.0" + mime-db: "npm:1.52.0" + checksum: 10c0/82fb07ec56d8ff1fc999a84f2f217aa46cb6ed1033fefaabd5785b9a974ed225c90dc72fff460259e66b95b73648596dbcc50d51ed69cdf464af2d237d3149b2 + languageName: node + linkType: hard -mustache@^4.2.0: - version "4.2.0" - resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64" - integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ== +"ms@npm:^2.0.0": + version: 2.1.3 + resolution: "ms@npm:2.1.3" + checksum: 10c0/d924b57e7312b3b63ad21fc5b3dc0af5e78d61a1fc7cfb5457edaf26326bf62be5307cc87ffb6862ef1c2b33b0233cdb5d4f01c4c958cc0d660948b65a287a48 + languageName: node + linkType: hard -nock@*, nock@^14.0.1: - version "14.0.1" - resolved "https://registry.yarnpkg.com/nock/-/nock-14.0.1.tgz#62006248bbbc7637322c9fc73f90b93a431b4f5e" - integrity sha512-IJN4O9pturuRdn60NjQ7YkFt6Rwei7ZKaOwb1tvUIIqTgeD0SDDAX3vrqZD4wcXczeEy/AsUXxpGpP/yHqV7xg== +"msgpack-lite@npm:^0.1.26": + version: 0.1.26 + resolution: "msgpack-lite@npm:0.1.26" dependencies: - "@mswjs/interceptors" "^0.37.3" - json-stringify-safe "^5.0.1" - propagate "^2.0.0" + event-lite: "npm:^0.1.1" + ieee754: "npm:^1.1.8" + int64-buffer: "npm:^0.1.9" + isarray: "npm:^1.0.0" + bin: + msgpack: ./bin/msgpack + checksum: 10c0/ba571dca7d789fa033523b74c1aae52bbd023834bcad3f397f481889a8df6cdb6b163b73307be8b744c420ce6d3c0e697f588bb96984c04f9dcf09370b9f12d4 + languageName: node + linkType: hard -node-domexception@1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5" - integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ== +"mustache@npm:^4.2.0": + version: 4.2.0 + resolution: "mustache@npm:4.2.0" + bin: + mustache: bin/mustache + checksum: 10c0/1f8197e8a19e63645a786581d58c41df7853da26702dbc005193e2437c98ca49b255345c173d50c08fe4b4dbb363e53cb655ecc570791f8deb09887248dd34a2 + languageName: node + linkType: hard -node-fetch@^2.6.7: - version "2.7.0" - resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d" - integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A== +"nock@npm:*, nock@npm:^14.0.1": + version: 14.0.1 + resolution: "nock@npm:14.0.1" dependencies: - whatwg-url "^5.0.0" + "@mswjs/interceptors": "npm:^0.37.3" + json-stringify-safe: "npm:^5.0.1" + propagate: "npm:^2.0.0" + checksum: 10c0/258d123eb726f81268ee8ba2b69f8fdd5763c416027542bf5d255dae9c21ab3fcff936f2f57fa829dac4371aea2d4bd34a2dc3837008f317bb9893bf48fe736d + languageName: node + linkType: hard -openai@^4.77.0: - version "4.83.0" - resolved "https://registry.yarnpkg.com/openai/-/openai-4.83.0.tgz#87edfebecf8a4dc2317269dd704cf0ebd9f11979" - integrity sha512-fmTsqud0uTtRKsPC7L8Lu55dkaTwYucqncDHzVvO64DKOpNTuiYwjbR/nVgpapXuYy8xSnhQQPUm+3jQaxICgw== +"node-domexception@npm:1.0.0": + version: 1.0.0 + resolution: "node-domexception@npm:1.0.0" + checksum: 10c0/5e5d63cda29856402df9472335af4bb13875e1927ad3be861dc5ebde38917aecbf9ae337923777af52a48c426b70148815e890a5d72760f1b4d758cc671b1a2b + languageName: node + linkType: hard + +"node-fetch@npm:^2.6.7": + version: 2.7.0 + resolution: "node-fetch@npm:2.7.0" dependencies: - "@types/node" "^18.11.18" - "@types/node-fetch" "^2.6.4" - abort-controller "^3.0.0" - agentkeepalive "^4.2.1" - form-data-encoder "1.7.2" - formdata-node "^4.3.2" - node-fetch "^2.6.7" + whatwg-url: "npm:^5.0.0" + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + checksum: 10c0/b55786b6028208e6fbe594ccccc213cab67a72899c9234eb59dba51062a299ea853210fcf526998eaa2867b0963ad72338824450905679ff0fa304b8c5093ae8 + languageName: node + linkType: hard -outvariant@^1.4.0, outvariant@^1.4.3: - version "1.4.3" - resolved "https://registry.yarnpkg.com/outvariant/-/outvariant-1.4.3.tgz#221c1bfc093e8fec7075497e7799fdbf43d14873" - integrity sha512-+Sl2UErvtsoajRDKCE5/dBz4DIvHXQQnAxtQTF04OJxY0+DyZXSo5P5Bb7XYWOh81syohlYL24hbDwxedPUJCA== - -p-finally@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" - integrity sha512-LICb2p9CB7FS+0eR1oqWnHhp0FljGLZCWBE9aix0Uye9W8LTQPwMTYVGWQWIw9RdQiDg4+epXQODwIYJtSJaow== - -p-queue@^6.6.2: - version "6.6.2" - resolved "https://registry.yarnpkg.com/p-queue/-/p-queue-6.6.2.tgz#2068a9dcf8e67dd0ec3e7a2bcb76810faa85e426" - integrity sha512-RwFpb72c/BhQLEXIZ5K2e+AhgNVmIejGlTgiB9MzZ0e93GRvqZ7uSi0dvRF7/XIXDeNkra2fNHBxTyPDGySpjQ== +"openai@npm:^4.77.0": + version: 4.83.0 + resolution: "openai@npm:4.83.0" dependencies: - eventemitter3 "^4.0.4" - p-timeout "^3.2.0" + "@types/node": "npm:^18.11.18" + "@types/node-fetch": "npm:^2.6.4" + abort-controller: "npm:^3.0.0" + agentkeepalive: "npm:^4.2.1" + form-data-encoder: "npm:1.7.2" + formdata-node: "npm:^4.3.2" + node-fetch: "npm:^2.6.7" + peerDependencies: + ws: ^8.18.0 + zod: ^3.23.8 + peerDependenciesMeta: + ws: + optional: true + zod: + optional: true + bin: + openai: bin/cli + checksum: 10c0/8ca7cf1e67a91b746402575acff035dc664f4b50f95533229caa581a9c4f16e9692765fc53be3e8b0ecda0c5efc6735e803154b02c7d69119149f622792e0bb0 + languageName: node + linkType: hard -p-retry@4: - version "4.6.2" - resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.6.2.tgz#9baae7184057edd4e17231cee04264106e092a16" - integrity sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ== +"outvariant@npm:^1.4.0, outvariant@npm:^1.4.3": + version: 1.4.3 + resolution: "outvariant@npm:1.4.3" + checksum: 10c0/5976ca7740349cb8c71bd3382e2a762b1aeca6f33dc984d9d896acdf3c61f78c3afcf1bfe9cc633a7b3c4b295ec94d292048f83ea2b2594fae4496656eba992c + languageName: node + linkType: hard + +"p-finally@npm:^1.0.0": + version: 1.0.0 + resolution: "p-finally@npm:1.0.0" + checksum: 10c0/6b8552339a71fe7bd424d01d8451eea92d379a711fc62f6b2fe64cad8a472c7259a236c9a22b4733abca0b5666ad503cb497792a0478c5af31ded793d00937e7 + languageName: node + linkType: hard + +"p-queue@npm:^6.6.2": + version: 6.6.2 + resolution: "p-queue@npm:6.6.2" dependencies: - "@types/retry" "0.12.0" - retry "^0.13.1" + eventemitter3: "npm:^4.0.4" + p-timeout: "npm:^3.2.0" + checksum: 10c0/5739ecf5806bbeadf8e463793d5e3004d08bb3f6177bd1a44a005da8fd81bb90f80e4633e1fb6f1dfd35ee663a5c0229abe26aebb36f547ad5a858347c7b0d3e + languageName: node + linkType: hard -p-timeout@^3.2.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-3.2.0.tgz#c7e17abc971d2a7962ef83626b35d635acf23dfe" - integrity sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg== +"p-retry@npm:4": + version: 4.6.2 + resolution: "p-retry@npm:4.6.2" dependencies: - p-finally "^1.0.0" + "@types/retry": "npm:0.12.0" + retry: "npm:^0.13.1" + checksum: 10c0/d58512f120f1590cfedb4c2e0c42cb3fa66f3cea8a4646632fcb834c56055bb7a6f138aa57b20cc236fb207c9d694e362e0b5c2b14d9b062f67e8925580c73b0 + languageName: node + linkType: hard -propagate@^2.0.0: - version "2.0.1" - resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45" - integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag== - -retry@^0.13.1: - version "0.13.1" - resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658" - integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg== - -semver@^7.6.3: - version "7.7.1" - resolved "https://registry.yarnpkg.com/semver/-/semver-7.7.1.tgz#abd5098d82b18c6c81f6074ff2647fd3e7220c9f" - integrity sha512-hlq8tAfn0m/61p4BVRcPzIGr6LKiMwo4VM6dGi6pt4qcRkmNzTcWq6eCEjEh+qXjkMDvPlOFFSGwQjoEa6gyMA== - -simple-wcswidth@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/simple-wcswidth/-/simple-wcswidth-1.0.1.tgz#8ab18ac0ae342f9d9b629604e54d2aa1ecb018b2" - integrity sha512-xMO/8eNREtaROt7tJvWJqHBDTMFN4eiQ5I4JRMuilwfnFcV5W9u7RUkueNkdw0jPqGMX36iCywelS5yilTuOxg== - -strict-event-emitter@^0.5.1: - version "0.5.1" - resolved "https://registry.yarnpkg.com/strict-event-emitter/-/strict-event-emitter-0.5.1.tgz#1602ece81c51574ca39c6815e09f1a3e8550bd93" - integrity sha512-vMgjE/GGEPEFnhFub6pa4FmJBRBVOLpIII2hvCZ8Kzb7K0hlHo7mQv6xYrBvCL2LtAIBwFUK8wvuJgTVSQ5MFQ== - -supports-color@^7.1.0: - version "7.2.0" - resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.2.0.tgz#1b7dcdcb32b8138801b3e478ba6a51caa89648da" - integrity sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw== +"p-timeout@npm:^3.2.0": + version: 3.2.0 + resolution: "p-timeout@npm:3.2.0" dependencies: - has-flag "^4.0.0" + p-finally: "npm:^1.0.0" + checksum: 10c0/524b393711a6ba8e1d48137c5924749f29c93d70b671e6db761afa784726572ca06149c715632da8f70c090073afb2af1c05730303f915604fd38ee207b70a61 + languageName: node + linkType: hard -tr46@~0.0.3: - version "0.0.3" - resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" - integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== +"propagate@npm:^2.0.0": + version: 2.0.1 + resolution: "propagate@npm:2.0.1" + checksum: 10c0/01e1023b60ae4050d1a2783f976d7db702022dbdb70dba797cceedad8cfc01b3939c41e77032f8c32aa9d93192fe937ebba1345e8604e5ce61fd3b62ee3003b8 + languageName: node + linkType: hard -undici-types@~5.26.4: - version "5.26.5" - resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" - integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== +"retry@npm:^0.13.1": + version: 0.13.1 + resolution: "retry@npm:0.13.1" + checksum: 10c0/9ae822ee19db2163497e074ea919780b1efa00431d197c7afdb950e42bf109196774b92a49fc9821f0b8b328a98eea6017410bfc5e8a0fc19c85c6d11adb3772 + languageName: node + linkType: hard -undici-types@~6.20.0: - version "6.20.0" - resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.20.0.tgz#8171bf22c1f588d1554d55bf204bc624af388433" - integrity sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg== +"semver@npm:^7.6.3": + version: 7.7.1 + resolution: "semver@npm:7.7.1" + bin: + semver: bin/semver.js + checksum: 10c0/fd603a6fb9c399c6054015433051bdbe7b99a940a8fb44b85c2b524c4004b023d7928d47cb22154f8d054ea7ee8597f586605e05b52047f048278e4ac56ae958 + languageName: node + linkType: hard -uuid@^10.0.0: - version "10.0.0" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-10.0.0.tgz#5a95aa454e6e002725c79055fd42aaba30ca6294" - integrity sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ== +"simple-wcswidth@npm:^1.0.1": + version: 1.0.1 + resolution: "simple-wcswidth@npm:1.0.1" + checksum: 10c0/2befead4c97134424aa3fba593a81daa9934fd61b9e4c65374b57ac5eecc2f2be1984b017bbdbc919923e19b77f2fcbdb94434789b9643fa8c3fde3a2a6a4b6f + languageName: node + linkType: hard -web-streams-polyfill@4.0.0-beta.3: - version "4.0.0-beta.3" - resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz#2898486b74f5156095e473efe989dcf185047a38" - integrity sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug== +"strict-event-emitter@npm:^0.5.1": + version: 0.5.1 + resolution: "strict-event-emitter@npm:0.5.1" + checksum: 10c0/f5228a6e6b6393c57f52f62e673cfe3be3294b35d6f7842fc24b172ae0a6e6c209fa83241d0e433fc267c503bc2f4ffdbe41a9990ff8ffd5ac425ec0489417f7 + languageName: node + linkType: hard -webidl-conversions@^3.0.0: - version "3.0.1" - resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" - integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== - -whatwg-url@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" - integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw== +"supports-color@npm:^7.1.0": + version: 7.2.0 + resolution: "supports-color@npm:7.2.0" dependencies: - tr46 "~0.0.3" - webidl-conversions "^3.0.0" + has-flag: "npm:^4.0.0" + checksum: 10c0/afb4c88521b8b136b5f5f95160c98dee7243dc79d5432db7efc27efb219385bbc7d9427398e43dd6cc730a0f87d5085ce1652af7efbe391327bc0a7d0f7fc124 + languageName: node + linkType: hard -zod-to-json-schema@^3.22.3: - version "3.24.1" - resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.24.1.tgz#f08c6725091aadabffa820ba8d50c7ab527f227a" - integrity sha512-3h08nf3Vw3Wl3PK+q3ow/lIil81IT2Oa7YpQyUUDsEWbXveMesdfK1xBd2RhCkynwZndAxixji/7SYJJowr62w== +"tr46@npm:~0.0.3": + version: 0.0.3 + resolution: "tr46@npm:0.0.3" + checksum: 10c0/047cb209a6b60c742f05c9d3ace8fa510bff609995c129a37ace03476a9b12db4dbf975e74600830ef0796e18882b2381fb5fb1f6b4f96b832c374de3ab91a11 + languageName: node + linkType: hard -zod@^3.22.4: - version "3.24.1" - resolved "https://registry.yarnpkg.com/zod/-/zod-3.24.1.tgz#27445c912738c8ad1e9de1bea0359fa44d9d35ee" - integrity sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A== +"undici-types@npm:~5.26.4": + version: 5.26.5 + resolution: "undici-types@npm:5.26.5" + checksum: 10c0/bb673d7876c2d411b6eb6c560e0c571eef4a01c1c19925175d16e3a30c4c428181fb8d7ae802a261f283e4166a0ac435e2f505743aa9e45d893f9a3df017b501 + languageName: node + linkType: hard + +"undici-types@npm:~6.20.0": + version: 6.20.0 + resolution: "undici-types@npm:6.20.0" + checksum: 10c0/68e659a98898d6a836a9a59e6adf14a5d799707f5ea629433e025ac90d239f75e408e2e5ff086afc3cace26f8b26ee52155293564593fbb4a2f666af57fc59bf + languageName: node + linkType: hard + +"uuid@npm:^10.0.0": + version: 10.0.0 + resolution: "uuid@npm:10.0.0" + bin: + uuid: dist/bin/uuid + checksum: 10c0/eab18c27fe4ab9fb9709a5d5f40119b45f2ec8314f8d4cf12ce27e4c6f4ffa4a6321dc7db6c515068fa373c075b49691ba969f0010bf37f44c37ca40cd6bf7fe + languageName: node + linkType: hard + +"web-streams-polyfill@npm:4.0.0-beta.3": + version: 4.0.0-beta.3 + resolution: "web-streams-polyfill@npm:4.0.0-beta.3" + checksum: 10c0/a9596779db2766990117ed3a158e0b0e9f69b887a6d6ba0779940259e95f99dc3922e534acc3e5a117b5f5905300f527d6fbf8a9f0957faf1d8e585ce3452e8e + languageName: node + linkType: hard + +"webidl-conversions@npm:^3.0.0": + version: 3.0.1 + resolution: "webidl-conversions@npm:3.0.1" + checksum: 10c0/5612d5f3e54760a797052eb4927f0ddc01383550f542ccd33d5238cfd65aeed392a45ad38364970d0a0f4fea32e1f4d231b3d8dac4a3bdd385e5cf802ae097db + languageName: node + linkType: hard + +"whatwg-url@npm:^5.0.0": + version: 5.0.0 + resolution: "whatwg-url@npm:5.0.0" + dependencies: + tr46: "npm:~0.0.3" + webidl-conversions: "npm:^3.0.0" + checksum: 10c0/1588bed84d10b72d5eec1d0faa0722ba1962f1821e7539c535558fb5398d223b0c50d8acab950b8c488b4ba69043fd833cc2697056b167d8ad46fac3995a55d5 + languageName: node + linkType: hard + +"zod-to-json-schema@npm:^3.22.3": + version: 3.24.1 + resolution: "zod-to-json-schema@npm:3.24.1" + peerDependencies: + zod: ^3.24.1 + checksum: 10c0/dd4e72085003e41a3f532bd00061f27041418a4eb176aa6ce33042db08d141bd37707017ee9117d97738ae3f22fc3e1404ea44e6354634ac5da79d7d3173b4ee + languageName: node + linkType: hard + +"zod@npm:^3.22.4": + version: 3.24.1 + resolution: "zod@npm:3.24.1" + checksum: 10c0/0223d21dbaa15d8928fe0da3b54696391d8e3e1e2d0283a1a070b5980a1dbba945ce631c2d1eccc088fdbad0f2dfa40155590bf83732d3ac4fcca2cc9237591b + languageName: node + linkType: hard