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

Vercel AI SDK响应转流失败(429状态码)问题排查求助

解决Vercel AI聊天机器人429错误问题

问题描述

按照教程使用Vercel AI构建简易聊天机器人,代码与示例完全一致,OpenAI密钥有效,但发送消息后终端报错:

error node_modules\ai\dist\index.mjs (110:10) @ AIStream
Failed to convert the response to stream. Received status code: 429

附代码

page.tsx

"use client";

import { useChat } from "ai/react";

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat();

  return (
    <div className="mx-auto w-full max-w-md py-24 flex flex-col stretch">
      {messages.length > 0
        ? messages.map((m) => (
            <div key={m.id}>
              {m.role === "user" ? "User: " : "AI: "}
              {m.content}
            </div>
          ))
        : null}

      <form onSubmit={handleSubmit}>
        <input
          className="fixed w-full max-w-md bottom-0 border border-gray-300 rounded mb-8 shadow-xl p-2"
          value={input}
          placeholder="Say something..."
          onChange={handleInputChange}
        />
      </form>
    </div>
  );
}

route.ts

import { Configuration, OpenAIApi } from "openai-edge";
import { OpenAIStream, StreamingTextResponse } from "ai";

const config = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);

export const runtime = "edge";

export async function POST(req: Request) {
  const { messages } = await req.json();

  const response = await openai.createChatCompletion({
    model: "gpt-3.5-turbo",
    stream: true,
    messages,
  });

  const stream = OpenAIStream(response);
  return new StreamingTextResponse(stream);
}

错误原因

  • 429状态码代表请求超限,说明你的OpenAI API调用频率或token用量超过了当前账户的配额限制,和代码逻辑无关
  • 即使密钥有效,免费账户的速率限制(比如gpt-3.5-turbo每分钟最多3次请求、40000 tokens)很容易触发该错误

解决办法

  1. 检查账户配额:登录OpenAI后台查看API使用情况,确认是否超出速率限制或额度
  2. 等待配额重置:免费账户的速率限制通常每分钟重置一次,可暂停操作等待后重试
  3. 升级账户:若频繁触发限制,可升级到付费账户提高请求速率和token额度
  4. 前端防重复提交:在page.tsx中利用isLoading状态禁用输入框,避免短时间内重复发送请求:
    "use client";
    
    import { useChat } from "ai/react";
    
    export default function Chat() {
      const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat();
    
      return (
        <div className="mx-auto w-full max-w-md py-24 flex flex-col stretch">
          {messages.length > 0
            ? messages.map((m) => (
                <div key={m.id}>
                  {m.role === "user" ? "User: " : "AI: "}
                  {m.content}
                </div>
              ))
            : null}
    
          <form onSubmit={handleSubmit}>
            <input
              className="fixed w-full max-w-md bottom-0 border border-gray-300 rounded mb-8 shadow-xl p-2"
              value={input}
              placeholder="Say something..."
              onChange={handleInputChange}
              disabled={isLoading}
            />
          </form>
        </div>
      );
    }
    
  5. 后端错误捕获:在route.ts中添加错误处理,返回更友好的提示:
    import { Configuration, OpenAIApi } from "openai-edge";
    import { OpenAIStream, StreamingTextResponse } from "ai";
    
    const config = new Configuration({
      apiKey: process.env.OPENAI_API_KEY,
    });
    const openai = new OpenAIApi(config);
    
    export const runtime = "edge";
    
    export async function POST(req: Request) {
      try {
        const { messages } = await req.json();
    
        const response = await openai.createChatCompletion({
          model: "gpt-3.5-turbo",
          stream: true,
          messages,
        });
    
        if (response.status === 429) {
          return new Response("请求过于频繁,请稍后再试", { status: 429 });
        }
    
        const stream = OpenAIStream(response);
        return new StreamingTextResponse(stream);
      } catch (error) {
        return new Response("服务器错误", { status: 500 });
      }
    }
    

内容的提问来源于stack exchange,提问作者Bob

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 03:17:14