ai icon indicating copy to clipboard operation
ai copied to clipboard

generateText(): generated text is empty when used with custom tool

Open eyaldar opened this issue 1 year ago • 15 comments

Description

Hi, I have a custom tool I wrote and when I'm trying to use it with generateText it return an empty text result. my tool definition (assume I see the code is called and return result)

tool({
                description: ' "Use this tool to fetch real-time web data for queries that require the latest information, niche topics, local context, or when the answer may have changed since the model’s last update. This includes news, recent events, evolving technologies, up-to-date documentation, or anything likely not captured in training data. The tool supports issuing queries to a search engine and browsing the returned links to extract relevant content. ALWAYS PASS engine parameter with \'google\'',
                parameters: z.object({
                    query: z.string()
                }),
                // Function to execute the search
                execute: async ({ query }) => {
                    try {
                        console.log(`[SERPAPI] Searching for: ${query}`);
                        // Format our query for SerpAPI directly
                        const params = new URLSearchParams({
                            engine: 'google',
                            api_key: apiKey,
                            q: query,
                            output: 'json'
                        });
                        // Make the request to SERP API
                        const response = await fetch(`https://serpapi.com/search?${params.toString()}`);
                        if (!response.ok) {
                            throw new Error(`SERP API error: ${response.status} ${response.statusText}`);
                        }
                        const data = await response.json();
                        console.log(`[SERPAPI] Received response with ${data.organic_results?.length || 0} results`);
                        // Format the results to match our expected schema defined in constants.js
                        const results = data.organic_results?.map(result => ({
                            url: result.link,
                            snippet: result.snippet || result.title // Fallback to title if snippet isn't available
                        })) || [];
                        // Return in the format our app expects
                        return JSON.stringify({ results });
                    } catch (error) {
                        // Enhanced error logging with full stack trace
                        console.error('[SERPAPI] Search error:');
                        console.error(error);
                        console.error(error.stack);
                        return JSON.stringify({ results: [] }); // Return empty results on error to match schema
                    }
                }
            })

And this is my call:

import { openai } from '@ai-sdk/openai';
import { generateText, Output } from 'ai';
import { z } from 'zod';
const searchResultsSchema = z.object({
  results: z.array(z.object({
    url: z.string(),
    snippet: z.string()
  }))
});
const response = await generateText({
    model: openai.responses('gpt-4o-mini'),
    prompt: `${prompt}
    IMPORTANT: You must ALWAYS return this exact structure no matter what the tool returns.
    NEVER return raw markdown or the original tool response or empty strings.`,
    tools: {
      serpapi_search: /* the tool definition above  */
    },
    tool_choice: { type: 'tool', toolName: 'serpapi_search' },
    experimental_output: Output.object({ schema: searchResultsSchema }),
    debug: true,
    maxToolRoundTrip: 2,
    onStepFinish({ text, toolCalls, toolResults, finishReason, usage }) {
      // Log the tool results
    }
});
        // Safely access results, handling null/undefined
        let results = response.experimental_output?.results || [];

Adding the following parts work:

    maxToolRoundTrip: 1,
    maxSteps: 2,
...
    experimental_prepareStep: async ({ model, stepNumber, maxSteps, steps }) => {
      if (stepNumber > 0) {
        return {
          experimental_activeTools: [],
        };
      }
    },

Is there a better way to control how many times a tool is called? what am I doing wrong?

AI SDK Version

  • ai: 4.3.16
  • @ai-sdk/openai: 1.3.22

eyaldar avatar May 21 '25 11:05 eyaldar

Hey, trying again, is this really the way to solve this issue?

eyaldar avatar Jun 15 '25 07:06 eyaldar

hi can you log any potential errors with onError? also what is returning an empty result, your tool or the text generation?

lgrammel avatar Jun 26 '25 06:06 lgrammel

Hey, I'll check the onError and get back to you. The tool returns responses correctly, but the text generation returns empty unless I block the tools from hogging all the allocated requests (like you can see in the shared code)

eyaldar avatar Jun 26 '25 07:06 eyaldar

I'm not sure where can I put the onError, I don't see it as an option in generateText, what am I doing wrong?

eyaldar avatar Jun 26 '25 09:06 eyaldar

Nevermind, was thinking this is stream text. So you are seeing no errors?

