_make_message_chunk_from_anthropic_event() — langchain Function Reference
Architecture documentation for the _make_message_chunk_from_anthropic_event() function in chat_models.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 01106d9b_3ac9_a2a5_056c_a55bc89d961b["_make_message_chunk_from_anthropic_event()"] a85819c7_917d_4c71_2864_a19e68947340["chat_models.py"] 01106d9b_3ac9_a2a5_056c_a55bc89d961b -->|defined in| a85819c7_917d_4c71_2864_a19e68947340 d6746b81_ab7c_4b02_8921_f885e52e303e["_stream()"] d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| 01106d9b_3ac9_a2a5_056c_a55bc89d961b 8028f890_4975_51cc_2409_45db9b39971a["_astream()"] 8028f890_4975_51cc_2409_45db9b39971a -->|calls| 01106d9b_3ac9_a2a5_056c_a55bc89d961b e8fbda3b_7aa9_1575_74a6_35146039904f["_create_usage_metadata()"] 01106d9b_3ac9_a2a5_056c_a55bc89d961b -->|calls| e8fbda3b_7aa9_1575_74a6_35146039904f style 01106d9b_3ac9_a2a5_056c_a55bc89d961b fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/anthropic/langchain_anthropic/chat_models.py lines 1927–2102
def _make_message_chunk_from_anthropic_event(
event: anthropic.types.RawMessageStreamEvent,
*,
stream_usage: bool = True,
coerce_content_to_string: bool,
block_start_event: anthropic.types.RawMessageStreamEvent | None = None,
) -> tuple[AIMessageChunk | None, anthropic.types.RawMessageStreamEvent | None]:
"""Convert Anthropic streaming event to `AIMessageChunk`.
Args:
event: Raw streaming event from Anthropic SDK
stream_usage: Whether to include usage metadata in the output chunks.
coerce_content_to_string: Whether to convert structured content to plain
text strings.
When `True`, only text content is preserved; when `False`, structured
content like tool calls and citations are maintained.
block_start_event: Previous content block start event, used for tracking
tool use blocks and maintaining context across related events.
Returns:
Tuple with
- `AIMessageChunk`: Converted message chunk with appropriate content and
metadata, or `None` if the event doesn't produce a chunk
- `RawMessageStreamEvent`: Updated `block_start_event` for tracking content
blocks across sequential events, or `None` if not applicable
Note:
Not all Anthropic events result in message chunks. Events like internal
state changes return `None` for the message chunk while potentially
updating the `block_start_event` for context tracking.
"""
message_chunk: AIMessageChunk | None = None
# Reference: Anthropic SDK streaming implementation
# https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/lib/streaming/_messages.py # noqa: E501
if event.type == "message_start" and stream_usage:
# Capture model name, but don't include usage_metadata yet
# as it will be properly reported in message_delta with complete info
if hasattr(event.message, "model"):
response_metadata: dict[str, Any] = {"model_name": event.message.model}
else:
response_metadata = {}
message_chunk = AIMessageChunk(
content="" if coerce_content_to_string else [],
response_metadata=response_metadata,
)
elif (
event.type == "content_block_start"
and event.content_block is not None
and (
"tool_result" in event.content_block.type
or "tool_use" in event.content_block.type
or "document" in event.content_block.type
or "redacted_thinking" in event.content_block.type
)
):
if coerce_content_to_string:
warnings.warn("Received unexpected tool content block.", stacklevel=2)
content_block = event.content_block.model_dump()
if "caller" in content_block and content_block["caller"] is None:
content_block.pop("caller")
content_block["index"] = event.index
if event.content_block.type == "tool_use":
if (
parsed_args := getattr(event.content_block, "input", None)
) and isinstance(parsed_args, dict):
# In some cases parsed args are represented in start event, with no
# following input_json_delta events
args = json.dumps(parsed_args)
else:
args = ""
tool_call_chunk = create_tool_call_chunk(
index=event.index,
id=event.content_block.id,
name=event.content_block.name,
args=args,
)
tool_call_chunks = [tool_call_chunk]
Domain
Subdomains
Calls
Called By
Source
Frequently Asked Questions
What does _make_message_chunk_from_anthropic_event() do?
_make_message_chunk_from_anthropic_event() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is _make_message_chunk_from_anthropic_event() defined?
_make_message_chunk_from_anthropic_event() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 1927.
What does _make_message_chunk_from_anthropic_event() call?
_make_message_chunk_from_anthropic_event() calls 1 function(s): _create_usage_metadata.
What calls _make_message_chunk_from_anthropic_event()?
_make_message_chunk_from_anthropic_event() is called by 2 function(s): _astream, _stream.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free