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

Groq provider streamObject fails with AI_TypeValidationError and doesn't produce results #4047

Open
e-simpson opened this issue Dec 9, 2024 · 3 comments
Labels
ai/provider bug Something isn't working

Comments

@e-simpson
Copy link

Description

Unable to use streaming object functionality with the Groq provider 1.0.7 on AI SDK 4.0.13.

Uncaught (in promise) AI_TypeValidationError: Type validation failed: Value: undefined.
Error message: [
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "undefined",
    "path": [],
    "message": "Required"
  }
]
    at _TypeValidationError.wrap (type-validation-error.ts:49:9)
    at safeValidateTypes (validate-types.ts:63:34)
    at Object.validateFinalResult (output-strategy.ts:86:12)
    at Object.transform (stream-object.ts:802:38)Caused by: ZodError: [
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "undefined",
    "path": [],
    "message": "Required"
  }
]
    at get error (index.mjs:587:31)
    at Object.validate (schema.ts:81:45)
    at safeValidateTypes (validate-types.ts:55:30)
    at Object.validateFinalResult (output-strategy.ts:86:12)
    at Object.transform (stream-object.ts:802:38)

Code example

import { createGroq } from '@ai-sdk/groq';
import { streamObject } from 'ai';
import { z } from 'zod';

const groq = createGroq();

const { partialObjectStream, object, usage } = streamObject({
    model: groq('llama-3.3-70b-versatile'),
    schema: z.object({
        recipe: z.object({
            name: z.string(),
            ingredients: z.array(z.string()),
            steps: z.array(z.string()),
        }),
     }),
     prompt: 'Generate a lasagna recipe.'
});

for await (const partialObject of partialObjectStream) {
     console.log("streaming", partialObject, partialObjectStream);
}
console.log("full object", object, usage);

AI provider

@ai-sdk/groq v1.0.7

Additional context

GenerateObject works fine.

I tried using the 1.0.7 openai provider and the Groq openai API as the baseURL, and found that I can get streamObject to produce the result, but no streaming behaviour.

Tests:

  • groq provider w/ streamObject & llama-3.3-70b-versatile model -> ERROR
  • groq provider w/ generateObject & llama-3.3-70b-versatile model -> WORKS
  • openai provider w/ streamObject & llama-3.3-70b-versatile model -> WORKS but no streaming behaviour
  • openai provider w/ generateObject & llama-3.3-70b-versatile model -> WORKS
@e-simpson e-simpson added the bug Something isn't working label Dec 9, 2024
@e-simpson
Copy link
Author

The groq provider page lists that tool streaming is supported but I'm not sure it is. I can't even seem to get it working on the Groq typescript SDK itself. Non-streaming works fine there too.

@lgrammel
Copy link
Collaborator

Can you try with

model: groq('llama-3.1-70b-versatile')

I just checked with llama-3.3 vs 3.1, and only 3.1 works. Currently unclear if it's a groq model limitation or a provider issue.

@lgrammel
Copy link
Collaborator

lgrammel commented Dec 10, 2024

The explanation for the observed behavior:

  • generateObject / groq -> uses groq structured outputs (option only available for non-streaming use per groq api)
  • generateObject / openai -> uses tool call
  • streamObject / groq -> no structured output enforcements leads to breakage; recommend prompt engineering
  • streamObject / openai -> uses tool call (but tool call is not streamed)

You can enable tool mode on the groq provider with mode: 'tool':

  const result = streamObject({
    model: groq('llama-3.3-70b-versatile'),
    mode: 'tool',
    // ...
   })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/provider bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants