Skip to content

Dart: structured output can fail with unterminated JSON string #4880

@jeffdh5

Description

@jeffdh5

Summary

When using Dart Genkit structured output (outputSchema) with Gemini, generation can fail with:

FormatException: Failed to parse extracted JSON: FormatException: Unterminated string

I can reproduce this with schema-constrained output (recipe object) where one or more string fields are long. I think it's because there are multiple chunks and somethings there is no marker in the middle chunks.

Minimal repro

import 'package:genkit/genkit.dart';
import 'package:genkit_google_genai/genkit_google_genai.dart';
import 'package:schemantic/schemantic.dart';

part 'main.g.dart';

@Schema()
abstract class $Recipe {
  String get title;
  List<String> get ingredients;
  List<String> get steps;
  int get prepTimeMinutes;
}

void main() async {
  final ai = Genkit(plugins: [googleAI()]);

  await ai.generate(
    model: googleAI.gemini('gemini-2.5-flash'),
    prompt:
        'Generate a recipe for chocolate chip cookies with detailed ingredient strings.',
    outputSchema: Recipe.$schema,
  );
}

Observed

Fails with an unterminated JSON parse error, e.g.:

GenkitException: Google AI Error: FormatException: Failed to parse extracted JSON:
FormatException: Unterminated string (at character 167)

Suspected area

extractJson() in Dart core uses lastIndexOf('}') to identify JSON end during parsing of accumulated streamed text. That can incorrectly slice partial JSON and lead to unterminated string errors.

File reference from local pub cache:

  • genkit/lib/src/extract.dart

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions