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]: Support injected tool args that are arbitrary types #27045

Merged
merged 5 commits into from
Oct 2, 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
7 changes: 5 additions & 2 deletions libs/core/langchain_core/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _create_subset_model_v2(
fn_description: Optional[str] = None,
) -> type[pydantic.BaseModel]:
"""Create a pydantic model with a subset of the model fields."""
from pydantic import create_model
from pydantic import ConfigDict, create_model
from pydantic.fields import FieldInfo

descriptions_ = descriptions or {}
Expand All @@ -278,7 +278,10 @@ def _create_subset_model_v2(
if field.metadata:
field_info.metadata = field.metadata
fields[field_name] = (field.annotation, field_info)
rtn = create_model(name, **fields) # type: ignore

rtn = create_model( # type: ignore
name, **fields, __config__=ConfigDict(arbitrary_types_allowed=True)
)

# TODO(0.3): Determine if there is a more "pydantic" way to preserve annotations.
# This is done to preserve __annotations__ when working with pydantic 2.x
Expand Down
15 changes: 15 additions & 0 deletions libs/core/tests/unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2090,3 +2090,18 @@ class FooSchema(BaseModel):

with pytest.raises(NotImplementedError):
assert tool.invoke("hello") == "hello"


def test_injected_arg_with_complex_type() -> None:
"""Test that an injected tool arg can be a complex type."""

class Foo:
def __init__(self) -> None:
self.value = "bar"

@tool
def injected_tool(x: int, foo: Annotated[Foo, InjectedToolArg]) -> str:
"""Tool that has an injected tool arg."""
return foo.value

assert injected_tool.invoke({"x": 5, "foo": Foo()}) == "bar" # type: ignore
Loading