From 86252d2ae6a860c96e8dd61505601d8d2128d63b Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Fri, 15 Aug 2025 15:39:36 -0400 Subject: [PATCH] refactor: move ID prefixes --- libs/core/langchain_core/messages/ai.py | 18 +++--------------- .../langchain_core/messages/content_blocks.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.py index 207ab6c1133..76bc8d980a1 100644 --- a/libs/core/langchain_core/messages/ai.py +++ b/libs/core/langchain_core/messages/ai.py @@ -26,18 +26,6 @@ from langchain_core.utils.usage import _dict_int_op logger = logging.getLogger(__name__) -_LC_AUTO_PREFIX = "lc_" -"""LangChain auto-generated ID prefix for messages and content blocks.""" - -_LC_ID_PREFIX = f"{_LC_AUTO_PREFIX}run-" -"""Internal tracing/callback system identifier. - -Used for: -- Tracing. Every LangChain operation (LLM call, chain execution, tool use, etc.) - gets a unique run_id (UUID) -- Enables tracking parent-child relationships between operations -""" - class InputTokenDetails(TypedDict, total=False): """Breakdown of input token counts. @@ -523,15 +511,15 @@ def add_ai_message_chunks( for id_ in candidates: if ( id_ - and not id_.startswith(_LC_ID_PREFIX) - and not id_.startswith(_LC_AUTO_PREFIX) + and not id_.startswith(types.LC_ID_PREFIX) + and not id_.startswith(types.LC_AUTO_PREFIX) ): chunk_id = id_ break else: # second pass: prefer lc_run-* ids over lc_* ids for id_ in candidates: - if id_ and id_.startswith(_LC_ID_PREFIX): + if id_ and id_.startswith(types.LC_ID_PREFIX): chunk_id = id_ break else: diff --git a/libs/core/langchain_core/messages/content_blocks.py b/libs/core/langchain_core/messages/content_blocks.py index 1448d37086b..32673f1d3cc 100644 --- a/libs/core/langchain_core/messages/content_blocks.py +++ b/libs/core/langchain_core/messages/content_blocks.py @@ -135,6 +135,18 @@ from uuid import uuid4 from typing_extensions import NotRequired, TypedDict, TypeGuard +LC_AUTO_PREFIX = "lc_" +"""LangChain auto-generated ID prefix for messages and content blocks.""" + +LC_ID_PREFIX = f"{LC_AUTO_PREFIX}run-" +"""Internal tracing/callback system identifier. + +Used for: +- Tracing. Every LangChain operation (LLM call, chain execution, tool use, etc.) + gets a unique run_id (UUID) +- Enables tracking parent-child relationships between operations +""" + def _ensure_id(id_val: Optional[str]) -> str: """Ensure the ID is a valid string, generating a new UUID if not provided. @@ -148,7 +160,7 @@ def _ensure_id(id_val: Optional[str]) -> str: Returns: A string ID, either the validated provided value or a newly generated UUID4. """ - return id_val or str(f"lc_{uuid4()}") + return id_val or str(f"{LC_AUTO_PREFIX}{uuid4()}") class Citation(TypedDict):