Skip to content

The agent engineering platform.

core[minor]: Add optional ID field to Document schema (#23411)

Eugene Yurtsev96b72edac83b
diff --git a/libs/core/langchain_core/documents/base.py b/libs/core/langchain_core/documents/base.py
index ed6955d7e6..e48bc55c94 100644
--- a/libs/core/langchain_core/documents/base.py
+++ b/libs/core/langchain_core/documents/base.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Any, List, Literal
+from typing import Any, List, Literal, Optional
from langchain_core.load.serializable import Serializable
from langchain_core.pydantic_v1 import Field
@@ -21,6 +21,16 @@ class Document(Serializable):
)
"""
+ # The ID field is optional at the moment.
+ # It will likely become required in a future major release after
+ # it has been adopted by enough vectorstore implementations.
+ id: Optional[str] = None
+ """An optional identifier for the document.
+
+ Ideally this should be unique across the document collection and formatted
+ as a UUID, but this will not be enforced.
+ """
+
page_content: str
"""String text."""
metadata: dict = Field(default_factory=dict)
@@ -42,3 +52,18 @@ class Document(Serializable):
def get_lc_namespace(cls) -> List[str]:
"""Get the namespace of the langchain object."""
return ["langchain", "schema", "document"]
+
+ def __str__(self) -> str:
+ """Override __str__ to restrict it to page_content and metadata."""
+ # The format matches pydantic format for __str__.
+ #
+ # The purpose of this change is to make sure that user code that
+ # feeds Document objects directly into prompts remains unchanged
+ # due to the addition of the id field (or any other fields in the future).
+ #
+ # This override will likely be removed in the future in favor of
+ # a more general solution of formatting content directly inside the prompts.
+ if self.metadata:
+ return f"page_content='{self.page_content}' metadata={self.metadata}"
+ else:
+ return f"page_content='{self.page_content}'"
diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.py
index 5a0cf6e16a..7f06928f4f 100644
--- a/libs/core/tests/unit_tests/runnables/test_runnable.py
+++ b/libs/core/tests/unit_tests/runnables/test_runnable.py
@@ -311,6 +311,10 @@ def test_schemas(snapshot: SnapshotAssertion) -> None:
"properties": {
"page_content": {"title": "Page Content", "type": "string"},
"metadata": {"title": "Metadata", "type": "object"},
+ "id": {
+ "title": "Id",
+ "type": "string",
+ },
"type": {
"title": "Type",
"enum": ["Document"],