From f691dc348f7c2caf2a84cdf65fc684bdc2dafc26 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Fri, 15 Aug 2025 15:42:17 -0400 Subject: [PATCH] refactor: make `ensure_id` public --- libs/core/langchain_core/messages/__init__.py | 7 ++++++ .../langchain_core/messages/content_blocks.py | 22 +++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/libs/core/langchain_core/messages/__init__.py b/libs/core/langchain_core/messages/__init__.py index 410299ea5b5..a0482bee862 100644 --- a/libs/core/langchain_core/messages/__init__.py +++ b/libs/core/langchain_core/messages/__init__.py @@ -33,6 +33,8 @@ if TYPE_CHECKING: ) from langchain_core.messages.chat import ChatMessage, ChatMessageChunk from langchain_core.messages.content_blocks import ( + LC_AUTO_PREFIX, + LC_ID_PREFIX, Annotation, AudioContentBlock, Citation, @@ -53,6 +55,7 @@ if TYPE_CHECKING: WebSearchResult, convert_to_openai_data_block, convert_to_openai_image_block, + ensure_id, is_data_content_block, is_reasoning_block, is_text_block, @@ -85,6 +88,8 @@ if TYPE_CHECKING: ) __all__ = ( + "LC_AUTO_PREFIX", + "LC_ID_PREFIX", "AIMessage", "AIMessageChunk", "Annotation", @@ -128,6 +133,7 @@ __all__ = ( "convert_to_openai_data_block", "convert_to_openai_image_block", "convert_to_openai_messages", + "ensure_id", "filter_messages", "get_buffer_string", "is_data_content_block", @@ -145,6 +151,7 @@ __all__ = ( ) _dynamic_imports = { + "ensure_id": "content_blocks", "AIMessage": "ai", "AIMessageChunk": "ai", "Annotation": "content_blocks", diff --git a/libs/core/langchain_core/messages/content_blocks.py b/libs/core/langchain_core/messages/content_blocks.py index 32673f1d3cc..ac00966814a 100644 --- a/libs/core/langchain_core/messages/content_blocks.py +++ b/libs/core/langchain_core/messages/content_blocks.py @@ -148,7 +148,7 @@ Used for: """ -def _ensure_id(id_val: Optional[str]) -> str: +def ensure_id(id_val: Optional[str]) -> str: """Ensure the ID is a valid string, generating a new UUID if not provided. Auto-generated UUIDs are prefixed by ``'lc_'`` to indicate they are @@ -1106,7 +1106,7 @@ def create_text_block( block = TextContentBlock( type="text", text=text, - id=_ensure_id(id), + id=ensure_id(id), ) if annotations is not None: block["annotations"] = annotations @@ -1156,7 +1156,7 @@ def create_image_block( msg = "Must provide one of: url, base64, or file_id" raise ValueError(msg) - block = ImageContentBlock(type="image", id=_ensure_id(id)) + block = ImageContentBlock(type="image", id=ensure_id(id)) if url is not None: block["url"] = url @@ -1216,7 +1216,7 @@ def create_video_block( msg = "mime_type is required when using base64 data" raise ValueError(msg) - block = VideoContentBlock(type="video", id=_ensure_id(id)) + block = VideoContentBlock(type="video", id=ensure_id(id)) if url is not None: block["url"] = url @@ -1276,7 +1276,7 @@ def create_audio_block( msg = "mime_type is required when using base64 data" raise ValueError(msg) - block = AudioContentBlock(type="audio", id=_ensure_id(id)) + block = AudioContentBlock(type="audio", id=ensure_id(id)) if url is not None: block["url"] = url @@ -1336,7 +1336,7 @@ def create_file_block( msg = "mime_type is required when using base64 data" raise ValueError(msg) - block = FileContentBlock(type="file", id=_ensure_id(id)) + block = FileContentBlock(type="file", id=ensure_id(id)) if url is not None: block["url"] = url @@ -1390,7 +1390,7 @@ def create_plaintext_block( block = PlainTextContentBlock( type="text-plain", mime_type="text/plain", - id=_ensure_id(id), + id=ensure_id(id), ) if text is not None: @@ -1443,7 +1443,7 @@ def create_tool_call( type="tool_call", name=name, args=args, - id=_ensure_id(id), + id=ensure_id(id), ) if index is not None: @@ -1480,7 +1480,7 @@ def create_reasoning_block( block = ReasoningContentBlock( type="reasoning", reasoning=reasoning or "", - id=_ensure_id(id), + id=ensure_id(id), ) if index is not None: @@ -1521,7 +1521,7 @@ def create_citation( prefixed with ``'lc_'`` to indicate it is a LangChain-generated ID. """ - block = Citation(type="citation", id=_ensure_id(id)) + block = Citation(type="citation", id=ensure_id(id)) if url is not None: block["url"] = url @@ -1565,7 +1565,7 @@ def create_non_standard_block( block = NonStandardContentBlock( type="non_standard", value=value, - id=_ensure_id(id), + id=ensure_id(id), ) if index is not None: