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
21.48k stars 2.98k forks source link

CrewAI - Langchain StructuredTool JSON markdown string as input #923

Open ChristianJohnston97 opened 4 months ago

ChristianJohnston97 commented 4 months ago

I have a custom tool using the langchain StructuredTool.from_function

from langchain.tools.base import StructuredTool
from langchain_core.pydantic_v1 import BaseModel, Field

create_draft_tool = StructuredTool.from_function(
func=create_draft,
name="Create Draft",
description="""
    Create an email draft. Ensure to pass input as a valid JSON.
    Only pass in a JSON object. Do not pass in any markdown string anywhere.
""",
args_schema=DraftInput,
return_direct=True
)

where DraftInput is a Pydantic class (fields excluded)

class DraftInput(BaseModel):
  to: str = Field(
      ...,
      description="Who to send the email to",
      alias="to"
  )

but when the tool is called, it passes in the correct JSON but inside a markdown string like so:

Action: Create Draft
Action Input:
```json
   {
     "to": "<email>"
   }
```.

and so the tool fails with the error Task output: Error: the Action Input is not a valid key, value dictionary.

How can I force it to just input the JSON object and not inside the markdown string.

langchain~=0.2.6

crewai==0.36.0

mackimart1 commented 4 months ago

I've been having problems with my tools as well not sure why my search tool decided to stop working along with every other tool.

ChristianJohnston97 commented 4 months ago

@theCyberTech any thoughts? This is quite a big issue when using tools.

voytas75 commented 1 month ago

i have the same issue with build-in FileWriteTool. crewai version: 0.63.2 crewai-tools 0.12.1 Python 3.12.6 langchain 0.2.16 Windows 11

# Agent: Thinker
## Thought: Thought: I need to write content to a specified TXT file to meet the given criteria.
## Using tool: File Writer Tool
## Tool Input:
"{\"filename\": \"important_document.txt\", \"content\": \"This is the content of the important document that must be written to the file.\", \"overwrite\": true}"
## Tool Output:
Error: the Action Input is not a valid key, value dictionary.

i think that https://github.com/crewAIInc/crewAI/issues/1344 is connected with the issue

joaomdmoura commented 1 month ago

I would recommend build tools using the from crewai_tools import BaseTool class - https://docs.crewai.com/how-to/Create-Custom-Tools/

@voytas75 I think this might be not related, but could you try on the new version 0.63.6 ?

voytas75 commented 1 month ago

@joaomdmoura this error still occurs on 0.63.6

I will just add that now, as in previous versions, calling the tool directly creates a file:

from crewai_tools import FileWriterTool

file_writer_tool = FileWriterTool()

input_args = {
    'filename': "test.txt",
    'content': "test content",
    'directory': "c:\\temp",
    'overwrite': True,
    'encoding': 'utf-8'
}
file_writer_tool._run(** input_args)
joaomdmoura commented 1 month ago

Nice testing it today, what model you're using so I can use the same?

voytas75 commented 1 month ago

azure/gpt4o https://gist.github.com/voytas75/1d73b9d4b1c6d5a6e995fb823e47c688

voytas75 commented 1 month ago

crewai version: 0.64

the same issue as earlier https://github.com/crewAIInc/crewAI/issues/923#issuecomment-2371935965

@ChristianJohnston97 what about you custom tool?

joaomdmoura commented 1 month ago

Thanks for the crew gist I'll look into this one soon

voytas75 commented 1 month ago

crewai version: 0.70.1

the same issue as earlier https://github.com/crewAIInc/crewAI/issues/923#issuecomment-2371935965

@ChristianJohnston97 what about your custom tool?

senj commented 1 month ago

I have the same problem, for me sometimes it works with

## Tool Input:
{\"query\": \"my query\"}

but most of the time I get something like

## Tool Input:
"{\"name\": \"my query\", \"description\": \"\", \"args_schema\": {}, \"return_direct\": false, \"verbose\": false}"

resulting in

## Tool Output:
Error: the Action Input is not a valid key, value dictionary.

using gpt-4o-mini and tried @tool and using the base class

saginawj commented 3 weeks ago

same issue with 0.76.2. In fact, just running "crewai create crew" and then uncommenting the Custom Tool in the template, I get this error.

arnaud0617 commented 1 week ago

I have the same error and this is very impredictable.

For the same custom tool, one iteration it works, next one it fails.

when ## Tool Input: "{\"query\": {\"description\": >> It fails ...... and I get the message Input should be a valid string

but sometimes, without changing anything , description disappears and it works!

Tool Input:

"{\"query\": \" Expected prompt.... > and it works.


crewai-tools = "==0.13.4" crewai = "==0.76.9"

voytas75 commented 1 week ago

i have the same issue with build-in FileWriteTool. crewai version: 0.63.2 crewai-tools 0.12.1 Python 3.12.6 langchain 0.2.16 Windows 11

# Agent: Thinker
## Thought: Thought: I need to write content to a specified TXT file to meet the given criteria.
## Using tool: File Writer Tool
## Tool Input:
"{\"filename\": \"important_document.txt\", \"content\": \"This is the content of the important document that must be written to the file.\", \"overwrite\": true}"
## Tool Output:
Error: the Action Input is not a valid key, value dictionary.

i think that #1344 is connected with the issue

it worked! crewai version: 0.79.4 crewai tools version: 0.14.0