feat: update message ID logic

This commit is contained in:
Mason Daugherty 2025-08-06 11:10:58 -04:00
parent 990deaedaf
commit 9e92aef933
No known key found for this signature in database
2 changed files with 18 additions and 4 deletions

View File

@ -25,8 +25,10 @@ from langchain_core.utils.usage import _dict_int_op
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
_LC_AUTO_PREFIX = "lc_"
"""LangChain auto-generated ID prefix for messages and content blocks."""
_LC_ID_PREFIX = "run-" _LC_ID_PREFIX = f"{_LC_AUTO_PREFIX}run-"
"""Internal tracing/callback system identifier. """Internal tracing/callback system identifier.
Used for: Used for:
@ -35,9 +37,6 @@ Used for:
- Enables tracking parent-child relationships between operations - Enables tracking parent-child relationships between operations
""" """
_LC_AUTO_PREFIX = "lc_"
"""LangChain auto-generated ID prefix for messages and content blocks."""
class InputTokenDetails(TypedDict, total=False): class InputTokenDetails(TypedDict, total=False):
"""Breakdown of input token counts. """Breakdown of input token counts.

View File

@ -48,6 +48,7 @@ from langchain_core.messages import (
get_buffer_string, get_buffer_string,
is_data_content_block, is_data_content_block,
) )
from langchain_core.messages.ai import _LC_ID_PREFIX
from langchain_core.messages.utils import ( from langchain_core.messages.utils import (
convert_from_v1_message, convert_from_v1_message,
convert_to_messages_v1, convert_to_messages_v1,
@ -471,6 +472,9 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC):
run_manager.on_llm_error(e) run_manager.on_llm_error(e)
raise raise
if run_manager and full_message.id and full_message.id.startswith("lc_"):
full_message.id = f"{_LC_ID_PREFIX}-{run_manager.run_id}-0"
run_manager.on_llm_end(full_message) run_manager.on_llm_end(full_message)
return full_message return full_message
@ -542,6 +546,9 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC):
) )
raise raise
if run_manager and full_message.id and full_message.id.startswith("lc_"):
full_message.id = f"{_LC_ID_PREFIX}-{run_manager.run_id}-0"
await run_manager.on_llm_end(full_message) await run_manager.on_llm_end(full_message)
return full_message return full_message
@ -613,6 +620,10 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC):
raise raise
msg = add_ai_message_chunks(chunks[0], *chunks[1:]) msg = add_ai_message_chunks(chunks[0], *chunks[1:])
if run_manager and msg.id and msg.id.startswith("lc_"):
msg.id = f"{_LC_ID_PREFIX}-{run_manager.run_id}-0"
run_manager.on_llm_end(msg) run_manager.on_llm_end(msg)
@override @override
@ -686,6 +697,10 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC):
raise raise
msg = add_ai_message_chunks(chunks[0], *chunks[1:]) msg = add_ai_message_chunks(chunks[0], *chunks[1:])
if run_manager and msg.id and msg.id.startswith("lc_"):
msg.id = f"{_LC_ID_PREFIX}-{run_manager.run_id}-0"
await run_manager.on_llm_end(msg) await run_manager.on_llm_end(msg)
# --- Custom methods --- # --- Custom methods ---