Skip to content

feat(@built-in-ai/transformers-js): generateObject support #65

@jpabbuehl

Description

@jpabbuehl

Thanks for your work. it's really works well for many use cases, except one I'm struggling with

Problem Description

When using TransformersJS models (via @built-in-ai/transformers-js or the custom HuggingFace provider), the models report that they support generateObject functionality, but attempting to use structured output generation with JSON schemas fails. This causes issues in applications that rely on checking provider.generateObject availability to determine whether to use structured or text-based generation.

Current Behavior

  1. The provider/model reports generateObject is available (truthy check passes)
  2. When calling generateObject with a Zod schema, the SDK warns: "JSON response format is not supported by TransformersJS"
  3. The generation fails with NoObjectGeneratedError: No object generated: could not parse the response
  4. The model returns plain text instead of structured JSON

Expected Behavior

Either:

  • A) TransformersJS models should properly support structured output with JSON schemas, OR
  • B) The provider should not expose generateObject method if it's not actually supported

Reproduction Steps

import { generateObject } from 'ai';
import { transformersJS } from '@built-in-ai/transformers-js';
import { z } from 'zod';

const model = transformersJS('HuggingFaceTB/SmolLM2-360M-Instruct', {
  device: 'cpu'
});

// This check passes, suggesting structured output is supported
if (model.generateObject) {
  console.log('✅ Model claims to support generateObject');
}

// But this fails
const result = await generateObject({
  model,
  schema: z.object({
    feedback: z.enum(['Positive', 'Neutral', 'Negative']),
  }),
  prompt: 'Label this customer review: There is something wrong with your product that...'
});
// ❌ Throws: NoObjectGeneratedError

Error Output

AI SDK Warning: The "responseFormat" setting is not supported by this model - JSON response format is not supported by TransformersJS

NoObjectGeneratedError [AI_NoObjectGeneratedError]: No object generated: could not parse the response.
  cause: JSONParseError [AI_JSONParseError]: JSON parsing failed: Text: Feedback: Ne...
  Error message: Unexpected token 'F', "Feedback: Ne"... is not valid JSON

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions