Skip to content

Error in LangChainToolAdapter GoogleDriveSearchTool: Unable to generate pydantic-core schema #6385

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

Open
dnascimento opened this issue Apr 24, 2025 · 0 comments
Labels
help wanted Extra attention is needed needs-triage

Comments

@dnascimento
Copy link

What happened?

Describe the bug
The pytdantic-core schema seems to be failing with tools like GoogleDriveSearchTool. What am I missing here?

pydantic==2.10.6
pydantic-settings==2.8.1
pydantic_core==2.27.2
langchain==0.3.24
langchain-aws==0.2.18
langchain-community==0.3.22
langchain-core==0.3.55
langchain-experimental==0.3.4
langchain-text-splitters==0.3.8
autogen-agentchat==0.4.9.2
autogen-core==0.5.4
autogen-ext==0.5.4

ERROR:root:An error occurred: Unable to generate pydantic-core schema for <class 'langchain_core.callbacks.manager.CallbackManagerForToolRun'>. 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.

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.

For further information visit https://errors.pydantic.dev/2.11/u/schema-for-unknown-type
Traceback (most recent call last):
  File "tests/tool_langchain.py", line 112, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "tests/tool_langchain.py", line 80, in main
    autogen_tool = LangChainToolAdapter(drive_tool)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/autogen_ext/tools/langchain/_langchain_adapter.py", line 175, in __init__
    args_type = create_model(f"{name}Args", **fields)  # type: ignore
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/main.py", line 1772, in create_model
    return meta(
           ^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 237, in __new__
    complete_model_class(
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 597, in complete_model_class
    schema = gen_schema.generate_schema(cls)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 706, in generate_schema
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 984, in _generate_schema_inner
    return self._model_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 817, in _model_schema
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1186, in _generate_md_field_schema
    common_field = self._common_field_schema(name, field_info, decorators)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1352, in _common_field_schema
    schema = self._apply_annotations(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 2264, in _apply_annotations
    schema = get_inner_schema(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 83, in __call__
    schema = self._handler(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 2246, in inner_handler
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 989, in _generate_schema_inner
    return self.match_type(obj)
           ^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1103, in match_type
    return self._match_generic_type(obj, origin)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1126, in _match_generic_type
    return self._union_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1414, in _union_schema
    choices.append(self.generate_schema(arg))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 706, in generate_schema
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 989, in _generate_schema_inner
    return self.match_type(obj)
           ^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1107, in match_type
    return self._unknown_type_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 634, in _unknown_type_schema
    raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'langchain_core.callbacks.manager.CallbackManagerForToolRun'>. 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.

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.

For further information visit https://errors.pydantic.dev/2.11/u/schema-for-unknown-type

To Reproduce

import asyncio
import os
from autogen_ext.tools.langchain import LangChainToolAdapter
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.messages import TextMessage
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_core import CancellationToken
from google.oauth2 import service_account
from langchain_googledrive.tools.google_drive.tool import GoogleDriveSearchTool
from langchain_googledrive.utilities.google_drive import GoogleDriveAPIWrapper
import logging

async def main() -> None:
    # Step 1: Authenticate with service account
    SERVICE_ACCOUNT_FILE = "./service-account.json"
    SCOPES = ["https://www.googleapis.com/auth/drive.readonly"]

    try:
        credentials = service_account.Credentials.from_service_account_file(
            SERVICE_ACCOUNT_FILE, scopes=SCOPES
        )
        logging.info(f"Using service account: {credentials.service_account_email}")

        # Initialize Google Drive API wrapper
        folder_id = "root"
        drive_wrapper = GoogleDriveAPIWrapper(
            credentials=credentials,
            folder_id=folder_id,
            num_results=20,
            template="gdrive-query-in-folder",
            mode="documents-markdown"
        )
        
        # Create the Google Drive search tool
        drive_tool = GoogleDriveSearchTool(api_wrapper=drive_wrapper)

        print(drive_tool.invoke("my_docs"))
        
        # # Wrap the LangChain tool with Autogen adapter
        autogen_tool = LangChainToolAdapter(drive_tool)
        
        # Initialize the model client
        model_client = OpenAIChatCompletionClient(
            model="gpt-4",
            api_key=os.getenv("OPENAI_API_KEY"),
            model_config={"arbitrary_types_allowed": True}
        )
        
        # Create the assistant agent with the Google Drive tool
        agent = AssistantAgent(
            "assistant",
            tools=[autogen_tool],
            model_client=model_client,
            system_message="You can search for files in Google Drive using the provided tool."
        )
        
        await Console(
            agent.on_messages_stream(
                [TextMessage(content="Tell me about the user", source="user")],
                CancellationToken()
            )
        )

    except Exception as e:
        logging.error(f"An error occurred: {str(e)}")
        raise

if __name__ == "__main__":
    logging.info("Starting Google Drive search tool")
    asyncio.run(main())
    logging.info("Done")

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Which packages was the bug in?

Python Extensions (autogen-ext)

AutoGen library version.

Python 0.5.4

Other library version.

No response

Model used

gpt-4

Model provider

OpenAI

Other model provider

No response

Python version

3.12

.NET version

None

Operating system

Ubuntu

@ekzhu ekzhu added the help wanted Extra attention is needed label Apr 25, 2025
@ekzhu ekzhu changed the title LangChainToolAdapter: GoogleDriveSearchTool Error in LangChainToolAdapter GoogleDriveSearchTool: Unable to generate pydantic-core schema Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed needs-triage
Projects
None yet
Development

No branches or pull requests

2 participants