lgrammel avatar Jun 26 '25 09:06 lgrammel

No errors, toolsResult return the tool results, text result is empty and experimental_output crashes

eyaldar avatar Jun 26 '25 09:06 eyaldar

same issue here after I upgrade to v5. the result.content is tool call info. and result.text is empty

lili21 avatar Aug 06 '25 08:08 lili21

Same issue here after I upgrade to v5. Still no fix?

anriuss avatar Sep 05 '25 14:09 anriuss

Sorry that we didn't get to it. A lot changed since the issue was originally created.

@lili21 @anriuss could you or someone else provide a minimal code snippet that reproduces the problem with latest ai?

gr2m avatar Sep 08 '25 01:09 gr2m

Actually the code stays about the same... not a lot have changed in those part specifically (except for tool -> parameters to inputSchema):

import { tool, generateText, Output } from 'ai';
import dotenv from 'dotenv';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
dotenv.config();

const exampleTool = tool({
    description: ' "Use this tool to fetch real-time web data for queries that require the latest information, niche topics, local context, or when the answer may have changed since the model’s last update. This includes news, recent events, evolving technologies, up-to-date documentation, or anything likely not captured in training data. The tool supports issuing queries to a search engine and browsing the returned links to extract relevant content. ALWAYS PASS engine parameter with \'google\'',
    inputSchema: z.object({
        query: z.string()
    }),
    // Function to execute the search
    execute: async ({ query }) => {
        const results = [{
            "url": "https://www.google.com",
            "snippet": "Google is a search engine"
        }, {
            "url": "https://www.yahoo.com",
            "snippet": "Yahoo is a search engine"
        }]
        // Return in the format our app expects
        return JSON.stringify({ results });
    }
})

const searchResultsSchema = z.object({
  results: z.array(z.object({
    url: z.string(),
    snippet: z.string()
  }))
});

const prompt = `Find me a site that has a blog post about the latest news in the world`;

const response = await generateText({
    model: openai.responses('gpt-4o-mini'),
    prompt: `${prompt}
    IMPORTANT: You must ALWAYS return this exact structure no matter what the tool returns.
    NEVER return raw markdown or the original tool response or empty strings.`,
    tools: {
      exampleTool
    },
    tool_choice: { type: 'tool', toolName: 'exampleTool' },
    experimental_output: Output.object({ schema: searchResultsSchema }),
    debug: true,
    onStepFinish({ text, toolCalls, toolResults, finishReason, usage }) {
      // Log the tool results
    }
});
        // Safely access results, handling null/undefined
let results = response.experimental_output?.results || [];

console.log(results);

eyaldar avatar Sep 08 '25 11:09 eyaldar

use the stepCountIs. I had the same issue and it was fixed adding const result = await generateText({ model: openai("gpt-5"), stopWhen: stepCountIs(2), }); const { generateText, tool, stepCountIs } = await import("ai");

aurindo10 avatar Sep 08 '25 16:09 aurindo10

This works, but I don't understand why... why do I have to add stopWhen property and why isn't that documented?

eyaldar avatar Sep 08 '25 18:09 eyaldar

Wow, this is really weird. I had the same issue and why the heck is stopWhen: stepCountIs(2) solving this?

edit: do not forget to import stepCountIs from ai: import { generateText, stepCountIs } from 'ai';

FakieHeelflip avatar Sep 23 '25 08:09 FakieHeelflip

So the reason you need to add the stopWhen parameter is that the generateText and other primitive functions only execute in one step (i.e. one call)

See docs ref here

By adding that parameter, you essentially let the api be called twice, now with the toolResult. Which then results in obtaining final text.

Since this is documented and expected behaviour, marking this as closed

aayush-kapoor avatar Nov 13 '25 23:11 aayush-kapoor

Identify issue type

Identified as None
Confidence: High

Rationale

This is primarily a usage/support question resolved by configuring documented multi-step execution, not a confirmed bug or a request to change the product or documentation.

Evidence

  • The request forces a tool call, while generateText performs only one step by default, leaving no subsequent step to generate final text from the tool result.
  • Adding stopWhen: stepCountIs(2) enables the required second model call and resolves the behavior.
  • The closing comment identifies this as documented, expected multi-step tool-calling behavior rather than an SDK failure.

lgrammel avatar Jul 22 '26 15:07 lgrammel