langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.51k stars 2.13k forks source link

[Agent][createXmlAgent] DynamicStructuredTool Received tool input did not match expected schema #4635

Closed fvisticot closed 3 months ago

fvisticot commented 7 months ago

Checked other resources

Example Code

arg passed to the tool is correct but in String format instead of object.

{ "to": "test@test.com", "subject": "je vais bien", "body": "" }

const agent = await createXmlAgent({
            llm: this.llm,
            tools: this.tools,
            prompt: this.prompt,
        });

        const agentExecutor = new AgentExecutor({
            agent,
            tools: this.tools,
            returnIntermediateSteps: true,
            verbose: false
        });

        const result = await agentExecutor.invoke({
            input: question,
            chat_history: this.chatMessageHistory
        });
const emailTool = new DynamicStructuredTool({
    name: "EmailService",
    description:
        "Used to send email to user. to is user email, subject is email subject, body is email boday",
    schema: z.object({
        to: z.string().describe("to is email destination"),
        subject: z.string().describe("subject is email title"),
        body: z.string().describe("body is email body"),
    }),
    func: async ({to, subject, body}) => {
        console.log(`Email params: ${to}, ${subject}`);
        await sendEmail(to, subject, body);
        return "OK"
    }
});

Error Message and Stack Trace (if applicable)

{ "issues": [ { "code": "invalid_type", "expected": "object", "received": "string", "path": [], "message": "Expected object, received string" } ], "name": "ZodError" }

Description

arg passed to the tool is correct but in String format instead of object.

System Info

0.1.22

dosubot[bot] commented 7 months ago

Salut @fvisticot, ravi de te revoir par ici ! J'espère que tout va bien de ton côté.

Based on the information you've provided, it seems like the issue is related to the way the input is being passed to the invoke method of the AgentExecutor class. The invoke method expects an object as input, but it seems like a string is being passed instead.

Here's the relevant part of your code:

const result = await agentExecutor.invoke({
    input: question,
    chat_history: this.chatMessageHistory
});

In this code, question should be an object that matches the schema defined in your DynamicStructuredTool. However, from the error message you're seeing, it seems like question is a string.

To resolve this issue, you should ensure that question is an object that matches the schema defined in your DynamicStructuredTool. Here's an example of how you might do this:

const question = {
    to: "test@test.com",
    subject: "je vais bien",
    body: ""
};

const result = await agentExecutor.invoke({
    input: question,
    chat_history: this.chatMessageHistory
});

In this example, question is an object that matches the schema defined in your DynamicStructuredTool, so it should be correctly processed by the invoke method of the AgentExecutor class.

I hope this helps! If you have any further questions, please don't hesitate to ask.

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
fvisticot commented 7 months ago

The automatic answer is not correct. I can not have a question with this format: Any other advice ?

const question = {
    to: "test@test.com",
    subject: "je vais bien",
    body: ""
};
mnofrizal commented 6 months ago

any update ? i got same error