I am trying to use an ImportString to get an instance of a given object: from __future__ import annotations from pydantic.types import ImportString from pydantic import Field, BaseModel from typing import Annotated, Callable class Foo: """A test class.""" def __init__(self, name: str) -> None: self.name = name def __call__(self, name: str) -> str: """Call the foo class.""" return f"hello {name}" x = Foo("world") def say_hello(name: str) -> str: """Say hello to a name.""" return f"hello {name}" def test_pydantic_ai_tool() -> None: """Test that the PydanticAiTool type can be used to validate a tool.""" class Bar(BaseModel): foo: Annotated[ ImportString[Callable[..., str]], Field(description="A tool that can be used in a pydantic-ai agent."), ] bar = Bar.model_validate( { "foo": "_tests.utils.types.test__pydantic_ai_tool.say_hello", }, ) assert bar.foo("world") == "hello world" assert bar.model_dump_json() == '{"foo":"_tests.utils.types.test__pydantic_ai_tool.say_hello"}' def test_pydantic_ai_tool_two() -> None: """Test that the PydanticAiTool type can be used to validate a tool.""" class Bar(BaseModel): foo: Annotated[ ImportString[Foo], Field(description="A tool that can be used in a pydantic-ai agent."), ] bar = Bar.model_validate( { "foo": "_tests.utils.types.test__pydantic_ai_tool.x", }, ) assert bar.foo("world") == "hello world" assert bar.model_dump_json() == '{"foo": "_tests.utils.types.test__pydantic_ai_tool.x"}' In this example, the first test works but the second test fails with: E pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'test__pydantic_ai_tool.Foo'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it. E E If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion. E E For further information visit https://errors.pydantic.dev/2.10/u/schema-for-unknown-type .venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py:513: PydanticSchemaGenerationError I would expect the ImportString to import the object at the path and check that it is an instance of the given type, and then for the schema generation it should just return the import string (which is the case for the first test, if you do model_dump_json you get the import string and not the schema of whatever type you are trying to import). I am not sure why there needs to be a schema of the ImportString generic type. Is there a way around this? Continue reading...