Skip to content

Message Processing

Overview

Main features include:

  • Merge reasoning content into final responses
  • Merge streaming output Chunks

Merge Reasoning Content into Final Response

Used to merge reasoning content (reasoning_content) into the final response (content).

Function Description

Function Description
convert_reasoning_content_for_ai_message Merge reasoning content in AIMessage into the content field (for model's invoke and ainvoke)
convert_reasoning_content_for_chunk_iterator Merge reasoning content in streaming responses into the content field (for model's stream)
aconvert_reasoning_content_for_chunk_iterator Async version of convert_reasoning_content_for_chunk_iterator, for async streaming processing (for model's astream)

Code Example

from langchain_dev_utils.message_convert import (
    convert_reasoning_content_for_ai_message,
    convert_reasoning_content_for_chunk_iterator,
)

response = model.invoke("你好")
converted_response = convert_reasoning_content_for_ai_message(
    response, think_tag=("<start>", "<end>")
)
print(converted_response.content)

for chunk in convert_reasoning_content_for_chunk_iterator(
    model.stream("你好"), think_tag=("<start>", "<end>")
):
    print(chunk.content, end="", flush=True)

Merge Streaming Output Chunks

Provides utility functions to merge multiple AIMessageChunks generated due to streaming output into a single AIMessage.

Core Functions

Function Description
merge_ai_message_chunk Merge AI message chunks

Code Example

from langchain_dev_utils.message_convert import merge_ai_message_chunk

chunks = []
for chunk in model.stream("你好"):
    chunks.append(chunk)

merged_message = merge_ai_message_chunk(chunks)
print(merged_message)