_format_for_tracing() — langchain Function Reference
Architecture documentation for the _format_for_tracing() function in chat_models.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD f1b77769_1c98_a324_e709_fd921b433e56["_format_for_tracing()"] 20f4116a_d26d_2a5f_4a10_67af6940e081["chat_models.py"] f1b77769_1c98_a324_e709_fd921b433e56 -->|defined in| 20f4116a_d26d_2a5f_4a10_67af6940e081 7f210df0_4f6a_343c_b4fe_f942048ea776["stream()"] 7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| f1b77769_1c98_a324_e709_fd921b433e56 39e8cf9b_1f2e_9a93_e3f1_aaaffdfd70fa["astream()"] 39e8cf9b_1f2e_9a93_e3f1_aaaffdfd70fa -->|calls| f1b77769_1c98_a324_e709_fd921b433e56 fccabb0a_e230_0454_e79d_6a55d54eee3b["generate()"] fccabb0a_e230_0454_e79d_6a55d54eee3b -->|calls| f1b77769_1c98_a324_e709_fd921b433e56 e539ab1d_5151_8ba1_cfe0_47ef5adc1f67["agenerate()"] e539ab1d_5151_8ba1_cfe0_47ef5adc1f67 -->|calls| f1b77769_1c98_a324_e709_fd921b433e56 style f1b77769_1c98_a324_e709_fd921b433e56 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/language_models/chat_models.py lines 114–179
def _format_for_tracing(messages: list[BaseMessage]) -> list[BaseMessage]:
"""Format messages for tracing in `on_chat_model_start`.
- Update image content blocks to OpenAI Chat Completions format (backward
compatibility).
- Add `type` key to content blocks that have a single key.
Args:
messages: List of messages to format.
Returns:
List of messages formatted for tracing.
"""
messages_to_trace = []
for message in messages:
message_to_trace = message
if isinstance(message.content, list):
for idx, block in enumerate(message.content):
if isinstance(block, dict):
# Update image content blocks to OpenAI # Chat Completions format.
if (
block.get("type") == "image"
and is_data_content_block(block)
and not ("file_id" in block or block.get("source_type") == "id")
):
if message_to_trace is message:
# Shallow copy
message_to_trace = message.model_copy()
message_to_trace.content = list(message_to_trace.content)
message_to_trace.content[idx] = ( # type: ignore[index] # mypy confused by .model_copy
convert_to_openai_image_block(block)
)
elif (
block.get("type") == "file"
and is_data_content_block(block) # v0 (image/audio/file) or v1
and "base64" in block
# Backward compat: convert v1 base64 blocks to v0
):
if message_to_trace is message:
# Shallow copy
message_to_trace = message.model_copy()
message_to_trace.content = list(message_to_trace.content)
message_to_trace.content[idx] = { # type: ignore[index]
**{k: v for k, v in block.items() if k != "base64"},
"data": block["base64"],
"source_type": "base64",
}
elif len(block) == 1 and "type" not in block:
# Tracing assumes all content blocks have a "type" key. Here
# we add this key if it is missing, and there's an obvious
# choice for the type (e.g., a single key in the block).
if message_to_trace is message:
# Shallow copy
message_to_trace = message.model_copy()
message_to_trace.content = list(message_to_trace.content)
key = next(iter(block))
message_to_trace.content[idx] = { # type: ignore[index]
"type": key,
key: block[key],
}
messages_to_trace.append(message_to_trace)
return messages_to_trace
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does _format_for_tracing() do?
_format_for_tracing() is a function in the langchain codebase, defined in libs/core/langchain_core/language_models/chat_models.py.
Where is _format_for_tracing() defined?
_format_for_tracing() is defined in libs/core/langchain_core/language_models/chat_models.py at line 114.
What calls _format_for_tracing()?
_format_for_tracing() is called by 4 function(s): agenerate, astream, generate, stream.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free