diff --git a/libs/core/langchain_core/load/mapping.py b/libs/core/langchain_core/load/mapping.py index 5dac9ccd839..10a3a6413c7 100644 --- a/libs/core/langchain_core/load/mapping.py +++ b/libs/core/langchain_core/load/mapping.py @@ -481,6 +481,12 @@ SERIALIZABLE_MAPPING: Dict[Tuple[str, ...], Tuple[str, ...]] = { "retry", "RunnableRetry", ), + ("langchain_core", "prompts", "structured", "StructuredPrompt"): ( + "langchain_core", + "prompts", + "structured", + "StructuredPrompt", + ), } # Needed for backwards compatibility for old versions of LangChain where things @@ -522,12 +528,6 @@ _OG_SERIALIZABLE_MAPPING: Dict[Tuple[str, ...], Tuple[str, ...]] = { "image", "ImagePromptTemplate", ), - ("langchain", "prompts", "chat", "StructuredPrompt"): ( - "langchain_core", - "prompts", - "structured", - "StructuredPrompt", - ), } # Needed for backwards compatibility for a few versions where we serialized diff --git a/libs/core/langchain_core/prompts/structured.py b/libs/core/langchain_core/prompts/structured.py index fd4b1b40b42..882f34cc827 100644 --- a/libs/core/langchain_core/prompts/structured.py +++ b/libs/core/langchain_core/prompts/structured.py @@ -3,6 +3,7 @@ from typing import ( Callable, Dict, Iterator, + List, Mapping, Optional, Sequence, @@ -34,6 +35,15 @@ from langchain_core.runnables.base import ( class StructuredPrompt(ChatPromptTemplate): schema_: Union[Dict, Type[BaseModel]] + @classmethod + def get_lc_namespace(cls) -> List[str]: + """Get the namespace of the langchain object. + + For example, if the class is `langchain.llms.openai.OpenAI`, then the + namespace is ["langchain", "llms", "openai"] + """ + return cls.__module__.split(".") + @classmethod def from_messages_and_schema( cls, diff --git a/libs/langchain/tests/unit_tests/load/test_serializable.py b/libs/langchain/tests/unit_tests/load/test_serializable.py index 64d762035f0..a8a35a4fe7c 100644 --- a/libs/langchain/tests/unit_tests/load/test_serializable.py +++ b/libs/langchain/tests/unit_tests/load/test_serializable.py @@ -40,16 +40,23 @@ def import_all_modules(package_name: str) -> dict: def test_serializable_mapping() -> None: - # This should have had a different namespace, as it was never - # exported from the langchain module, but we keep for whoever has - # already serialized it. to_skip = { + # This should have had a different namespace, as it was never + # exported from the langchain module, but we keep for whoever has + # already serialized it. ("langchain", "prompts", "image", "ImagePromptTemplate"): ( "langchain_core", "prompts", "image", "ImagePromptTemplate", ), + # This is not exported from langchain, only langchain_core + ("langchain_core", "prompts", "structured", "StructuredPrompt"): ( + "langchain_core", + "prompts", + "structured", + "StructuredPrompt", + ), } serializable_modules = import_all_modules("langchain") missing = set(SERIALIZABLE_MAPPING).difference(