You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React调用Google Gemini API的generateContent报TypeError错误求助

解决Google Gemini API调用时的"request is not iterable"错误

错误原因

你调用generateContent时传入的参数格式不符合Gemini JavaScript SDK要求。当前代码中传入的{ prompt, generationConfig }不是SDK认可的参数结构——SDK要求参数为字符串类型prompt,或包含contents字段(可迭代的内容数组)的请求对象,而非直接将prompt作为顶级键。内部处理逻辑因找不到可迭代的内容结构,抛出了TypeError: request is not iterable错误。

修复方案

将所有generateContent的调用修改为以下两种正确格式之一:

方式一:直接传入prompt字符串+配置参数

const result = await model.generateContent(prompt, generationConfig);

方式二:使用规范的请求对象格式(适合多轮对话或复杂内容场景)

const result = await model.generateContent({
  contents: [{
    role: 'user',
    parts: [{ text: prompt }]
  }],
  generationConfig
});

修改后的完整代码

import {
    GoogleGenerativeAI,
    HarmCategory,
    HarmBlockThreshold,
} from "@google/generative-ai";

const apiKey = process.env.REACT_APP_GEN_AI_API_KEY;
const genAI = new GoogleGenerativeAI(apiKey);

const model = genAI.getGenerativeModel({
    model: "gemini-1.5-flash",
});

const generationConfig = {
    temperature: 1,
    topP: 0.95,
    topK: 64,
    maxOutputTokens: 8192,
    responseMimeType: "text/plain",
};

export async function run({ education, technologies, level, projects, firstCourse }) {
    try {
        console.log("Inputs for career suggestion:", { education, technologies, level, projects, firstCourse });

        // Career suggestion
        const prompt = `Education: ${education}, skills: ${technologies}, Level: ${level}, Projects_done: ${projects}. Based on my education, skills, and level, which career should I opt for ${firstCourse}? Just tell me one branch in one word.`;
        console.log("Prompt for career suggestion:", prompt);
        
        // 修改后的调用方式(方式一)
        const result = await model.generateContent(prompt, generationConfig);

        if (!result || typeof result.text !== 'string') {
            throw new Error("Invalid response from model for career suggestion");
        }

        const decision = result.text.trim() || "Unable to determine";

        // Best courses
        const prompt_2 = `${decision} was suggested by a friend. Give me the 5 best free courses with links to master this field, ordered from beginner to advanced. Only list 5 links, no description.`;
        console.log("Prompt for courses:", prompt_2);
        
        // 修改后的调用方式
        const result_2 = await model.generateContent(prompt_2, generationConfig);

        if (!result_2 || typeof result_2.text !== 'string') {
            throw new Error("Invalid response from model for courses");
        }

        const courseDisplay = result_2.text.trim() || "No courses found";

        //Trending projects
        const prompt_3 = `For a ${decision} career, suggest 5 trending projects to build my skills at ${level} level.`;
        console.log("Prompt for projects:", prompt_3);
        
        // 修改后的调用方式
        const result_3 = await model.generateContent(prompt_3, generationConfig);

        if (!result_3 || typeof result_3.text !== 'string') {
            throw new Error("Invalid response from model for projects");
        }

        const project = result_3.text.trim() || "No projects available";

        return { decision, courseDisplay, project };
    } catch (error) {
        console.error("Error generating content:", error);
        return { decision: "Error", courseDisplay: "Error fetching courses", project: "Error fetching projects" };
    }
}

 export default run;

内容的提问来源于stack exchange,提问作者Bharath kumar G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 23:30:55