langchain-core 0.3.79__py3-none-any.whl → 1.0.0__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.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +52 -65
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +19 -19
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +323 -334
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +441 -507
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +48 -63
- langchain_core/document_loaders/base.py +23 -23
- langchain_core/document_loaders/langsmith.py +37 -37
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +62 -65
- langchain_core/documents/compressor.py +4 -4
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +10 -11
- langchain_core/globals.py +3 -151
- langchain_core/indexing/api.py +61 -66
- langchain_core/indexing/base.py +58 -58
- langchain_core/indexing/in_memory.py +3 -3
- langchain_core/language_models/__init__.py +14 -27
- langchain_core/language_models/_utils.py +270 -84
- langchain_core/language_models/base.py +55 -162
- langchain_core/language_models/chat_models.py +442 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +61 -39
- langchain_core/language_models/llms.py +123 -231
- langchain_core/load/dump.py +4 -5
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +39 -40
- langchain_core/messages/__init__.py +61 -22
- langchain_core/messages/ai.py +368 -163
- langchain_core/messages/base.py +214 -43
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +41 -38
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +38 -28
- langchain_core/messages/tool.py +96 -103
- langchain_core/messages/utils.py +478 -504
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +58 -61
- langchain_core/output_parsers/json.py +7 -8
- langchain_core/output_parsers/list.py +5 -7
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +14 -19
- langchain_core/output_parsers/pydantic.py +12 -13
- langchain_core/output_parsers/string.py +2 -2
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +8 -10
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +8 -8
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +12 -12
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +45 -55
- langchain_core/prompts/chat.py +254 -313
- langchain_core/prompts/dict.py +5 -5
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +4 -4
- langchain_core/prompts/structured.py +42 -50
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +49 -190
- langchain_core/runnables/base.py +1484 -1709
- langchain_core/runnables/branch.py +45 -61
- langchain_core/runnables/config.py +80 -88
- langchain_core/runnables/configurable.py +117 -134
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +85 -95
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +38 -50
- langchain_core/runnables/graph_png.py +15 -16
- langchain_core/runnables/history.py +135 -148
- langchain_core/runnables/passthrough.py +124 -150
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +25 -30
- langchain_core/runnables/schema.py +79 -74
- langchain_core/runnables/utils.py +62 -68
- langchain_core/stores.py +81 -115
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +179 -187
- langchain_core/tools/convert.py +131 -139
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +11 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +30 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +50 -60
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +115 -70
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +43 -43
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +46 -8
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -191
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +37 -42
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +8 -8
- langchain_core/utils/mustache.py +9 -11
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +80 -54
- langchain_core/vectorstores/base.py +129 -164
- langchain_core/vectorstores/in_memory.py +99 -174
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -447
- langchain_core/memory.py +0 -120
- langchain_core/messages/content_blocks.py +0 -176
- langchain_core/prompts/pipeline.py +0 -138
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-0.3.79.dist-info/RECORD +0 -174
- langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
langchain_core/utils/utils.py
CHANGED
|
@@ -6,9 +6,10 @@ import functools
|
|
|
6
6
|
import importlib
|
|
7
7
|
import os
|
|
8
8
|
import warnings
|
|
9
|
-
from collections.abc import Iterator, Sequence
|
|
9
|
+
from collections.abc import Callable, Iterator, Sequence
|
|
10
10
|
from importlib.metadata import version
|
|
11
|
-
from typing import Any,
|
|
11
|
+
from typing import Any, overload
|
|
12
|
+
from uuid import uuid4
|
|
12
13
|
|
|
13
14
|
from packaging.version import parse
|
|
14
15
|
from pydantic import SecretStr
|
|
@@ -24,11 +25,11 @@ def xor_args(*arg_groups: tuple[str, ...]) -> Callable:
|
|
|
24
25
|
"""Validate specified keyword args are mutually exclusive.
|
|
25
26
|
|
|
26
27
|
Args:
|
|
27
|
-
*arg_groups
|
|
28
|
+
*arg_groups: Groups of mutually exclusive keyword args.
|
|
28
29
|
|
|
29
30
|
Returns:
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
Decorator that validates the specified keyword args
|
|
32
|
+
are mutually exclusive.
|
|
32
33
|
"""
|
|
33
34
|
|
|
34
35
|
def decorator(func: Callable) -> Callable:
|
|
@@ -59,7 +60,7 @@ def raise_for_status_with_text(response: Response) -> None:
|
|
|
59
60
|
"""Raise an error with the response text.
|
|
60
61
|
|
|
61
62
|
Args:
|
|
62
|
-
response
|
|
63
|
+
response: The response to check for errors.
|
|
63
64
|
|
|
64
65
|
Raises:
|
|
65
66
|
ValueError: If the response has an error status code.
|
|
@@ -78,11 +79,13 @@ def mock_now(dt_value: datetime.datetime) -> Iterator[type]:
|
|
|
78
79
|
dt_value: The datetime value to use for datetime.now().
|
|
79
80
|
|
|
80
81
|
Yields:
|
|
81
|
-
|
|
82
|
+
The mocked datetime class.
|
|
82
83
|
|
|
83
84
|
Example:
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
```python
|
|
86
|
+
with mock_now(datetime.datetime(2011, 2, 3, 10, 11)):
|
|
87
|
+
assert datetime.datetime.now() == datetime.datetime(2011, 2, 3, 10, 11)
|
|
88
|
+
```
|
|
86
89
|
"""
|
|
87
90
|
|
|
88
91
|
class MockDateTime(datetime.datetime):
|
|
@@ -90,7 +93,7 @@ def mock_now(dt_value: datetime.datetime) -> Iterator[type]:
|
|
|
90
93
|
|
|
91
94
|
@classmethod
|
|
92
95
|
@override
|
|
93
|
-
def now(cls, tz:
|
|
96
|
+
def now(cls, tz: datetime.tzinfo | None = None) -> "MockDateTime":
|
|
94
97
|
# Create a copy of dt_value.
|
|
95
98
|
return MockDateTime(
|
|
96
99
|
dt_value.year,
|
|
@@ -112,21 +115,19 @@ def mock_now(dt_value: datetime.datetime) -> Iterator[type]:
|
|
|
112
115
|
|
|
113
116
|
|
|
114
117
|
def guard_import(
|
|
115
|
-
module_name: str, *, pip_name:
|
|
118
|
+
module_name: str, *, pip_name: str | None = None, package: str | None = None
|
|
116
119
|
) -> Any:
|
|
117
120
|
"""Dynamically import a module.
|
|
118
121
|
|
|
119
122
|
Raise an exception if the module is not installed.
|
|
120
123
|
|
|
121
124
|
Args:
|
|
122
|
-
module_name
|
|
123
|
-
pip_name
|
|
124
|
-
|
|
125
|
-
package (str, optional): The package to import the module from.
|
|
126
|
-
Defaults to None.
|
|
125
|
+
module_name: The name of the module to import.
|
|
126
|
+
pip_name: The name of the module to install with pip.
|
|
127
|
+
package: The package to import the module from.
|
|
127
128
|
|
|
128
129
|
Returns:
|
|
129
|
-
|
|
130
|
+
The imported module.
|
|
130
131
|
|
|
131
132
|
Raises:
|
|
132
133
|
ImportError: If the module is not installed.
|
|
@@ -145,23 +146,20 @@ def guard_import(
|
|
|
145
146
|
|
|
146
147
|
def check_package_version(
|
|
147
148
|
package: str,
|
|
148
|
-
lt_version:
|
|
149
|
-
lte_version:
|
|
150
|
-
gt_version:
|
|
151
|
-
gte_version:
|
|
149
|
+
lt_version: str | None = None,
|
|
150
|
+
lte_version: str | None = None,
|
|
151
|
+
gt_version: str | None = None,
|
|
152
|
+
gte_version: str | None = None,
|
|
152
153
|
) -> None:
|
|
153
154
|
"""Check the version of a package.
|
|
154
155
|
|
|
155
156
|
Args:
|
|
156
|
-
package
|
|
157
|
-
lt_version
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
Defaults to None.
|
|
163
|
-
gte_version (str, optional): The version must be greater than or equal to this.
|
|
164
|
-
Defaults to None.
|
|
157
|
+
package: The name of the package.
|
|
158
|
+
lt_version: The version must be less than this.
|
|
159
|
+
lte_version: The version must be less than or equal to this.
|
|
160
|
+
gt_version: The version must be greater than this.
|
|
161
|
+
gte_version: The version must be greater than or equal to this.
|
|
162
|
+
|
|
165
163
|
|
|
166
164
|
Raises:
|
|
167
165
|
ValueError: If the package version does not meet the requirements.
|
|
@@ -200,7 +198,7 @@ def get_pydantic_field_names(pydantic_cls: Any) -> set[str]:
|
|
|
200
198
|
pydantic_cls: Pydantic class.
|
|
201
199
|
|
|
202
200
|
Returns:
|
|
203
|
-
|
|
201
|
+
Field names.
|
|
204
202
|
"""
|
|
205
203
|
all_required_field_names = set()
|
|
206
204
|
if is_pydantic_v1_subclass(pydantic_cls):
|
|
@@ -227,7 +225,7 @@ def _build_model_kwargs(
|
|
|
227
225
|
all_required_field_names: All required field names for the pydantic class.
|
|
228
226
|
|
|
229
227
|
Returns:
|
|
230
|
-
|
|
228
|
+
Extra kwargs.
|
|
231
229
|
|
|
232
230
|
Raises:
|
|
233
231
|
ValueError: If a field is specified in both values and extra_kwargs.
|
|
@@ -275,7 +273,7 @@ def build_extra_kwargs(
|
|
|
275
273
|
all_required_field_names: All required field names for the pydantic class.
|
|
276
274
|
|
|
277
275
|
Returns:
|
|
278
|
-
|
|
276
|
+
Extra kwargs.
|
|
279
277
|
|
|
280
278
|
Raises:
|
|
281
279
|
ValueError: If a field is specified in both values and extra_kwargs.
|
|
@@ -305,14 +303,14 @@ def build_extra_kwargs(
|
|
|
305
303
|
return extra_kwargs
|
|
306
304
|
|
|
307
305
|
|
|
308
|
-
def convert_to_secret_str(value:
|
|
306
|
+
def convert_to_secret_str(value: SecretStr | str) -> SecretStr:
|
|
309
307
|
"""Convert a string to a SecretStr if needed.
|
|
310
308
|
|
|
311
309
|
Args:
|
|
312
|
-
value
|
|
310
|
+
value: The value to convert.
|
|
313
311
|
|
|
314
312
|
Returns:
|
|
315
|
-
|
|
313
|
+
The SecretStr value.
|
|
316
314
|
"""
|
|
317
315
|
if isinstance(value, SecretStr):
|
|
318
316
|
return value
|
|
@@ -344,29 +342,29 @@ def from_env(key: str, /, *, error_message: str) -> Callable[[], str]: ...
|
|
|
344
342
|
|
|
345
343
|
@overload
|
|
346
344
|
def from_env(
|
|
347
|
-
key:
|
|
345
|
+
key: str | Sequence[str], /, *, default: str, error_message: str | None
|
|
348
346
|
) -> Callable[[], str]: ...
|
|
349
347
|
|
|
350
348
|
|
|
351
349
|
@overload
|
|
352
350
|
def from_env(
|
|
353
|
-
key: str, /, *, default: None, error_message:
|
|
354
|
-
) -> Callable[[],
|
|
351
|
+
key: str, /, *, default: None, error_message: str | None
|
|
352
|
+
) -> Callable[[], str | None]: ...
|
|
355
353
|
|
|
356
354
|
|
|
357
355
|
@overload
|
|
358
356
|
def from_env(
|
|
359
|
-
key:
|
|
360
|
-
) -> Callable[[],
|
|
357
|
+
key: str | Sequence[str], /, *, default: None
|
|
358
|
+
) -> Callable[[], str | None]: ...
|
|
361
359
|
|
|
362
360
|
|
|
363
361
|
def from_env(
|
|
364
|
-
key:
|
|
362
|
+
key: str | Sequence[str],
|
|
365
363
|
/,
|
|
366
364
|
*,
|
|
367
|
-
default:
|
|
368
|
-
error_message:
|
|
369
|
-
) ->
|
|
365
|
+
default: str | _NoDefaultType | None = _NoDefault,
|
|
366
|
+
error_message: str | None = None,
|
|
367
|
+
) -> Callable[[], str] | Callable[[], str | None]:
|
|
370
368
|
"""Create a factory method that gets a value from an environment variable.
|
|
371
369
|
|
|
372
370
|
Args:
|
|
@@ -383,7 +381,7 @@ def from_env(
|
|
|
383
381
|
factory method that will look up the value from the environment.
|
|
384
382
|
"""
|
|
385
383
|
|
|
386
|
-
def get_from_env_fn() ->
|
|
384
|
+
def get_from_env_fn() -> str | None:
|
|
387
385
|
"""Get a value from an environment variable.
|
|
388
386
|
|
|
389
387
|
Raises:
|
|
@@ -415,7 +413,7 @@ def from_env(
|
|
|
415
413
|
|
|
416
414
|
|
|
417
415
|
@overload
|
|
418
|
-
def secret_from_env(key:
|
|
416
|
+
def secret_from_env(key: str | Sequence[str], /) -> Callable[[], SecretStr]: ...
|
|
419
417
|
|
|
420
418
|
|
|
421
419
|
@overload
|
|
@@ -424,8 +422,8 @@ def secret_from_env(key: str, /, *, default: str) -> Callable[[], SecretStr]: ..
|
|
|
424
422
|
|
|
425
423
|
@overload
|
|
426
424
|
def secret_from_env(
|
|
427
|
-
key:
|
|
428
|
-
) -> Callable[[],
|
|
425
|
+
key: str | Sequence[str], /, *, default: None
|
|
426
|
+
) -> Callable[[], SecretStr | None]: ...
|
|
429
427
|
|
|
430
428
|
|
|
431
429
|
@overload
|
|
@@ -433,12 +431,12 @@ def secret_from_env(key: str, /, *, error_message: str) -> Callable[[], SecretSt
|
|
|
433
431
|
|
|
434
432
|
|
|
435
433
|
def secret_from_env(
|
|
436
|
-
key:
|
|
434
|
+
key: str | Sequence[str],
|
|
437
435
|
/,
|
|
438
436
|
*,
|
|
439
|
-
default:
|
|
440
|
-
error_message:
|
|
441
|
-
) ->
|
|
437
|
+
default: str | _NoDefaultType | None = _NoDefault,
|
|
438
|
+
error_message: str | None = None,
|
|
439
|
+
) -> Callable[[], SecretStr | None] | Callable[[], SecretStr]:
|
|
442
440
|
"""Secret from env.
|
|
443
441
|
|
|
444
442
|
Args:
|
|
@@ -452,7 +450,7 @@ def secret_from_env(
|
|
|
452
450
|
factory method that will look up the secret from the environment.
|
|
453
451
|
"""
|
|
454
452
|
|
|
455
|
-
def get_secret_from_env() ->
|
|
453
|
+
def get_secret_from_env() -> SecretStr | None:
|
|
456
454
|
"""Get a value from an environment variable.
|
|
457
455
|
|
|
458
456
|
Raises:
|
|
@@ -482,3 +480,31 @@ def secret_from_env(
|
|
|
482
480
|
raise ValueError(msg)
|
|
483
481
|
|
|
484
482
|
return get_secret_from_env
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
LC_AUTO_PREFIX = "lc_"
|
|
486
|
+
"""LangChain auto-generated ID prefix for messages and content blocks."""
|
|
487
|
+
|
|
488
|
+
LC_ID_PREFIX = "lc_run-"
|
|
489
|
+
"""Internal tracing/callback system identifier.
|
|
490
|
+
|
|
491
|
+
Used for:
|
|
492
|
+
- Tracing. Every LangChain operation (LLM call, chain execution, tool use, etc.)
|
|
493
|
+
gets a unique run_id (UUID)
|
|
494
|
+
- Enables tracking parent-child relationships between operations
|
|
495
|
+
"""
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def ensure_id(id_val: str | None) -> str:
|
|
499
|
+
"""Ensure the ID is a valid string, generating a new UUID if not provided.
|
|
500
|
+
|
|
501
|
+
Auto-generated UUIDs are prefixed by `'lc_'` to indicate they are
|
|
502
|
+
LangChain-generated IDs.
|
|
503
|
+
|
|
504
|
+
Args:
|
|
505
|
+
id_val: Optional string ID value to validate.
|
|
506
|
+
|
|
507
|
+
Returns:
|
|
508
|
+
A string ID, either the validated provided value or a newly generated UUID4.
|
|
509
|
+
"""
|
|
510
|
+
return id_val or f"{LC_AUTO_PREFIX}{uuid4()}"
|