core: Removing unnecessary pydantic core schema rebuilds (#30848)

We only need to rebuild model schemas if type annotation information
isn't available during declaration - that shouldn't be the case for
these types corrected here.

Need to do more thorough testing to make sure these structures have
complete schemas, but hopefully this boosts startup / import time.
This commit is contained in:
Sydney Runkle
2025-04-16 12:00:08 -04:00
committed by GitHub
parent 60d8ade078
commit 88fce67724
22 changed files with 26 additions and 63 deletions

View File

@@ -227,9 +227,6 @@ class SerializableModel(GenericFakeChatModel):
return True
SerializableModel.model_rebuild()
def test_serialization_with_rate_limiter() -> None:
"""Test model serialization with rate limiter."""
from langchain_core.load import dumps

View File

@@ -45,8 +45,6 @@ def test_base_generation_parser() -> None:
assert isinstance(content, str)
return content.swapcase()
StrInvertCase.model_rebuild()
model = GenericFakeChatModel(messages=iter([AIMessage(content="hEllo")]))
chain = model | StrInvertCase()
assert chain.invoke("") == "HeLLO"

View File

@@ -35,9 +35,6 @@ class FakeStructuredChatModel(FakeListChatModel):
return "fake-messages-list-chat-model"
FakeStructuredChatModel.model_rebuild()
def test_structured_prompt_pydantic() -> None:
class OutputSchema(BaseModel):
name: str

View File

@@ -1188,9 +1188,6 @@ class HardCodedRetriever(BaseRetriever):
return self.documents
HardCodedRetriever.model_rebuild()
async def test_event_stream_with_retriever() -> None:
"""Test the event stream with a retriever."""
retriever = HardCodedRetriever(

View File

@@ -0,0 +1,20 @@
import importlib
from pathlib import Path
from pydantic import BaseModel
def test_all_models_built() -> None:
for path in Path("../core/langchain_core/").glob("*"):
module_name = path.stem
if not module_name.startswith(".") and path.suffix != ".typed":
module = importlib.import_module("langchain_core." + module_name)
all_ = getattr(module, "__all__", [])
for attr_name in all_:
attr = getattr(module, attr_name)
try:
if issubclass(attr, BaseModel):
assert attr.__pydantic_complete__ is True
except TypeError:
# This is expected for non-class attributes
pass

View File

@@ -1091,9 +1091,6 @@ class FooBase(BaseTool):
return assert_bar(bar, bar_config)
FooBase.model_rebuild()
class AFooBase(FooBase):
async def _arun(self, bar: Any, bar_config: RunnableConfig, **kwargs: Any) -> Any:
return assert_bar(bar, bar_config)