refactor: move ID prefixes

This commit is contained in:
Mason Daugherty
2025-08-15 15:39:36 -04:00
parent 8fc1973bbf
commit 86252d2ae6
2 changed files with 16 additions and 16 deletions

View File

@@ -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:

View File

@@ -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):