GenkitG
Genkitโ€ข10mo ago
wet-aqua

Does anyone have examples or docs of

Does anyone have examples or docs of using custom tools when using structured output? After moving to structured JSON output, it seems that the tool use also has to be structured (makes sense). Not finding a whole lot in the docs, but it seems I need to use defineTool()'s inputJsonSchema instead of inputSchema and vice versa.

The tool works fine when executed alone in the genkit backend, but when called through an LLM call, I get this response:

I am sorry, I cannot fulfill this request. The response should be in JSON format, but I need to call the getWeather tool first to get the weather in Los Angeles. Then I can put the weather information in the JSON format.


current tool definition:

const inSchema: JSONSchema7 = {
  title: "getWeather",
  description: "Get the current weather based on a location",
  type: "object",
  $schema: "http://json-schema.org/draft-07/schema#",
  required: ["location"],
  properties: {
    location: {
      title: "Location",
      type: "string",
      description: "The location to get the current weather for",
    },
  },
};

const outSchema: JSONSchema7 = {
  title: "getWeather",
  description: "The current weather based on a location (eg city or country)",
  type: "object",
  $schema: "http://json-schema.org/draft-07/schema#",
  required: ["location", "description"],
  properties: {
    location: {
      title: "Location",
      type: "string",
      description: "The city or country to get the current weather for",
    },
    description: {
      title: "Description",
      type: "string",
      description: "The current weather description",
    },
  },
};

const getWeather = ai.defineTool(
  {
    name: "getWeather",
    description: "Gets the current weather in a given location",
    inputJsonSchema: inSchema,
    outputJsonSchema: outSchema,
  },
  async (input) => {
    return {
      location: input.location,
      description: `The weather in ${input.location} is 20 celcius`,
    };
  }
Was this page helpful?