Skip to content
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

DOC: inaccurate description in the structured output conceptual guide #7577

Closed
2 tasks done
saharis9988 opened this issue Jan 24, 2025 · 1 comment · Fixed by #7578
Closed
2 tasks done

DOC: inaccurate description in the structured output conceptual guide #7577

saharis9988 opened this issue Jan 24, 2025 · 1 comment · Fixed by #7578
Labels
auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder

Comments

@saharis9988
Copy link
Contributor

Checklist

  • I added a very descriptive title to this issue.
  • I included a link to the documentation page I am referring to (if applicable).

Issue with current documentation:

Hi team,
I’m working on understanding how structured output is implemented without relying on the withStructuredOutput method, in order to better grasp the underlying process.
In my attempt to implement this via tool calling, I ran into an issue when trying to bind a Zod schema directly to bindTools (following the documentation). An exception is thrown because the schema doesn’t implement the necessary tool interface.
It seems the documentation example should explicitly show how to create a tool from the schema before passing it to bindTools, as this step appears to be missing..
docs

Idea or request for content:

No response

Copy link

dosubot bot commented Jan 24, 2025

Hey @saharis9988! I'm here to help you with any bugs, questions, or contributions you have. Let's work on resolving this issue together.

To address the issue you're encountering, you can create a tool from a Zod schema using the tool function from the @langchain/core/tools package before passing it to bindTools. Here's an example of how to do this:

import { z } from "zod";
import { tool } from "@langchain/core/tools";

const myToolSchema = z.object({
  param1: z.string().describe("Description for param1"),
  param2: z.number().describe("Description for param2"),
});

const myTool = tool(async (input): Promise<string> => {
  // Your custom logic here
  return `Processed ${input.param1} and ${input.param2}`;
}, {
  name: "my_custom_tool",
  description: "A custom tool that processes param1 and param2",
  schema: myToolSchema,
});

// Example invocation
await myTool.invoke({ param1: "example", param2: 42 });

Once you have defined your tool, you can bind it to a chat model using the .bindTools() method:

import { ChatGoogleGenerativeAI } from "@langchain/google-genai";

const llm = new ChatGoogleGenerativeAI({
  model: "gemini-1.5-pro",
  temperature: 0,
  maxRetries: 2,
  // other params...
});

const llmWithTools = llm.bindTools([myTool]);

// Now you can invoke the model with the tool bound
const res = await llmWithTools.invoke("Your prompt here");
console.log(res);

This setup allows the chat model to generate arguments for the tool, and you can then decide whether to execute the tool based on these arguments [1][2][3].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot dosubot bot added the auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder label Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant