Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Parsing Error in generateObject Function with Unterminated String #1921

Closed
Brayan233 opened this issue Jun 11, 2024 · 5 comments
Closed

Comments

@Brayan233
Copy link

Brayan233 commented Jun 11, 2024

Description

Steps to Reproduce

  1. Set up the environment with Node.js and necessary dependencies.
  2. Use the generateObject function with the demo code : https://sdk.vercel.ai/docs/reference/ai-sdk-core/generate-object#generateobject
  3. Run the script.

Code example

import { openai } from '@ai-sdk/openai';
import { generateObject } from 'ai';
import { z } from 'zod';
import dotenv from 'dotenv';

dotenv.config();

const { object } = await generateObject({
  model: openai('gpt-4o'),
  mode: "json",
  max_tokens: 1000000000,
  temperature: 0.5,
  schema: z.object({
    recipe: z.object({
      name: z.string(),
      ingredients: z.array(z.string()),
      steps: z.array(z.string()),
    }),
  }),
  prompt: 'Generate a lasagna recipe.',
  log: true,
});

console.log(JSON.stringify(object, null, 2));

Additional context

Actual error :

The function returns an unterminated string error at position 166 in the JSON data :

JSONParseError [AI_JSONParseError]: JSON parsing failed: Text: {
  "recipe": {
    "name": "Classic Lasagna",
    "ingredients": [
      "1 pound ground beef",
      "1 onion, chopped",
      "2 cloves garlic, minced",
      "1 (.
Error message: Unterminated string in JSON at position 166

Possible Cause

The model might include characters like quotation marks or parentheses that are not correctly escaped, leading to parsing errors. This could be an issue with how the model's output is formatted or escaped before parsing into JSON.

@lgrammel
Copy link
Collaborator

You have increased the temperature setting, leading to more random output, and are using a model that's weaker for tool calls (gpt-4o) in JSON mode. This is expected to happen, because the model might produce invalid JSON output in such a setting.

@pruett
Copy link

pruett commented Jun 15, 2024

I tried this example with gpt-3.5-turbo and a temp of 0, and still receiving AI_JSONParseError. Even the example copy/pasted from https://sdk.vercel.ai/examples/next-app/basics/generating-object produces something similar.

Is this expected?

@lgrammel
Copy link
Collaborator

Please try without mode: json (then it uses tool calling w/ the OpenAI provider, which is often more robust). For better tool calling results, check out the prompt engineering with tools tips.

@pruett
Copy link

pruett commented Jun 15, 2024

Thanks for the reply. No difference for me, even if I use generateText, for example. The error message is odd, too:

  cause: JSONParseError [AI_JSONParseError]: JSON parsing failed: Text: �webpack/lib/util/registerExternalSerializer�webpack-sources/RawSource�__webpack_require__.r(__webpack_exports__);

Clearly something is off given that it's parsing webpack 😅. All I'm trying is:

export async function GET(request: NextRequest) {
  // imports...

  const { text } = await generateText({
    model: openai("gpt-3.5-turbo"),
    prompt: "tell me a good joke.",
    temperature: 0,
  });
  
  console.log({ text });
  
  return new NextResponse();
}

I'm simply running this code within an API route. I must be doing something incorrectly. Just not sure what.

@pruett
Copy link

pruett commented Jun 15, 2024

Problem solved ✅

Solution: Changing the API route from a GET to a POST

I'm guessing there were weird caching issues from marking the route as a GET that goes away when switching to a POST. Hope that this helps anyone that might stumble across this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants