crewAIInc / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
18.96k stars 2.61k forks source link

Using `Task.output_json` is throwing TypeError #684

Closed billy-le closed 2 weeks ago

billy-le commented 3 months ago

I raised an issue in the Discord channel but haven't gotten a solution yet so I thought I might open an issue here.

This is the repo where I'm having the problem. The branch I'm working on is ft/pydantic

This is the error I keep having even after following instructions from the community.

INFO:     127.0.0.1:52734 - "POST /generate HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/routing.py", line 72, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/fastapi/routing.py", line 278, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/fastapi/routing.py", line 193, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/starlette/concurrency.py", line 42, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 851, in run
    result = context.run(func, *args)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/projects/dotted-crew/src/main.py", line 13, in generate_itinerary
    result = dotted_crew.run()
             ^^^^^^^^^^^^^^^^^
  File "/Users/billyle/projects/dotted-crew/src/crew.py", line 92, in run
    result = crew.kickoff()
             ^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/crew.py", line 266, in kickoff
    result, manager_metrics = self._run_hierarchical_process()  # type: ignore # Unpacking a string is disallowed
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/crew.py", line 348, in _run_hierarchical_process
    task_output = task.execute(
                  ^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/task.py", line 183, in execute
    result = self._execute(
             ^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/task.py", line 198, in _execute
    exported_output = self._export_output(result)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/task.py", line 272, in _export_output
    model_schema = PydanticSchemaParser(model=model).get_schema()  # type: ignore # Argument "model" to "PydanticSchemaParser" has incompatible type "type[BaseModel] | None"; expected "type[BaseModel]"
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/utilities/pydantic_schema_parser.py", line 16, in get_schema
    return self._get_model_schema(self.model)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/utilities/pydantic_schema_parser.py", line 21, in _get_model_schema
    field_type_str = self._get_field_type(field, depth + 1)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/utilities/pydantic_schema_parser.py", line 33, in _get_field_type
    nested_schema = self._get_model_schema(list_item_type, depth + 1)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/utilities/pydantic_schema_parser.py", line 21, in _get_model_schema
    field_type_str = self._get_field_type(field, depth + 1)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/utilities/pydantic_schema_parser.py", line 33, in _get_field_type
    nested_schema = self._get_model_schema(list_item_type, depth + 1)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/utilities/pydantic_schema_parser.py", line 21, in _get_model_schema
    field_type_str = self._get_field_type(field, depth + 1)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/billyle/.pyenv/versions/3.12.2/envs/new-crew/lib/python3.12/site-packages/crewai/utilities/pydantic_schema_parser.py", line 37, in _get_field_type
    elif issubclass(field_type, BaseModel):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen abc>", line 123, in __subclasscheck__
TypeError: issubclass() arg 1 must be a class

The AI Agents sometimes are able to have a "Final Answer" but it still fails to parse it into a Pydantic Model.

ArtyomVancyan commented 3 months ago

Try describing your fields with Field class. For example, in the weather.py you should do the following:

from pydantic import BaseModel
from typing import Union

class Weather(BaseModel):
    date: str = Field(description="Date in YYYY-MM-DD format.")
    temp: float = Field(description="A floating number representing temperature.")
    scale: Union["fahrenheit", "celsius"] = Field(description="The scale of the weather.")
billy-le commented 3 months ago

@ArtyomVancyan Unfortunately I am presented with the same errors after updating the generative models with the Field class.

ArtyomVancyan commented 3 months ago

Just to clarify, have you updated all the models or just this one? Because you should update all of them. Also, make sure all models are inherited from the BaseModel.

billy-le commented 3 months ago

I've updated all the models and dependant models assigned to a Task.output_json.

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been stalled for 5 days with no activity.