Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[patch]: exclude special pydantic init params #25084

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libs/core/langchain_core/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,10 @@ def _get_all_basemodel_annotations(
if isinstance(cls, type):
annotations: Dict[str, Type] = {}
for name, param in inspect.signature(cls).parameters.items():
# Exclude hidden init args added by pydantic Config. For example if
# BaseModel(extra="allow") then "extra_data" will part of init sig.
if (fields := getattr(cls, "__fields__", {})) and name not in fields:
continue
annotations[name] = param.annotation
orig_bases: Tuple = getattr(cls, "__orig_bases__", tuple())
# cls has subscript: cls = FooBar[int]
Expand Down
6 changes: 3 additions & 3 deletions libs/core/tests/unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,11 +1751,11 @@ def test__get_all_basemodel_annotations_v2(use_v1_namespace: bool) -> None:

if use_v1_namespace:

class ModelA(BaseModel, Generic[A]):
class ModelA(BaseModel, Generic[A], extra="allow"):
a: A
else:

class ModelA(BaseModelProper, Generic[A]): # type: ignore[no-redef]
class ModelA(BaseModelProper, Generic[A], extra="allow"): # type: ignore[no-redef]
a: A

class ModelB(ModelA[str]):
Expand Down Expand Up @@ -1812,7 +1812,7 @@ class ModelD(ModelC, Generic[D]):
def test__get_all_basemodel_annotations_v1() -> None:
A = TypeVar("A")

class ModelA(BaseModel, Generic[A]):
class ModelA(BaseModel, Generic[A], extra="allow"):
a: A

class ModelB(ModelA[str]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ class Joke(BaseModel): # Uses langchain_core.pydantic_v1.BaseModel
assert isinstance(chunk, dict) # for mypy
assert set(chunk.keys()) == {"setup", "punchline"}

def test_tool_message_histories_string_content(
self,
model: BaseChatModel,
) -> None:
def test_tool_message_histories_string_content(self, model: BaseChatModel) -> None:
"""
Test that message histories are compatible with string tool contents
(e.g. OpenAI).
Expand Down
Loading