langgraph-api 0.20.0.dev2__py3-none-any.whl → 0.20.0.dev4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langgraph_api/__init__.py +1 -1
- langgraph_api/_checkpointer/_adapter.py +5 -4
- langgraph_api/api/__init__.py +4 -7
- langgraph_api/api/assistants.py +2 -9
- langgraph_api/api/event_streaming.py +1 -5
- langgraph_api/api/mcp/_call_thread.py +1 -1
- langgraph_api/api/meta.py +5 -10
- langgraph_api/api/runs.py +8 -20
- langgraph_api/api/store.py +1 -1
- langgraph_api/api/threads.py +9 -24
- langgraph_api/cache.py +2 -15
- langgraph_api/cli.py +2 -11
- langgraph_api/cron_scheduler.py +2 -23
- langgraph_api/encryption/middleware.py +1 -66
- langgraph_api/errors.py +1 -2
- langgraph_api/event_streaming/state_normalizers.py +4 -0
- langgraph_api/feature_flags.py +8 -9
- langgraph_api/graph.py +3 -133
- langgraph_api/grpc/ops/runs.py +14 -2
- langgraph_api/grpc/ops/threads.py +2 -3
- langgraph_api/js/package.json +10 -10
- langgraph_api/js/remote.py +3 -3
- langgraph_api/js/yarn.lock +214 -168
- langgraph_api/metrics_collector.py +15 -25
- langgraph_api/metrics_otlp.py +2 -2
- langgraph_api/models/run.py +1 -6
- langgraph_api/operator_routes.py +1 -6
- langgraph_api/queue_entrypoint.py +2 -2
- langgraph_api/runtime/encryption.py +11 -149
- langgraph_api/runtime/lifespan.py +2 -2
- langgraph_api/runtime/store.py +134 -2
- langgraph_api/self_hosted_metrics.py +2 -8
- langgraph_api/server.py +1 -1
- langgraph_api/store.py +47 -22
- langgraph_api/stream.py +14 -22
- langgraph_api/transcript/__init__.py +0 -0
- langgraph_api/transcript/input_ids.py +100 -0
- langgraph_api/worker.py +1 -6
- langgraph_api/worker_runtime.py +5 -16
- {langgraph_api-0.20.0.dev2.dist-info → langgraph_api-0.20.0.dev4.dist-info}/METADATA +3 -3
- {langgraph_api-0.20.0.dev2.dist-info → langgraph_api-0.20.0.dev4.dist-info}/RECORD +53 -56
- langgraph_grpc_common/proto/core_api_pb2.py +178 -174
- langgraph_grpc_common/proto/core_api_pb2.pyi +101 -6
- langgraph_runtime/__init__.py +27 -55
- langgraph_runtime/checkpoint.py +5 -1
- langgraph_runtime/database.py +5 -1
- langgraph_runtime/encryption.py +6 -13
- langgraph_runtime/routes.py +6 -9
- langgraph_runtime/store.py +5 -1
- openapi.json +59 -0
- langgraph_runtime/lifespan.py +0 -4
- langgraph_runtime/metrics.py +0 -4
- langgraph_runtime/ops.py +0 -4
- langgraph_runtime/queue.py +0 -4
- langgraph_runtime/retry.py +0 -4
- {langgraph_api-0.20.0.dev2.dist-info → langgraph_api-0.20.0.dev4.dist-info}/WHEEL +0 -0
- {langgraph_api-0.20.0.dev2.dist-info → langgraph_api-0.20.0.dev4.dist-info}/entry_points.txt +0 -0
- {langgraph_api-0.20.0.dev2.dist-info → langgraph_api-0.20.0.dev4.dist-info}/licenses/LICENSE +0 -0
langgraph_api/metrics_otlp.py
CHANGED
|
@@ -269,8 +269,8 @@ GAUGE_PUBLISH_QUEUE_AVAILABILITY = def_gauge(
|
|
|
269
269
|
)
|
|
270
270
|
# Snapshot/state gauges pushed by the periodic
|
|
271
271
|
# metrics collector loop (langgraph_api.metrics_collector).
|
|
272
|
-
# Queue depth
|
|
273
|
-
# pool stats
|
|
272
|
+
# Queue depth comes from the API process, workers_max from wherever workers
|
|
273
|
+
# run, and pool stats from every process.
|
|
274
274
|
GAUGE_WORKERS_MAX = def_gauge("workers_max", METRIC_TIER_CRITICAL, lsd_web_metric=True)
|
|
275
275
|
GAUGE_NUM_PENDING_RUNS = def_gauge(
|
|
276
276
|
"num_pending_runs",
|
langgraph_api/models/run.py
CHANGED
|
@@ -12,12 +12,12 @@ from typing_extensions import TypedDict
|
|
|
12
12
|
|
|
13
13
|
from langgraph_api import config as api_config
|
|
14
14
|
from langgraph_api.auth.noop import UnauthenticatedUser
|
|
15
|
-
from langgraph_api.feature_flags import IS_POSTGRES_OR_GRPC_BACKEND
|
|
16
15
|
from langgraph_api.graph import (
|
|
17
16
|
GRAPHS,
|
|
18
17
|
get_assistant_id,
|
|
19
18
|
inject_current_dd_trace_context,
|
|
20
19
|
)
|
|
20
|
+
from langgraph_api.grpc.ops import Runs
|
|
21
21
|
from langgraph_api.otel_context import inject_current_trace_context
|
|
22
22
|
from langgraph_api.schema import (
|
|
23
23
|
All,
|
|
@@ -41,11 +41,6 @@ from langgraph_api.utils.uuids import uuid7
|
|
|
41
41
|
from langgraph_api.validation import strip_reserved
|
|
42
42
|
from langgraph_api.webhook import validate_or_drop_webhook
|
|
43
43
|
|
|
44
|
-
if IS_POSTGRES_OR_GRPC_BACKEND:
|
|
45
|
-
from langgraph_api.grpc.ops import Runs
|
|
46
|
-
else:
|
|
47
|
-
from langgraph_runtime.ops import Runs
|
|
48
|
-
|
|
49
44
|
if TYPE_CHECKING:
|
|
50
45
|
from starlette.authentication import BaseUser
|
|
51
46
|
|
langgraph_api/operator_routes.py
CHANGED
|
@@ -24,14 +24,9 @@ from starlette.routing import Route
|
|
|
24
24
|
from langgraph_api import config
|
|
25
25
|
from langgraph_api.auth.langsmith.client import create_client
|
|
26
26
|
from langgraph_api.errors import app_exception_handlers
|
|
27
|
-
from langgraph_api.
|
|
27
|
+
from langgraph_api.grpc.ops import Runs
|
|
28
28
|
from langgraph_runtime.database import connect
|
|
29
29
|
|
|
30
|
-
if IS_POSTGRES_OR_GRPC_BACKEND:
|
|
31
|
-
from langgraph_api.grpc.ops import Runs
|
|
32
|
-
else:
|
|
33
|
-
from langgraph_runtime.ops import Runs
|
|
34
|
-
|
|
35
30
|
if TYPE_CHECKING:
|
|
36
31
|
from starlette.requests import Request
|
|
37
32
|
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
|
@@ -21,11 +21,11 @@ import structlog
|
|
|
21
21
|
|
|
22
22
|
from langgraph_api.asyncio import NoSignalServer, run_main_loop
|
|
23
23
|
from langgraph_api.go_prometheus_metrics import merge_go_prometheus_text
|
|
24
|
+
from langgraph_api.runtime import lifespan
|
|
25
|
+
from langgraph_api.runtime.metrics import get_metrics
|
|
24
26
|
from langgraph_api.utils.errors import GraphLoadError, HealthServerStartupError
|
|
25
27
|
from langgraph_api.utils.network import format_hostport, normalize_host
|
|
26
|
-
from langgraph_runtime import lifespan
|
|
27
28
|
from langgraph_runtime.database import healthcheck
|
|
28
|
-
from langgraph_runtime.metrics import get_metrics
|
|
29
29
|
|
|
30
30
|
logger = structlog.stdlib.get_logger(__name__)
|
|
31
31
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""JSON decryption for the in-memory runtime; Go core encrypts.
|
|
2
2
|
|
|
3
3
|
The Postgres runtime replaces this module with its own.
|
|
4
4
|
"""
|
|
@@ -8,38 +8,13 @@ from __future__ import annotations
|
|
|
8
8
|
import asyncio
|
|
9
9
|
from typing import TYPE_CHECKING, Any
|
|
10
10
|
|
|
11
|
-
from langgraph_api.encryption.middleware import
|
|
12
|
-
decrypt_json_if_needed,
|
|
13
|
-
encrypt_json_if_needed,
|
|
14
|
-
)
|
|
15
|
-
from langgraph_api.encryption.shared import (
|
|
16
|
-
BLOB_ENCRYPTION_CONTEXT_KEY,
|
|
17
|
-
ENCRYPTION_CONTEXT_KEY,
|
|
18
|
-
)
|
|
19
|
-
from langgraph_api.serde import json_dumpb, json_loads
|
|
11
|
+
from langgraph_api.encryption.middleware import decrypt_json_if_needed
|
|
20
12
|
|
|
21
13
|
if TYPE_CHECKING:
|
|
22
|
-
from collections.abc import Mapping
|
|
23
|
-
|
|
24
14
|
from langgraph_api.encryption.aes_json import AesEncryptionInstance
|
|
25
15
|
from langgraph_api.encryption.custom import JsonEncryptionWrapper, ModelType
|
|
26
16
|
|
|
27
|
-
|
|
28
|
-
("run", "kwargs"): ("input", "config", "context", "command"),
|
|
29
|
-
("run", "config"): ("configurable", "metadata"),
|
|
30
|
-
("cron", "payload"): ("metadata", "context", "input", "config"),
|
|
31
|
-
("cron", "config"): ("configurable", "metadata"),
|
|
32
|
-
("assistant", "config"): ("configurable",),
|
|
33
|
-
("thread", "config"): ("configurable",),
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
ENCRYPTION_FIELDS: dict[str, tuple[str, ...]] = {
|
|
37
|
-
"thread": ("metadata", "config", "values", "interrupts", "error"),
|
|
38
|
-
"run": ("metadata", "kwargs"),
|
|
39
|
-
"assistant": ("metadata", "config", "context"),
|
|
40
|
-
"cron": ("metadata", "payload"),
|
|
41
|
-
"store": ("value",),
|
|
42
|
-
}
|
|
17
|
+
ENCRYPTION_FIELDS: dict[str, tuple[str, ...]] = {"store": ("value",)}
|
|
43
18
|
|
|
44
19
|
NEVER_ENCRYPT_FIELDS = frozenset(
|
|
45
20
|
{
|
|
@@ -90,138 +65,25 @@ def should_skip_encryption(key: str, path: str) -> bool:
|
|
|
90
65
|
return key in NEVER_ENCRYPT_FIELDS or f"{path}.{key}" in NEVER_ENCRYPT_PATHS
|
|
91
66
|
|
|
92
67
|
|
|
93
|
-
def extract_blob_encryption_context(
|
|
94
|
-
data: dict[str, Any] | None,
|
|
95
|
-
) -> dict[str, Any] | None:
|
|
96
|
-
"""Return checkpoint blob context carried by an encrypted resource."""
|
|
97
|
-
if data is None:
|
|
98
|
-
return None
|
|
99
|
-
|
|
100
|
-
return data.get(BLOB_ENCRYPTION_CONTEXT_KEY) or data.get(ENCRYPTION_CONTEXT_KEY)
|
|
101
|
-
|
|
102
|
-
|
|
103
68
|
def encryption_fields(model_type: ModelType) -> list[str]:
|
|
104
|
-
"""Return fields
|
|
69
|
+
"""Return fields Python still decrypts: custom-store values only."""
|
|
105
70
|
return list(ENCRYPTION_FIELDS[model_type])
|
|
106
71
|
|
|
107
72
|
|
|
108
|
-
async def _decrypt_field(
|
|
109
|
-
obj: dict[str, Any],
|
|
110
|
-
field_name: str,
|
|
111
|
-
encryption_instance: JsonEncryptionWrapper | AesEncryptionInstance | None,
|
|
112
|
-
model_type: ModelType,
|
|
113
|
-
) -> tuple[str, Any]:
|
|
114
|
-
if not obj.get(field_name):
|
|
115
|
-
return field_name, obj.get(field_name)
|
|
116
|
-
|
|
117
|
-
decrypted = await decrypt_json_if_needed(
|
|
118
|
-
obj[field_name], encryption_instance, model_type, field=field_name
|
|
119
|
-
)
|
|
120
|
-
nested_fields = NESTED_ENCRYPTED_SUBFIELDS.get((model_type, field_name), ())
|
|
121
|
-
if decrypted is not None:
|
|
122
|
-
results = await asyncio.gather(
|
|
123
|
-
*(
|
|
124
|
-
_decrypt_field(decrypted, name, encryption_instance, model_type)
|
|
125
|
-
for name in nested_fields
|
|
126
|
-
if isinstance(decrypted.get(name), dict)
|
|
127
|
-
)
|
|
128
|
-
)
|
|
129
|
-
for name, value in results:
|
|
130
|
-
decrypted[name] = value
|
|
131
|
-
return field_name, decrypted
|
|
132
|
-
|
|
133
|
-
|
|
134
73
|
async def decrypt_fields(
|
|
135
74
|
obj: dict[str, Any],
|
|
136
75
|
model_type: ModelType,
|
|
137
76
|
fields: list[str],
|
|
138
77
|
encryption_instance: JsonEncryptionWrapper | AesEncryptionInstance | None,
|
|
139
78
|
) -> None:
|
|
140
|
-
"""Decrypt
|
|
141
|
-
|
|
79
|
+
"""Decrypt top-level fields in place."""
|
|
80
|
+
names = [name for name in fields if obj.get(name)]
|
|
81
|
+
values = await asyncio.gather(
|
|
142
82
|
*(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if name in obj
|
|
146
|
-
)
|
|
147
|
-
)
|
|
148
|
-
for name, value in results:
|
|
149
|
-
obj[name] = value
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
async def _encrypt_field(
|
|
153
|
-
data: Mapping[str, Any],
|
|
154
|
-
field_name: str,
|
|
155
|
-
encryption_instance: JsonEncryptionWrapper | AesEncryptionInstance | None,
|
|
156
|
-
model_type: ModelType,
|
|
157
|
-
path: str | None = None,
|
|
158
|
-
) -> tuple[str, Any]:
|
|
159
|
-
if field_name not in data or data[field_name] is None:
|
|
160
|
-
return field_name, data.get(field_name)
|
|
161
|
-
|
|
162
|
-
field_data = data[field_name]
|
|
163
|
-
if model_type == "thread" and field_name == "values":
|
|
164
|
-
field_data = json_loads(json_dumpb(field_data))
|
|
165
|
-
current_path = f"{path}.{field_name}" if path else f"{model_type}.{field_name}"
|
|
166
|
-
nested_fields = NESTED_ENCRYPTED_SUBFIELDS.get((model_type, field_name), ())
|
|
167
|
-
extracted: dict[str, Any] = {}
|
|
168
|
-
if nested_fields:
|
|
169
|
-
if not isinstance(field_data, dict):
|
|
170
|
-
raise TypeError(
|
|
171
|
-
f"'{field_name}' must be a dict for encryption, "
|
|
172
|
-
f"got {type(field_data).__name__}"
|
|
173
|
-
)
|
|
174
|
-
for name in nested_fields:
|
|
175
|
-
value = field_data.get(name)
|
|
176
|
-
if isinstance(value, dict) and value:
|
|
177
|
-
extracted[name] = value
|
|
178
|
-
if extracted:
|
|
179
|
-
field_data = {k: v for k, v in field_data.items() if k not in extracted}
|
|
180
|
-
|
|
181
|
-
encrypted = await encrypt_json_if_needed(
|
|
182
|
-
field_data,
|
|
183
|
-
encryption_instance,
|
|
184
|
-
model_type,
|
|
185
|
-
field=field_name,
|
|
186
|
-
path=current_path,
|
|
187
|
-
should_skip=should_skip_encryption,
|
|
188
|
-
)
|
|
189
|
-
if extracted and isinstance(encrypted, dict):
|
|
190
|
-
results = await asyncio.gather(
|
|
191
|
-
*(
|
|
192
|
-
_encrypt_field(
|
|
193
|
-
{name: value}, name, encryption_instance, model_type, current_path
|
|
194
|
-
)
|
|
195
|
-
for name, value in extracted.items()
|
|
83
|
+
decrypt_json_if_needed(
|
|
84
|
+
obj[name], encryption_instance, model_type, field=name
|
|
196
85
|
)
|
|
197
|
-
|
|
198
|
-
for name, value in results:
|
|
199
|
-
encrypted[name] = value
|
|
200
|
-
marker = value.get("__encryption_context__")
|
|
201
|
-
if (
|
|
202
|
-
"__encryption_context__" not in encrypted
|
|
203
|
-
and isinstance(marker, dict)
|
|
204
|
-
and marker.get("__langgraph_encryption_type__") == "aes"
|
|
205
|
-
):
|
|
206
|
-
encrypted["__encryption_context__"] = marker
|
|
207
|
-
return field_name, encrypted
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
async def encrypt_fields(
|
|
211
|
-
data: Mapping[str, Any],
|
|
212
|
-
model_type: ModelType,
|
|
213
|
-
fields: list[str],
|
|
214
|
-
encryption_instance: JsonEncryptionWrapper | AesEncryptionInstance | None,
|
|
215
|
-
) -> dict[str, Any]:
|
|
216
|
-
"""Encrypt configured fields, preserving local-runtime nested structure."""
|
|
217
|
-
result = dict(data)
|
|
218
|
-
encrypted_fields = await asyncio.gather(
|
|
219
|
-
*(
|
|
220
|
-
_encrypt_field(data, name, encryption_instance, model_type)
|
|
221
|
-
for name in fields
|
|
222
|
-
if name in data
|
|
86
|
+
for name in names
|
|
223
87
|
)
|
|
224
88
|
)
|
|
225
|
-
|
|
226
|
-
result[name] = value
|
|
227
|
-
return result
|
|
89
|
+
obj.update(zip(names, values, strict=True))
|
|
@@ -11,7 +11,7 @@ from starlette.applications import Starlette
|
|
|
11
11
|
|
|
12
12
|
from langgraph_api import config, embedded_core
|
|
13
13
|
from langgraph_api.config import BG_JOB_SHUTDOWN_GRACE_PERIOD_SECS
|
|
14
|
-
from langgraph_api.feature_flags import
|
|
14
|
+
from langgraph_api.feature_flags import IS_INMEM_BACKEND
|
|
15
15
|
from langgraph_api.runtime import queue
|
|
16
16
|
from langgraph_license.validation import (
|
|
17
17
|
check_license_periodically,
|
|
@@ -55,7 +55,7 @@ async def _embedded_core():
|
|
|
55
55
|
LSD_GRPC_SERVER_ADDRESS is ignored: the in-memory runtime only talks to its
|
|
56
56
|
own SQLite core, never to an external one.
|
|
57
57
|
"""
|
|
58
|
-
if not
|
|
58
|
+
if not IS_INMEM_BACKEND:
|
|
59
59
|
yield
|
|
60
60
|
return
|
|
61
61
|
if os.environ.get("LSD_GRPC_SERVER_ADDRESS"):
|
langgraph_api/runtime/store.py
CHANGED
|
@@ -1,11 +1,143 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import importlib
|
|
3
|
+
import importlib.util
|
|
4
|
+
import sys
|
|
1
5
|
import threading
|
|
2
|
-
|
|
6
|
+
import warnings
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from typing import TYPE_CHECKING, Any
|
|
3
9
|
|
|
4
10
|
from langgraph_grpc_common.store import GrpcStore
|
|
5
11
|
|
|
6
12
|
import langgraph_api.config as api_config
|
|
13
|
+
from langgraph_api import timing
|
|
7
14
|
from langgraph_api.encryption.context import get_encryption_context
|
|
8
|
-
from langgraph_api.
|
|
15
|
+
from langgraph_api.timing import profiled_import
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from langchain_core.embeddings import Embeddings
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@functools.lru_cache(maxsize=1)
|
|
22
|
+
def _get_init_embeddings() -> Callable[[str, ...], "Embeddings"] | None:
|
|
23
|
+
try:
|
|
24
|
+
from langchain.embeddings import ( # noqa: PLC0415 # ty: ignore[unresolved-import]
|
|
25
|
+
init_embeddings,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
return init_embeddings
|
|
29
|
+
except ImportError:
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@timing.timer(
|
|
34
|
+
message="Loading embeddings {embeddings_path}",
|
|
35
|
+
metadata_fn=lambda index_config: {"embeddings_path": index_config.get("embed")},
|
|
36
|
+
warn_threshold_secs=5,
|
|
37
|
+
warn_message="Loading embeddings '{embeddings_path}' took longer than expected",
|
|
38
|
+
error_threshold_secs=10,
|
|
39
|
+
)
|
|
40
|
+
def resolve_embeddings(index_config: dict) -> "Embeddings":
|
|
41
|
+
"""Return embeddings from config.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
index_config: Configuration for the vector store index
|
|
45
|
+
Must contain an "embed" key specifying either:
|
|
46
|
+
- A path to a Python file and function (e.g. "./embeddings.py:get_embeddings")
|
|
47
|
+
- A LangChain embeddings identifier (e.g. "openai:text-embedding-3-small")
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
Embeddings: A LangChain embeddings instance
|
|
51
|
+
|
|
52
|
+
Raises:
|
|
53
|
+
ValueError: If embeddings cannot be loaded from the config
|
|
54
|
+
"""
|
|
55
|
+
from langchain_core.embeddings import Embeddings # noqa: PLC0415
|
|
56
|
+
from langgraph.store.base import ensure_embeddings # noqa: PLC0415
|
|
57
|
+
|
|
58
|
+
embed = index_config["embed"]
|
|
59
|
+
if isinstance(embed, Embeddings):
|
|
60
|
+
return embed
|
|
61
|
+
if callable(embed):
|
|
62
|
+
return ensure_embeddings(embed)
|
|
63
|
+
if not isinstance(embed, str):
|
|
64
|
+
raise ValueError(
|
|
65
|
+
f"Embeddings config must be a string or callable, got: {type(embed).__name__}"
|
|
66
|
+
)
|
|
67
|
+
if ".py:" in embed:
|
|
68
|
+
module_name, function = embed.rsplit(":", 1)
|
|
69
|
+
module_name = module_name.rstrip(":")
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
with profiled_import(embed):
|
|
73
|
+
if "/" in module_name:
|
|
74
|
+
# Load from file path
|
|
75
|
+
modname = (
|
|
76
|
+
module_name.replace("/", "__")
|
|
77
|
+
.replace(".py", "")
|
|
78
|
+
.replace(" ", "_")
|
|
79
|
+
)
|
|
80
|
+
modspec = importlib.util.spec_from_file_location(
|
|
81
|
+
modname, module_name
|
|
82
|
+
)
|
|
83
|
+
if modspec is None:
|
|
84
|
+
raise ValueError(
|
|
85
|
+
f"Could not find embeddings file: {module_name}"
|
|
86
|
+
)
|
|
87
|
+
module = importlib.util.module_from_spec(modspec)
|
|
88
|
+
sys.modules[modname] = module
|
|
89
|
+
modspec.loader.exec_module(module)
|
|
90
|
+
else:
|
|
91
|
+
# Load from Python module
|
|
92
|
+
module = importlib.import_module(module_name)
|
|
93
|
+
|
|
94
|
+
embedding_fn = getattr(module, function, None)
|
|
95
|
+
if embedding_fn is None:
|
|
96
|
+
raise ValueError(
|
|
97
|
+
f"Could not find embeddings function '{function}' in module: {module_name}"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if isinstance(embedding_fn, Embeddings):
|
|
101
|
+
return embedding_fn
|
|
102
|
+
elif not callable(embedding_fn):
|
|
103
|
+
raise ValueError(
|
|
104
|
+
f"Embeddings function '{function}' in module: {module_name} is not callable"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
return ensure_embeddings(embedding_fn)
|
|
108
|
+
|
|
109
|
+
except ImportError as e:
|
|
110
|
+
e.add_note(f"Could not import embeddings module:\n{module_name}\n\n")
|
|
111
|
+
if api_config.API_VARIANT == "local_dev":
|
|
112
|
+
e.add_note(
|
|
113
|
+
"If you're in development mode, make sure you've installed your project "
|
|
114
|
+
"and its dependencies:\n"
|
|
115
|
+
"- For requirements.txt: pip install -r requirements.txt\n"
|
|
116
|
+
"- For pyproject.toml: pip install -e .\n"
|
|
117
|
+
)
|
|
118
|
+
raise
|
|
119
|
+
except FileNotFoundError as e:
|
|
120
|
+
raise ValueError(f"Could not find embeddings file: {module_name}") from e
|
|
121
|
+
|
|
122
|
+
else:
|
|
123
|
+
# Load from LangChain embeddings
|
|
124
|
+
init_embeddings = _get_init_embeddings()
|
|
125
|
+
if init_embeddings is None:
|
|
126
|
+
raise ValueError(
|
|
127
|
+
f"Could not load LangChain embeddings '{embed}'. "
|
|
128
|
+
"Loading embeddings by provider:identifier requires the langchain package (>=0.3.9). "
|
|
129
|
+
"Install it with: pip install 'langchain>=0.3.9'"
|
|
130
|
+
" or specify 'embed' as a path to a "
|
|
131
|
+
"variable in a Python file instead."
|
|
132
|
+
)
|
|
133
|
+
# Capture warnings
|
|
134
|
+
with warnings.catch_warnings():
|
|
135
|
+
warnings.filterwarnings(
|
|
136
|
+
"ignore",
|
|
137
|
+
message=("The function `init_embeddings` is in beta."),
|
|
138
|
+
)
|
|
139
|
+
return init_embeddings(embed)
|
|
140
|
+
|
|
9
141
|
|
|
10
142
|
_STORE_CONFIG: api_config.StoreConfig = {}
|
|
11
143
|
|
|
@@ -13,16 +13,10 @@ from opentelemetry.sdk.resources import SERVICE_NAME, Resource
|
|
|
13
13
|
from langgraph_api import asyncio as lg_asyncio
|
|
14
14
|
from langgraph_api import config
|
|
15
15
|
from langgraph_api.api.meta import meta_pool_stats
|
|
16
|
-
from langgraph_api.
|
|
16
|
+
from langgraph_api.grpc.ops import Runs
|
|
17
17
|
from langgraph_api.http_metrics_utils import HTTP_LATENCY_BUCKETS
|
|
18
|
+
from langgraph_api.runtime.metrics import get_metrics
|
|
18
19
|
from langgraph_runtime.database import connect
|
|
19
|
-
from langgraph_runtime.metrics import get_metrics
|
|
20
|
-
|
|
21
|
-
if IS_POSTGRES_OR_GRPC_BACKEND:
|
|
22
|
-
from langgraph_api.grpc.ops import Runs
|
|
23
|
-
else:
|
|
24
|
-
from langgraph_runtime.ops import Runs
|
|
25
|
-
|
|
26
20
|
|
|
27
21
|
logger = structlog.stdlib.get_logger(__name__)
|
|
28
22
|
|
langgraph_api/server.py
CHANGED
|
@@ -44,8 +44,8 @@ from langgraph_api.middleware.http_logger import AccessLoggerMiddleware
|
|
|
44
44
|
from langgraph_api.middleware.private_network import PrivateNetworkMiddleware
|
|
45
45
|
from langgraph_api.middleware.request_id import RequestIdMiddleware
|
|
46
46
|
from langgraph_api.operator_routes import OperatorRoutesMiddleware
|
|
47
|
+
from langgraph_api.runtime.lifespan import lifespan
|
|
47
48
|
from langgraph_api.utils import SchemaGenerator
|
|
48
|
-
from langgraph_runtime.lifespan import lifespan
|
|
49
49
|
|
|
50
50
|
logging.captureWarnings(True)
|
|
51
51
|
logger = structlog.stdlib.get_logger(__name__)
|
langgraph_api/store.py
CHANGED
|
@@ -2,10 +2,10 @@ import asyncio
|
|
|
2
2
|
import importlib.util
|
|
3
3
|
import sys
|
|
4
4
|
import threading
|
|
5
|
-
from collections.abc import Callable
|
|
5
|
+
from collections.abc import Callable, Mapping
|
|
6
6
|
from contextlib import AsyncExitStack, asynccontextmanager
|
|
7
7
|
from random import choice
|
|
8
|
-
from typing import Any
|
|
8
|
+
from typing import Any, cast
|
|
9
9
|
|
|
10
10
|
import structlog
|
|
11
11
|
from langgraph.graph import StateGraph
|
|
@@ -14,14 +14,14 @@ from langgraph.store.base import BaseStore
|
|
|
14
14
|
|
|
15
15
|
from langgraph_api import config, timing
|
|
16
16
|
from langgraph_api.asyncio import as_asynccontextmanager
|
|
17
|
-
from langgraph_api.encryption
|
|
18
|
-
|
|
19
|
-
encrypt_request,
|
|
20
|
-
)
|
|
17
|
+
from langgraph_api.encryption import get_encryption
|
|
18
|
+
from langgraph_api.encryption.custom import ModelType
|
|
21
19
|
from langgraph_api.encryption.shared import ENCRYPTION_CONTEXT_KEY
|
|
22
|
-
from langgraph_api.
|
|
20
|
+
from langgraph_api.serde import Fragment, json_loads
|
|
23
21
|
from langgraph_api.timing import profiled_import
|
|
24
22
|
from langgraph_api.utils.config import run_in_executor
|
|
23
|
+
from langgraph_runtime.encryption import decrypt_fields, encryption_fields
|
|
24
|
+
from langgraph_runtime.store import Store
|
|
25
25
|
|
|
26
26
|
logger = structlog.stdlib.get_logger(__name__)
|
|
27
27
|
|
|
@@ -40,24 +40,54 @@ async def get_store() -> BaseStore:
|
|
|
40
40
|
return store
|
|
41
41
|
return STORE_STACK.store
|
|
42
42
|
else:
|
|
43
|
-
from langgraph_runtime.store import Store # noqa: PLC0415
|
|
44
|
-
|
|
45
43
|
return Store()
|
|
46
44
|
|
|
47
45
|
|
|
48
|
-
# Go core encrypts the
|
|
49
|
-
#
|
|
50
|
-
|
|
51
|
-
return IS_POSTGRES_OR_GRPC_BACKEND or CUSTOM_STORE is not None
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
# Go core encrypts the built-in store, and custom stores own their data format
|
|
47
|
+
# and remain plaintext here. Custom-store values written with an encryption
|
|
48
|
+
# context marker are still decrypted on read.
|
|
54
49
|
def _bypass_store_decryption(item: dict[str, Any]) -> bool:
|
|
55
50
|
if CUSTOM_STORE is None:
|
|
56
|
-
return
|
|
51
|
+
return True
|
|
57
52
|
value = item.get("value")
|
|
58
53
|
return not isinstance(value, dict) or ENCRYPTION_CONTEXT_KEY not in value
|
|
59
54
|
|
|
60
55
|
|
|
56
|
+
async def decrypt_response(
|
|
57
|
+
obj: Mapping[str, Any],
|
|
58
|
+
model_type: ModelType,
|
|
59
|
+
*,
|
|
60
|
+
plaintext_from_core: bool,
|
|
61
|
+
) -> dict[str, Any]:
|
|
62
|
+
"""Decrypt runtime-owned fields, or return Go-decrypted data unchanged."""
|
|
63
|
+
if plaintext_from_core:
|
|
64
|
+
return obj
|
|
65
|
+
|
|
66
|
+
encryption_instance = get_encryption()
|
|
67
|
+
if encryption_instance is None:
|
|
68
|
+
# Even without encryption, the error field is stored as bytes (bytea column)
|
|
69
|
+
# and must be parsed to return as a dict in the API response.
|
|
70
|
+
error = obj.get("error")
|
|
71
|
+
if isinstance(error, bytes | memoryview | Fragment):
|
|
72
|
+
result = dict(obj)
|
|
73
|
+
error_bytes = cast(
|
|
74
|
+
"bytes | Fragment",
|
|
75
|
+
bytes(error) if isinstance(error, memoryview) else error,
|
|
76
|
+
)
|
|
77
|
+
result["error"] = json_loads(error_bytes)
|
|
78
|
+
return result
|
|
79
|
+
return obj
|
|
80
|
+
|
|
81
|
+
field_names = encryption_fields(model_type)
|
|
82
|
+
result = dict(obj)
|
|
83
|
+
for field_name in field_names:
|
|
84
|
+
value = result.get(field_name)
|
|
85
|
+
if isinstance(value, (bytes, bytearray, memoryview, str, Fragment)):
|
|
86
|
+
result[field_name] = json_loads(value)
|
|
87
|
+
await decrypt_fields(result, model_type, field_names, encryption_instance)
|
|
88
|
+
return result
|
|
89
|
+
|
|
90
|
+
|
|
61
91
|
async def _decrypt_store_item(item: Any) -> dict[str, Any]:
|
|
62
92
|
data = item.dict()
|
|
63
93
|
return await decrypt_response(
|
|
@@ -73,12 +103,7 @@ async def put_item(
|
|
|
73
103
|
index: bool | list[str] | None,
|
|
74
104
|
ttl: float | None,
|
|
75
105
|
) -> None:
|
|
76
|
-
|
|
77
|
-
{"value": value}, "store", plaintext_for_core=_bypass_store_encryption()
|
|
78
|
-
)
|
|
79
|
-
await (await get_store()).aput(
|
|
80
|
-
namespace, key, payload["value"], index=index, ttl=ttl
|
|
81
|
-
)
|
|
106
|
+
await (await get_store()).aput(namespace, key, value, index=index, ttl=ttl)
|
|
82
107
|
|
|
83
108
|
|
|
84
109
|
async def get_item(
|
langgraph_api/stream.py
CHANGED
|
@@ -43,14 +43,13 @@ from langgraph_api.config import (
|
|
|
43
43
|
)
|
|
44
44
|
from langgraph_api.drain import mark_top_level
|
|
45
45
|
from langgraph_api.feature_flags import (
|
|
46
|
-
|
|
46
|
+
FF_TRANSCRIPT_API,
|
|
47
47
|
TOOLS_STREAM_SUPPORT,
|
|
48
48
|
UPDATES_NEEDED_FOR_INTERRUPTS,
|
|
49
49
|
USE_DURABILITY,
|
|
50
50
|
USE_RUNTIME_CONTEXT_API,
|
|
51
51
|
)
|
|
52
52
|
from langgraph_api.graph import get_graph
|
|
53
|
-
from langgraph_api.grpc.ops.runs import Runs as GrpcRuns
|
|
54
53
|
from langgraph_api.grpc.ops.runs import StreamPublishException
|
|
55
54
|
from langgraph_api.js.base import BaseRemotePregel
|
|
56
55
|
from langgraph_api.lifecycle import (
|
|
@@ -80,11 +79,9 @@ from langgraph_api.utils.uuids import uuid7
|
|
|
80
79
|
if TYPE_CHECKING:
|
|
81
80
|
from langchain_core.runnables import RunnableConfig
|
|
82
81
|
|
|
83
|
-
|
|
84
|
-
from langgraph_api.grpc.ops import Runs
|
|
85
|
-
else:
|
|
86
|
-
from langgraph_runtime.ops import Runs
|
|
82
|
+
from langgraph_api.grpc.ops import Runs
|
|
87
83
|
from langgraph_api.tracing_session import run_tracing_replicas
|
|
84
|
+
from langgraph_api.transcript.input_ids import assign_input_message_ids
|
|
88
85
|
|
|
89
86
|
logger = structlog.stdlib.get_logger(__name__)
|
|
90
87
|
|
|
@@ -343,6 +340,8 @@ async def astream_state(
|
|
|
343
340
|
if strict_resume:
|
|
344
341
|
configurable[STRICT_RESUME_KEY] = True
|
|
345
342
|
config = {**config, "configurable": configurable}
|
|
343
|
+
if FF_TRANSCRIPT_API and not is_remote_pregel:
|
|
344
|
+
input = assign_input_message_ids(graph.channels, input, run_id)
|
|
346
345
|
# A Command's first checkpoint keeps the id of the one it resumes but follows the
|
|
347
346
|
# Command's update, so a join resumed after its marker would skip the update.
|
|
348
347
|
skip_next_marker = isinstance(input, Command)
|
|
@@ -351,14 +350,13 @@ async def astream_state(
|
|
|
351
350
|
# round-trip converts those to ``None``.
|
|
352
351
|
stream_mode = [mode for mode in stream_mode if mode is not None]
|
|
353
352
|
# Root checkpoint markers let a run's join skip what a client already
|
|
354
|
-
# loaded.
|
|
353
|
+
# loaded. The Go core drops them before they reach clients. Under exit
|
|
355
354
|
# durability no step is saved, and the JS loop and langgraph < 0.6 then report
|
|
356
355
|
# every step under the last saved checkpoint's id. A JS graph's next step can send
|
|
357
356
|
# its `events` starts and legacy `messages/metadata` before the checkpoint's event,
|
|
358
357
|
# so a join after the marker would miss them; unmarked, such a join replays the run.
|
|
359
358
|
mark_checkpoints = (
|
|
360
|
-
|
|
361
|
-
and kwargs.get("durability") != "exit"
|
|
359
|
+
kwargs.get("durability") != "exit"
|
|
362
360
|
and kwargs.get("checkpoint_during") is not False
|
|
363
361
|
and (not is_remote_pregel or {"events", "messages"}.isdisjoint(stream_mode))
|
|
364
362
|
)
|
|
@@ -890,20 +888,14 @@ def publish_event(
|
|
|
890
888
|
earlier attempt; a single event, like the worker's terminal error, gets a
|
|
891
889
|
publisher id of its own with seq 0.
|
|
892
890
|
"""
|
|
893
|
-
if IS_POSTGRES_OR_GRPC_BACKEND:
|
|
894
|
-
return GrpcRuns.Stream.publish(
|
|
895
|
-
run_id,
|
|
896
|
-
mode,
|
|
897
|
-
payload,
|
|
898
|
-
thread_id=thread_id,
|
|
899
|
-
resumable=resumable,
|
|
900
|
-
publisher_id=publisher_id,
|
|
901
|
-
seq=seq,
|
|
902
|
-
)
|
|
903
|
-
# langgraph-runtime-inmem is a separate package whose publish takes no such
|
|
904
|
-
# keywords, and its in-process publish never retries.
|
|
905
891
|
return Runs.Stream.publish(
|
|
906
|
-
run_id,
|
|
892
|
+
run_id,
|
|
893
|
+
mode,
|
|
894
|
+
payload,
|
|
895
|
+
thread_id=thread_id,
|
|
896
|
+
resumable=resumable,
|
|
897
|
+
publisher_id=publisher_id,
|
|
898
|
+
seq=seq,
|
|
907
899
|
)
|
|
908
900
|
|
|
909
901
|
|
|
File without changes
|