langchain-core 1.0.0a2__py3-none-any.whl → 1.0.0a4__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/_api/beta_decorator.py +17 -40
- langchain_core/_api/deprecation.py +20 -7
- langchain_core/_api/path.py +19 -2
- langchain_core/_import_utils.py +7 -0
- langchain_core/agents.py +10 -6
- langchain_core/callbacks/base.py +28 -15
- langchain_core/callbacks/manager.py +81 -69
- langchain_core/callbacks/usage.py +4 -2
- langchain_core/chat_history.py +29 -21
- langchain_core/document_loaders/base.py +34 -9
- langchain_core/document_loaders/langsmith.py +3 -0
- langchain_core/documents/base.py +35 -10
- langchain_core/documents/transformers.py +4 -2
- langchain_core/embeddings/fake.py +8 -5
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/exceptions.py +7 -0
- langchain_core/globals.py +17 -28
- langchain_core/indexing/api.py +57 -45
- langchain_core/indexing/base.py +5 -8
- langchain_core/indexing/in_memory.py +23 -3
- langchain_core/language_models/__init__.py +6 -2
- langchain_core/language_models/_utils.py +27 -5
- langchain_core/language_models/base.py +33 -21
- langchain_core/language_models/chat_models.py +104 -31
- langchain_core/language_models/fake_chat_models.py +5 -7
- langchain_core/language_models/llms.py +54 -20
- langchain_core/load/dump.py +2 -3
- langchain_core/load/load.py +15 -1
- langchain_core/load/serializable.py +38 -43
- langchain_core/memory.py +7 -3
- langchain_core/messages/__init__.py +1 -1
- langchain_core/messages/ai.py +41 -34
- langchain_core/messages/base.py +20 -7
- langchain_core/messages/block_translators/__init__.py +10 -8
- langchain_core/messages/block_translators/anthropic.py +11 -7
- langchain_core/messages/block_translators/bedrock.py +76 -27
- langchain_core/messages/block_translators/bedrock_converse.py +259 -23
- langchain_core/messages/block_translators/google_genai.py +3 -1
- langchain_core/messages/block_translators/google_vertexai.py +3 -1
- langchain_core/messages/block_translators/groq.py +3 -1
- langchain_core/messages/block_translators/ollama.py +3 -1
- langchain_core/messages/block_translators/openai.py +50 -20
- langchain_core/messages/content.py +23 -13
- langchain_core/messages/human.py +2 -13
- langchain_core/messages/system.py +2 -6
- langchain_core/messages/tool.py +34 -14
- langchain_core/messages/utils.py +186 -73
- langchain_core/output_parsers/base.py +5 -2
- langchain_core/output_parsers/json.py +4 -4
- langchain_core/output_parsers/list.py +7 -22
- langchain_core/output_parsers/openai_functions.py +3 -0
- langchain_core/output_parsers/openai_tools.py +6 -1
- langchain_core/output_parsers/pydantic.py +4 -0
- langchain_core/output_parsers/string.py +5 -1
- langchain_core/output_parsers/xml.py +19 -19
- langchain_core/outputs/chat_generation.py +18 -7
- langchain_core/outputs/generation.py +14 -3
- langchain_core/outputs/llm_result.py +8 -1
- langchain_core/prompt_values.py +10 -4
- langchain_core/prompts/base.py +6 -11
- langchain_core/prompts/chat.py +88 -60
- langchain_core/prompts/dict.py +16 -8
- langchain_core/prompts/few_shot.py +9 -11
- langchain_core/prompts/few_shot_with_templates.py +5 -1
- langchain_core/prompts/image.py +12 -5
- langchain_core/prompts/loading.py +2 -2
- langchain_core/prompts/message.py +5 -6
- langchain_core/prompts/pipeline.py +13 -8
- langchain_core/prompts/prompt.py +22 -8
- langchain_core/prompts/string.py +18 -10
- langchain_core/prompts/structured.py +7 -2
- langchain_core/rate_limiters.py +2 -2
- langchain_core/retrievers.py +7 -6
- langchain_core/runnables/base.py +387 -246
- langchain_core/runnables/branch.py +11 -28
- langchain_core/runnables/config.py +20 -17
- langchain_core/runnables/configurable.py +34 -19
- langchain_core/runnables/fallbacks.py +20 -13
- langchain_core/runnables/graph.py +48 -38
- langchain_core/runnables/graph_ascii.py +40 -17
- langchain_core/runnables/graph_mermaid.py +54 -25
- langchain_core/runnables/graph_png.py +27 -31
- langchain_core/runnables/history.py +55 -58
- langchain_core/runnables/passthrough.py +44 -21
- langchain_core/runnables/retry.py +44 -23
- langchain_core/runnables/router.py +9 -8
- langchain_core/runnables/schema.py +9 -0
- langchain_core/runnables/utils.py +53 -90
- langchain_core/stores.py +19 -31
- langchain_core/sys_info.py +9 -8
- langchain_core/tools/base.py +36 -27
- langchain_core/tools/convert.py +25 -14
- langchain_core/tools/simple.py +36 -8
- langchain_core/tools/structured.py +25 -12
- langchain_core/tracers/base.py +2 -2
- langchain_core/tracers/context.py +5 -1
- langchain_core/tracers/core.py +110 -46
- langchain_core/tracers/evaluation.py +22 -26
- langchain_core/tracers/event_stream.py +97 -42
- langchain_core/tracers/langchain.py +12 -3
- langchain_core/tracers/langchain_v1.py +10 -2
- langchain_core/tracers/log_stream.py +56 -17
- langchain_core/tracers/root_listeners.py +4 -20
- langchain_core/tracers/run_collector.py +6 -16
- langchain_core/tracers/schemas.py +5 -1
- langchain_core/utils/aiter.py +14 -6
- langchain_core/utils/env.py +3 -0
- langchain_core/utils/function_calling.py +46 -20
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +12 -5
- langchain_core/utils/json.py +12 -3
- langchain_core/utils/json_schema.py +156 -40
- langchain_core/utils/loading.py +5 -1
- langchain_core/utils/mustache.py +25 -16
- langchain_core/utils/pydantic.py +38 -9
- langchain_core/utils/utils.py +25 -9
- langchain_core/vectorstores/base.py +7 -20
- langchain_core/vectorstores/in_memory.py +20 -14
- langchain_core/vectorstores/utils.py +18 -12
- langchain_core/version.py +1 -1
- langchain_core-1.0.0a4.dist-info/METADATA +77 -0
- langchain_core-1.0.0a4.dist-info/RECORD +181 -0
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -448
- langchain_core-1.0.0a2.dist-info/METADATA +0 -106
- langchain_core-1.0.0a2.dist-info/RECORD +0 -184
- {langchain_core-1.0.0a2.dist-info → langchain_core-1.0.0a4.dist-info}/WHEEL +0 -0
- {langchain_core-1.0.0a2.dist-info → langchain_core-1.0.0a4.dist-info}/entry_points.txt +0 -0
|
@@ -53,26 +53,28 @@ def _register_translators() -> None:
|
|
|
53
53
|
For translators implemented outside langchain-core, they can be registered by
|
|
54
54
|
calling ``register_translator`` from within the integration package.
|
|
55
55
|
"""
|
|
56
|
-
from langchain_core.messages.block_translators.anthropic import (
|
|
56
|
+
from langchain_core.messages.block_translators.anthropic import ( # noqa: PLC0415
|
|
57
57
|
_register_anthropic_translator,
|
|
58
58
|
)
|
|
59
|
-
from langchain_core.messages.block_translators.bedrock import (
|
|
59
|
+
from langchain_core.messages.block_translators.bedrock import ( # noqa: PLC0415
|
|
60
60
|
_register_bedrock_translator,
|
|
61
61
|
)
|
|
62
|
-
from langchain_core.messages.block_translators.bedrock_converse import (
|
|
62
|
+
from langchain_core.messages.block_translators.bedrock_converse import ( # noqa: PLC0415
|
|
63
63
|
_register_bedrock_converse_translator,
|
|
64
64
|
)
|
|
65
|
-
from langchain_core.messages.block_translators.google_genai import (
|
|
65
|
+
from langchain_core.messages.block_translators.google_genai import ( # noqa: PLC0415
|
|
66
66
|
_register_google_genai_translator,
|
|
67
67
|
)
|
|
68
|
-
from langchain_core.messages.block_translators.google_vertexai import (
|
|
68
|
+
from langchain_core.messages.block_translators.google_vertexai import ( # noqa: PLC0415
|
|
69
69
|
_register_google_vertexai_translator,
|
|
70
70
|
)
|
|
71
|
-
from langchain_core.messages.block_translators.groq import
|
|
72
|
-
|
|
71
|
+
from langchain_core.messages.block_translators.groq import ( # noqa: PLC0415
|
|
72
|
+
_register_groq_translator,
|
|
73
|
+
)
|
|
74
|
+
from langchain_core.messages.block_translators.ollama import ( # noqa: PLC0415
|
|
73
75
|
_register_ollama_translator,
|
|
74
76
|
)
|
|
75
|
-
from langchain_core.messages.block_translators.openai import (
|
|
77
|
+
from langchain_core.messages.block_translators.openai import ( # noqa: PLC0415
|
|
76
78
|
_register_openai_translator,
|
|
77
79
|
)
|
|
78
80
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
4
|
from collections.abc import Iterable
|
|
5
|
-
from typing import Any, Optional, cast
|
|
5
|
+
from typing import Any, Optional, Union, cast
|
|
6
6
|
|
|
7
7
|
from langchain_core.messages import AIMessage, AIMessageChunk
|
|
8
8
|
from langchain_core.messages import content as types
|
|
@@ -17,7 +17,7 @@ def _populate_extras(
|
|
|
17
17
|
|
|
18
18
|
for key, value in block.items():
|
|
19
19
|
if key not in known_fields:
|
|
20
|
-
if "extras" not in
|
|
20
|
+
if "extras" not in standard_block:
|
|
21
21
|
# Below type-ignores are because mypy thinks a non-standard block can
|
|
22
22
|
# get here, although we exclude them above.
|
|
23
23
|
standard_block["extras"] = {} # type: ignore[typeddict-unknown-key]
|
|
@@ -186,10 +186,12 @@ def _convert_citation_to_v1(citation: dict[str, Any]) -> types.Annotation:
|
|
|
186
186
|
def _convert_to_v1_from_anthropic(message: AIMessage) -> list[types.ContentBlock]:
|
|
187
187
|
"""Convert Anthropic message content to v1 format."""
|
|
188
188
|
if isinstance(message.content, str):
|
|
189
|
-
|
|
189
|
+
content: list[Union[str, dict]] = [{"type": "text", "text": message.content}]
|
|
190
|
+
else:
|
|
191
|
+
content = message.content
|
|
190
192
|
|
|
191
193
|
def _iter_blocks() -> Iterable[types.ContentBlock]:
|
|
192
|
-
for block in
|
|
194
|
+
for block in content:
|
|
193
195
|
if not isinstance(block, dict):
|
|
194
196
|
continue
|
|
195
197
|
block_type = block.get("type")
|
|
@@ -429,12 +431,12 @@ def _convert_to_v1_from_anthropic(message: AIMessage) -> list[types.ContentBlock
|
|
|
429
431
|
|
|
430
432
|
|
|
431
433
|
def translate_content(message: AIMessage) -> list[types.ContentBlock]:
|
|
432
|
-
"""Derive standard content blocks from a message with
|
|
434
|
+
"""Derive standard content blocks from a message with Anthropic content."""
|
|
433
435
|
return _convert_to_v1_from_anthropic(message)
|
|
434
436
|
|
|
435
437
|
|
|
436
438
|
def translate_content_chunk(message: AIMessageChunk) -> list[types.ContentBlock]:
|
|
437
|
-
"""Derive standard content blocks from a message chunk with
|
|
439
|
+
"""Derive standard content blocks from a message chunk with Anthropic content."""
|
|
438
440
|
return _convert_to_v1_from_anthropic(message)
|
|
439
441
|
|
|
440
442
|
|
|
@@ -443,7 +445,9 @@ def _register_anthropic_translator() -> None:
|
|
|
443
445
|
|
|
444
446
|
Run automatically when the module is imported.
|
|
445
447
|
"""
|
|
446
|
-
from langchain_core.messages.block_translators import
|
|
448
|
+
from langchain_core.messages.block_translators import ( # noqa: PLC0415
|
|
449
|
+
register_translator,
|
|
450
|
+
)
|
|
447
451
|
|
|
448
452
|
register_translator("anthropic", translate_content, translate_content_chunk)
|
|
449
453
|
|
|
@@ -1,43 +1,92 @@
|
|
|
1
|
-
"""Derivations of standard content blocks from
|
|
2
|
-
|
|
3
|
-
import warnings
|
|
1
|
+
"""Derivations of standard content blocks from Bedrock content."""
|
|
4
2
|
|
|
5
3
|
from langchain_core.messages import AIMessage, AIMessageChunk
|
|
6
4
|
from langchain_core.messages import content as types
|
|
5
|
+
from langchain_core.messages.block_translators.anthropic import (
|
|
6
|
+
_convert_to_v1_from_anthropic,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _convert_to_v1_from_bedrock(message: AIMessage) -> list[types.ContentBlock]:
|
|
11
|
+
"""Convert bedrock message content to v1 format."""
|
|
12
|
+
out = _convert_to_v1_from_anthropic(message)
|
|
13
|
+
|
|
14
|
+
content_tool_call_ids = {
|
|
15
|
+
block.get("id")
|
|
16
|
+
for block in out
|
|
17
|
+
if isinstance(block, dict) and block.get("type") == "tool_call"
|
|
18
|
+
}
|
|
19
|
+
for tool_call in message.tool_calls:
|
|
20
|
+
if (id_ := tool_call.get("id")) and id_ not in content_tool_call_ids:
|
|
21
|
+
tool_call_block: types.ToolCall = {
|
|
22
|
+
"type": "tool_call",
|
|
23
|
+
"id": id_,
|
|
24
|
+
"name": tool_call["name"],
|
|
25
|
+
"args": tool_call["args"],
|
|
26
|
+
}
|
|
27
|
+
if "index" in tool_call:
|
|
28
|
+
tool_call_block["index"] = tool_call["index"] # type: ignore[typeddict-item]
|
|
29
|
+
if "extras" in tool_call:
|
|
30
|
+
tool_call_block["extras"] = tool_call["extras"] # type: ignore[typeddict-item]
|
|
31
|
+
out.append(tool_call_block)
|
|
32
|
+
return out
|
|
33
|
+
|
|
7
34
|
|
|
8
|
-
|
|
35
|
+
def _convert_to_v1_from_bedrock_chunk(
|
|
36
|
+
message: AIMessageChunk,
|
|
37
|
+
) -> list[types.ContentBlock]:
|
|
38
|
+
"""Convert bedrock message chunk content to v1 format."""
|
|
39
|
+
if (
|
|
40
|
+
message.content == ""
|
|
41
|
+
and not message.additional_kwargs
|
|
42
|
+
and not message.tool_calls
|
|
43
|
+
):
|
|
44
|
+
# Bedrock outputs multiple chunks containing response metadata
|
|
45
|
+
return []
|
|
9
46
|
|
|
47
|
+
out = _convert_to_v1_from_anthropic(message)
|
|
10
48
|
|
|
11
|
-
|
|
49
|
+
if (
|
|
50
|
+
message.tool_call_chunks
|
|
51
|
+
and not message.content
|
|
52
|
+
and message.chunk_position != "last" # keep tool_calls if aggregated
|
|
53
|
+
):
|
|
54
|
+
for tool_call_chunk in message.tool_call_chunks:
|
|
55
|
+
tc: types.ToolCallChunk = {
|
|
56
|
+
"type": "tool_call_chunk",
|
|
57
|
+
"id": tool_call_chunk.get("id"),
|
|
58
|
+
"name": tool_call_chunk.get("name"),
|
|
59
|
+
"args": tool_call_chunk.get("args"),
|
|
60
|
+
}
|
|
61
|
+
if (idx := tool_call_chunk.get("index")) is not None:
|
|
62
|
+
tc["index"] = idx
|
|
63
|
+
out.append(tc)
|
|
64
|
+
return out
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def translate_content(message: AIMessage) -> list[types.ContentBlock]:
|
|
12
68
|
"""Derive standard content blocks from a message with Bedrock content."""
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"""Derive standard content blocks from a chunk with Bedrock content."""
|
|
25
|
-
global WARNED # noqa: PLW0603
|
|
26
|
-
if not WARNED:
|
|
27
|
-
warning_message = (
|
|
28
|
-
"Content block standardization is not yet fully supported for Bedrock."
|
|
29
|
-
)
|
|
30
|
-
warnings.warn(warning_message, stacklevel=2)
|
|
31
|
-
WARNED = True
|
|
32
|
-
raise NotImplementedError
|
|
69
|
+
if "claude" not in message.response_metadata.get("model_name", "").lower():
|
|
70
|
+
raise NotImplementedError # fall back to best-effort parsing
|
|
71
|
+
return _convert_to_v1_from_bedrock(message)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def translate_content_chunk(message: AIMessageChunk) -> list[types.ContentBlock]:
|
|
75
|
+
"""Derive standard content blocks from a message chunk with Bedrock content."""
|
|
76
|
+
# TODO: add model_name to all Bedrock chunks and update core merging logic
|
|
77
|
+
# to not append during aggregation. Then raise NotImplementedError here if
|
|
78
|
+
# not an Anthropic model to fall back to best-effort parsing.
|
|
79
|
+
return _convert_to_v1_from_bedrock_chunk(message)
|
|
33
80
|
|
|
34
81
|
|
|
35
82
|
def _register_bedrock_translator() -> None:
|
|
36
|
-
"""Register the
|
|
83
|
+
"""Register the bedrock translator with the central registry.
|
|
37
84
|
|
|
38
85
|
Run automatically when the module is imported.
|
|
39
86
|
"""
|
|
40
|
-
from langchain_core.messages.block_translators import
|
|
87
|
+
from langchain_core.messages.block_translators import ( # noqa: PLC0415
|
|
88
|
+
register_translator,
|
|
89
|
+
)
|
|
41
90
|
|
|
42
91
|
register_translator("bedrock", translate_content, translate_content_chunk)
|
|
43
92
|
|
|
@@ -1,37 +1,271 @@
|
|
|
1
1
|
"""Derivations of standard content blocks from Amazon (Bedrock Converse) content."""
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import base64
|
|
4
|
+
from collections.abc import Iterable
|
|
5
|
+
from typing import Any, Optional, cast
|
|
4
6
|
|
|
5
7
|
from langchain_core.messages import AIMessage, AIMessageChunk
|
|
6
8
|
from langchain_core.messages import content as types
|
|
7
9
|
|
|
8
|
-
WARNED = False
|
|
9
10
|
|
|
11
|
+
def _bytes_to_b64_str(bytes_: bytes) -> str:
|
|
12
|
+
return base64.b64encode(bytes_).decode("utf-8")
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
|
|
15
|
+
def _populate_extras(
|
|
16
|
+
standard_block: types.ContentBlock, block: dict[str, Any], known_fields: set[str]
|
|
17
|
+
) -> types.ContentBlock:
|
|
18
|
+
"""Mutate a block, populating extras."""
|
|
19
|
+
if standard_block.get("type") == "non_standard":
|
|
20
|
+
return standard_block
|
|
21
|
+
|
|
22
|
+
for key, value in block.items():
|
|
23
|
+
if key not in known_fields:
|
|
24
|
+
if "extras" not in standard_block:
|
|
25
|
+
# Below type-ignores are because mypy thinks a non-standard block can
|
|
26
|
+
# get here, although we exclude them above.
|
|
27
|
+
standard_block["extras"] = {} # type: ignore[typeddict-unknown-key]
|
|
28
|
+
standard_block["extras"][key] = value # type: ignore[typeddict-item]
|
|
29
|
+
|
|
30
|
+
return standard_block
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _convert_to_v1_from_converse_input(
|
|
34
|
+
content: list[types.ContentBlock],
|
|
35
|
+
) -> list[types.ContentBlock]:
|
|
36
|
+
"""Attempt to unpack non-standard blocks."""
|
|
37
|
+
|
|
38
|
+
def _iter_blocks() -> Iterable[types.ContentBlock]:
|
|
39
|
+
blocks: list[dict[str, Any]] = [
|
|
40
|
+
cast("dict[str, Any]", block)
|
|
41
|
+
if block.get("type") != "non_standard"
|
|
42
|
+
else block["value"] # type: ignore[typeddict-item] # this is only non-standard blocks
|
|
43
|
+
for block in content
|
|
44
|
+
]
|
|
45
|
+
for block in blocks:
|
|
46
|
+
num_keys = len(block)
|
|
47
|
+
|
|
48
|
+
if num_keys == 1 and (text := block.get("text")):
|
|
49
|
+
yield {"type": "text", "text": text}
|
|
50
|
+
|
|
51
|
+
elif (
|
|
52
|
+
num_keys == 1
|
|
53
|
+
and (document := block.get("document"))
|
|
54
|
+
and isinstance(document, dict)
|
|
55
|
+
and "format" in document
|
|
56
|
+
):
|
|
57
|
+
if document.get("format") == "pdf":
|
|
58
|
+
if "bytes" in document.get("source", {}):
|
|
59
|
+
file_block: types.FileContentBlock = {
|
|
60
|
+
"type": "file",
|
|
61
|
+
"base64": _bytes_to_b64_str(document["source"]["bytes"]),
|
|
62
|
+
"mime_type": "application/pdf",
|
|
63
|
+
}
|
|
64
|
+
_populate_extras(file_block, document, {"format", "source"})
|
|
65
|
+
yield file_block
|
|
66
|
+
|
|
67
|
+
else:
|
|
68
|
+
yield {"type": "non_standard", "value": block}
|
|
69
|
+
|
|
70
|
+
elif document["format"] == "txt":
|
|
71
|
+
if "text" in document.get("source", {}):
|
|
72
|
+
plain_text_block: types.PlainTextContentBlock = {
|
|
73
|
+
"type": "text-plain",
|
|
74
|
+
"text": document["source"]["text"],
|
|
75
|
+
"mime_type": "text/plain",
|
|
76
|
+
}
|
|
77
|
+
_populate_extras(
|
|
78
|
+
plain_text_block, document, {"format", "source"}
|
|
79
|
+
)
|
|
80
|
+
yield plain_text_block
|
|
81
|
+
else:
|
|
82
|
+
yield {"type": "non_standard", "value": block}
|
|
83
|
+
|
|
84
|
+
else:
|
|
85
|
+
yield {"type": "non_standard", "value": block}
|
|
86
|
+
|
|
87
|
+
elif (
|
|
88
|
+
num_keys == 1
|
|
89
|
+
and (image := block.get("image"))
|
|
90
|
+
and isinstance(image, dict)
|
|
91
|
+
and "format" in image
|
|
92
|
+
):
|
|
93
|
+
if "bytes" in image.get("source", {}):
|
|
94
|
+
image_block: types.ImageContentBlock = {
|
|
95
|
+
"type": "image",
|
|
96
|
+
"base64": _bytes_to_b64_str(image["source"]["bytes"]),
|
|
97
|
+
"mime_type": f"image/{image['format']}",
|
|
98
|
+
}
|
|
99
|
+
_populate_extras(image_block, image, {"format", "source"})
|
|
100
|
+
yield image_block
|
|
101
|
+
|
|
102
|
+
else:
|
|
103
|
+
yield {"type": "non_standard", "value": block}
|
|
104
|
+
|
|
105
|
+
elif block.get("type") in types.KNOWN_BLOCK_TYPES:
|
|
106
|
+
yield cast("types.ContentBlock", block)
|
|
107
|
+
|
|
108
|
+
else:
|
|
109
|
+
yield {"type": "non_standard", "value": block}
|
|
110
|
+
|
|
111
|
+
return list(_iter_blocks())
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _convert_citation_to_v1(citation: dict[str, Any]) -> types.Annotation:
|
|
115
|
+
standard_citation: types.Citation = {"type": "citation"}
|
|
116
|
+
if "title" in citation:
|
|
117
|
+
standard_citation["title"] = citation["title"]
|
|
118
|
+
if (
|
|
119
|
+
(source_content := citation.get("source_content"))
|
|
120
|
+
and isinstance(source_content, list)
|
|
121
|
+
and all(isinstance(item, dict) for item in source_content)
|
|
122
|
+
):
|
|
123
|
+
standard_citation["cited_text"] = "".join(
|
|
124
|
+
item.get("text", "") for item in source_content
|
|
18
125
|
)
|
|
19
|
-
warnings.warn(warning_message, stacklevel=2)
|
|
20
|
-
WARNED = True
|
|
21
|
-
raise NotImplementedError
|
|
22
126
|
|
|
127
|
+
known_fields = {"type", "source_content", "title", "index", "extras"}
|
|
23
128
|
|
|
24
|
-
|
|
129
|
+
for key, value in citation.items():
|
|
130
|
+
if key not in known_fields:
|
|
131
|
+
if "extras" not in standard_citation:
|
|
132
|
+
standard_citation["extras"] = {}
|
|
133
|
+
standard_citation["extras"][key] = value
|
|
134
|
+
|
|
135
|
+
return standard_citation
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _convert_to_v1_from_converse(message: AIMessage) -> list[types.ContentBlock]:
|
|
139
|
+
"""Convert Bedrock Converse message content to v1 format."""
|
|
140
|
+
if (
|
|
141
|
+
message.content == ""
|
|
142
|
+
and not message.additional_kwargs
|
|
143
|
+
and not message.tool_calls
|
|
144
|
+
):
|
|
145
|
+
# Converse outputs multiple chunks containing response metadata
|
|
146
|
+
return []
|
|
147
|
+
|
|
148
|
+
if isinstance(message.content, str):
|
|
149
|
+
message.content = [{"type": "text", "text": message.content}]
|
|
150
|
+
|
|
151
|
+
def _iter_blocks() -> Iterable[types.ContentBlock]:
|
|
152
|
+
for block in message.content:
|
|
153
|
+
if not isinstance(block, dict):
|
|
154
|
+
continue
|
|
155
|
+
block_type = block.get("type")
|
|
156
|
+
|
|
157
|
+
if block_type == "text":
|
|
158
|
+
if citations := block.get("citations"):
|
|
159
|
+
text_block: types.TextContentBlock = {
|
|
160
|
+
"type": "text",
|
|
161
|
+
"text": block.get("text", ""),
|
|
162
|
+
"annotations": [_convert_citation_to_v1(a) for a in citations],
|
|
163
|
+
}
|
|
164
|
+
else:
|
|
165
|
+
text_block = {"type": "text", "text": block["text"]}
|
|
166
|
+
if "index" in block:
|
|
167
|
+
text_block["index"] = block["index"]
|
|
168
|
+
yield text_block
|
|
169
|
+
|
|
170
|
+
elif block_type == "reasoning_content":
|
|
171
|
+
reasoning_block: types.ReasoningContentBlock = {"type": "reasoning"}
|
|
172
|
+
if reasoning_content := block.get("reasoning_content"):
|
|
173
|
+
if reasoning := reasoning_content.get("text"):
|
|
174
|
+
reasoning_block["reasoning"] = reasoning
|
|
175
|
+
if signature := reasoning_content.get("signature"):
|
|
176
|
+
if "extras" not in reasoning_block:
|
|
177
|
+
reasoning_block["extras"] = {}
|
|
178
|
+
reasoning_block["extras"]["signature"] = signature
|
|
179
|
+
|
|
180
|
+
if "index" in block:
|
|
181
|
+
reasoning_block["index"] = block["index"]
|
|
182
|
+
|
|
183
|
+
known_fields = {"type", "reasoning_content", "index", "extras"}
|
|
184
|
+
for key in block:
|
|
185
|
+
if key not in known_fields:
|
|
186
|
+
if "extras" not in reasoning_block:
|
|
187
|
+
reasoning_block["extras"] = {}
|
|
188
|
+
reasoning_block["extras"][key] = block[key]
|
|
189
|
+
yield reasoning_block
|
|
190
|
+
|
|
191
|
+
elif block_type == "tool_use":
|
|
192
|
+
if (
|
|
193
|
+
isinstance(message, AIMessageChunk)
|
|
194
|
+
and len(message.tool_call_chunks) == 1
|
|
195
|
+
and message.chunk_position != "last"
|
|
196
|
+
):
|
|
197
|
+
# Isolated chunk
|
|
198
|
+
tool_call_chunk: types.ToolCallChunk = (
|
|
199
|
+
message.tool_call_chunks[0].copy() # type: ignore[assignment]
|
|
200
|
+
)
|
|
201
|
+
if "type" not in tool_call_chunk:
|
|
202
|
+
tool_call_chunk["type"] = "tool_call_chunk"
|
|
203
|
+
yield tool_call_chunk
|
|
204
|
+
else:
|
|
205
|
+
tool_call_block: Optional[types.ToolCall] = None
|
|
206
|
+
# Non-streaming or gathered chunk
|
|
207
|
+
if len(message.tool_calls) == 1:
|
|
208
|
+
tool_call_block = {
|
|
209
|
+
"type": "tool_call",
|
|
210
|
+
"name": message.tool_calls[0]["name"],
|
|
211
|
+
"args": message.tool_calls[0]["args"],
|
|
212
|
+
"id": message.tool_calls[0].get("id"),
|
|
213
|
+
}
|
|
214
|
+
elif call_id := block.get("id"):
|
|
215
|
+
for tc in message.tool_calls:
|
|
216
|
+
if tc.get("id") == call_id:
|
|
217
|
+
tool_call_block = {
|
|
218
|
+
"type": "tool_call",
|
|
219
|
+
"name": tc["name"],
|
|
220
|
+
"args": tc["args"],
|
|
221
|
+
"id": tc.get("id"),
|
|
222
|
+
}
|
|
223
|
+
break
|
|
224
|
+
else:
|
|
225
|
+
pass
|
|
226
|
+
if not tool_call_block:
|
|
227
|
+
tool_call_block = {
|
|
228
|
+
"type": "tool_call",
|
|
229
|
+
"name": block.get("name", ""),
|
|
230
|
+
"args": block.get("input", {}),
|
|
231
|
+
"id": block.get("id", ""),
|
|
232
|
+
}
|
|
233
|
+
if "index" in block:
|
|
234
|
+
tool_call_block["index"] = block["index"]
|
|
235
|
+
yield tool_call_block
|
|
236
|
+
|
|
237
|
+
elif (
|
|
238
|
+
block_type == "input_json_delta"
|
|
239
|
+
and isinstance(message, AIMessageChunk)
|
|
240
|
+
and len(message.tool_call_chunks) == 1
|
|
241
|
+
):
|
|
242
|
+
tool_call_chunk = (
|
|
243
|
+
message.tool_call_chunks[0].copy() # type: ignore[assignment]
|
|
244
|
+
)
|
|
245
|
+
if "type" not in tool_call_chunk:
|
|
246
|
+
tool_call_chunk["type"] = "tool_call_chunk"
|
|
247
|
+
yield tool_call_chunk
|
|
248
|
+
|
|
249
|
+
else:
|
|
250
|
+
new_block: types.NonStandardContentBlock = {
|
|
251
|
+
"type": "non_standard",
|
|
252
|
+
"value": block,
|
|
253
|
+
}
|
|
254
|
+
if "index" in new_block["value"]:
|
|
255
|
+
new_block["index"] = new_block["value"].pop("index")
|
|
256
|
+
yield new_block
|
|
257
|
+
|
|
258
|
+
return list(_iter_blocks())
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def translate_content(message: AIMessage) -> list[types.ContentBlock]:
|
|
262
|
+
"""Derive standard content blocks from a message with Bedrock Converse content."""
|
|
263
|
+
return _convert_to_v1_from_converse(message)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def translate_content_chunk(message: AIMessageChunk) -> list[types.ContentBlock]:
|
|
25
267
|
"""Derive standard content blocks from a chunk with Bedrock Converse content."""
|
|
26
|
-
|
|
27
|
-
if not WARNED:
|
|
28
|
-
warning_message = (
|
|
29
|
-
"Content block standardization is not yet fully supported for Bedrock "
|
|
30
|
-
"Converse."
|
|
31
|
-
)
|
|
32
|
-
warnings.warn(warning_message, stacklevel=2)
|
|
33
|
-
WARNED = True
|
|
34
|
-
raise NotImplementedError
|
|
268
|
+
return _convert_to_v1_from_converse(message)
|
|
35
269
|
|
|
36
270
|
|
|
37
271
|
def _register_bedrock_converse_translator() -> None:
|
|
@@ -39,7 +273,9 @@ def _register_bedrock_converse_translator() -> None:
|
|
|
39
273
|
|
|
40
274
|
Run automatically when the module is imported.
|
|
41
275
|
"""
|
|
42
|
-
from langchain_core.messages.block_translators import
|
|
276
|
+
from langchain_core.messages.block_translators import ( # noqa: PLC0415
|
|
277
|
+
register_translator,
|
|
278
|
+
)
|
|
43
279
|
|
|
44
280
|
register_translator("bedrock_converse", translate_content, translate_content_chunk)
|
|
45
281
|
|
|
@@ -37,7 +37,9 @@ def _register_google_genai_translator() -> None:
|
|
|
37
37
|
|
|
38
38
|
Run automatically when the module is imported.
|
|
39
39
|
"""
|
|
40
|
-
from langchain_core.messages.block_translators import
|
|
40
|
+
from langchain_core.messages.block_translators import ( # noqa: PLC0415
|
|
41
|
+
register_translator,
|
|
42
|
+
)
|
|
41
43
|
|
|
42
44
|
register_translator("google_genai", translate_content, translate_content_chunk)
|
|
43
45
|
|
|
@@ -39,7 +39,9 @@ def _register_google_vertexai_translator() -> None:
|
|
|
39
39
|
|
|
40
40
|
Run automatically when the module is imported.
|
|
41
41
|
"""
|
|
42
|
-
from langchain_core.messages.block_translators import
|
|
42
|
+
from langchain_core.messages.block_translators import ( # noqa: PLC0415
|
|
43
|
+
register_translator,
|
|
44
|
+
)
|
|
43
45
|
|
|
44
46
|
register_translator("google_vertexai", translate_content, translate_content_chunk)
|
|
45
47
|
|
|
@@ -37,7 +37,9 @@ def _register_groq_translator() -> None:
|
|
|
37
37
|
|
|
38
38
|
Run automatically when the module is imported.
|
|
39
39
|
"""
|
|
40
|
-
from langchain_core.messages.block_translators import
|
|
40
|
+
from langchain_core.messages.block_translators import ( # noqa: PLC0415
|
|
41
|
+
register_translator,
|
|
42
|
+
)
|
|
41
43
|
|
|
42
44
|
register_translator("groq", translate_content, translate_content_chunk)
|
|
43
45
|
|
|
@@ -37,7 +37,9 @@ def _register_ollama_translator() -> None:
|
|
|
37
37
|
|
|
38
38
|
Run automatically when the module is imported.
|
|
39
39
|
"""
|
|
40
|
-
from langchain_core.messages.block_translators import
|
|
40
|
+
from langchain_core.messages.block_translators import ( # noqa: PLC0415
|
|
41
|
+
register_translator,
|
|
42
|
+
)
|
|
41
43
|
|
|
42
44
|
register_translator("ollama", translate_content, translate_content_chunk)
|
|
43
45
|
|