crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

CrewAI - Langchain StructuredTool JSON markdown string as input

Open ChristianJohnston97 opened this issue 1 year ago • 16 comments

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

ChristianJohnston97 avatar Jul 12 '24 08:07 ChristianJohnston97

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.

mackimart1 avatar Jul 12 '24 14:07 mackimart1

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

ChristianJohnston97 avatar Jul 16 '24 13:07 ChristianJohnston97

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

voytas75 avatar Sep 24 '24 17:09 voytas75

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 ?

joaomdmoura avatar Sep 25 '24 03:09 joaomdmoura

@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)

voytas75 avatar Sep 25 '24 05:09 voytas75

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

joaomdmoura avatar Sep 25 '24 15:09 joaomdmoura

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

voytas75 avatar Sep 25 '24 15:09 voytas75

crewai version: 0.64

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

@ChristianJohnston97 what about you custom tool?

voytas75 avatar Sep 27 '24 12:09 voytas75

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

joaomdmoura avatar Sep 28 '24 00:09 joaomdmoura

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?

voytas75 avatar Oct 11 '24 15:10 voytas75

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

senj avatar Oct 23 '24 11:10 senj

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.

saginawj avatar Oct 26 '24 15:10 saginawj

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"

arnaud0617 avatar Nov 14 '24 06:11 arnaud0617

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

voytas75 avatar Nov 14 '24 10:11 voytas75

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] avatar Dec 14 '24 12:12 github-actions[bot]

Just refreshing this as I think the issue is related to the fact that some inputs are being returned in json format from the llm with lowercase boolean values, whereas when that input is run through ast_literal.eval(), it throws an error because python literal dicts need booleans in uppercase. I made a comment on this post to hopefully see if this is indeed the root cause of these "Error: the Action Input is not a valid key, value dictionary." issues. https://github.com/crewAIInc/crewAI/issues/1344#issuecomment-2549417475

jtknox avatar Dec 17 '24 19:12 jtknox

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] avatar Jan 19 '25 12:01 github-actions[bot]

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

github-actions[bot] avatar Jan 24 '25 12:01 github-actions[bot]