Next.js Discord

Discord Forum

FUNCTION_INVOCATION_FAILED

Unanswered
New Guinea Freshwater Crocodile posted this in #help-forum
Open in Discord
Avatar
New Guinea Freshwater CrocodileOP
I have a new project with just 1 folder and 1 api endpoint. My plan is to host it on the edge.

This is the code below in the api/chat.ts

import { systemPrompt } from '@/constants/SystemPrompt';
import TarrotDeck from '@/constants/TarrotDeck';
import { openai } from '@ai-sdk/openai';
import { streamText, tool } from 'ai';
import { z } from 'zod';

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

    const result = streamText({
        model: openai("gpt-4o-mini"),
        system: systemPrompt({
            firstName: data.firstName,
            birthDate: data.dateOfBirth,
            birthLocation: data.locationOfBirth,
        }),
        messages: data.messages,
        tools: {
            /**
             * Pulls a random card from the tarot deck
             */
            tarotDeckCard: tool({
                description: "Pulls a random card from the tarot deck. Always use this tool if a question requires a card.",
                parameters: z.object({}),
                execute: async ({ }) => {
                    const cardNumber = Math.floor(Math.random() * TarrotDeck.length);
                    const randomCard = TarrotDeck[cardNumber];
                    return randomCard;
                },
            }),
        },
    });

    return result.toDataStreamResponse();
}


Everytime I try using it (locally running vercel dev) I get NO_RESPONSE_FROM_FUNCTION error

33 Replies

Avatar
New Guinea Freshwater CrocodileOP
This is my first time using edge stuff here, any help would be super appreciaated
Been stuck for a couple hours
Avatar
How long does the function take? Can you time to locally (not on edge)
Avatar
New Guinea Freshwater CrocodileOP
pretty much instantly fails @Arinji
or I get this error;
Error: Cannot read properties of undefined (reading 'startsWith')
Error: Function `api/chat.ts` failed with exit code 1
after simplifying the code to just this;
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
console.log(process.env.OPENAI_API_KEY)

export async function POST(req: Request) {
    console.log(process.env.OPENAI_API_KEY)
    const { messages } = await req.json();
    console.log("messages", messages)

    const result = streamText({
      model: openai('gpt-4o'),
      messages,
    });
  
    return result.toDataStreamResponse();
}
the e.g code is from vercerl ai sdk docs
If i make a new next app and use my code in app/api/chat/route.ts <--- the same code works fine
Avatar
So your code dosent work locally @New Guinea Freshwater Crocodile
Avatar
New Guinea Freshwater CrocodileOP
i havent tested it deployed but it works fine locally if I put the same code in a nextjs app api route instead of a new project with an api folder
Avatar
Wait.. what
One sec lemme read your issue again
Avatar
New Guinea Freshwater CrocodileOP
yeah thats the problem, i cant figure it out. If I create a new pnpm project (typescript), setup vercel.json and an api folder with the same code, it throws errors
Avatar
Wait I'm so dumb
I didn't read your question correctly
Why is your filename chat.ts
Avatar
New Guinea Freshwater CrocodileOP
api/chat.ts in the api project, in the nextjs api its route.ts
Avatar
I pretty much skipped over that part for some reason lmao
Avatar
New Guinea Freshwater CrocodileOP
ahaha lmao
Avatar
New Guinea Freshwater CrocodileOP
correct
its in a new project with just an api folder
in nextjs application, teh same code works fine
Avatar
Oh so you aren't using nextjs? This is like running just vercel edge right?
Avatar
New Guinea Freshwater CrocodileOP
yeah correct and the code is fine because it works fine inside a nextjs application
if i copy and paste the same code in a brand new nextjs application, no issues
Image
Package.json
{
  "name": "orra-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^22.10.5",
    "tsx": "^4.19.2",
    "typescript": "^5.7.2"
  },
  "dependencies": {
    "@ai-sdk/openai": "^1.0.13",
    "ai": "^4.0.27",
    "dotenv": "^16.4.7",
    "zod": "^3.24.1"
  }
}
tsconfig.json
Image
Avatar
Ok so
Sorry imma have to run it locally to see what's broken
Not sure why it works on nextjs but not vercel edge