Skip to content

[bug] JSON parsing fails with Anthropic when combining long prompt + complex schema #71

@csells

Description

@csells

When using Agent.sendFor() with Anthropic's Claude models, JSON parsing fails with FormatException: No JSON output found in response when both conditions are met:

  1. A long/complex prompt (~4000+ characters)
  2. A complex nested JSON schema (arrays of objects containing arrays)

Either condition alone works fine. The combination fails consistently.

Environment

  • dartantic_ai version: 1.3.0
  • Dart SDK: 3.10.0
  • Model: anthropic:claude-opus-4-5

Minimal Reproduction

/// Bug: dartantic_ai JSON parsing fails with Anthropic
/// when combining long prompt + complex nested schema.
///
/// Run: dart run bug_repro.dart
/// Requires: ANTHROPIC_API_KEY environment variable

import 'package:dartantic_ai/dartantic_ai.dart';
import 'package:json_schema/json_schema.dart';

void main() async {
  final agent = Agent('anthropic:claude-opus-4-5');

  // Complex nested schema (array of objects with nested arrays)
  final schema = JsonSchema.create({
    'type': 'object',
    'properties': {
      'name': {'type': 'string'},
      'items': {
        'type': 'array',
        'items': {
          'type': 'object',
          'properties': {
            'id': {'type': 'string'},
            'steps': {'type': 'array', 'items': {'type': 'string'}},
          },
          'required': ['id', 'steps'],
        },
      },
    },
    'required': ['name', 'items'],
  });

  // Short prompt - WORKS
  print('Test 1: Short prompt + complex schema');
  try {
    final result = await agent.sendFor<Map<String, dynamic>>(
      'List 2 files for a hello world app with steps for each.',
      outputSchema: schema,
    );
    print('  ✅ Success: ${result.output['name']}\n');
  } on Exception catch (e) {
    print('  ❌ Error: $e\n');
  }

  // Long prompt (~4000 chars) - FAILS
  final longPrompt = '''
Create a Flutter todo app with these detailed requirements:

## Core Features
- Add todos with text input, Enter key support, validation
- Toggle completion with checkbox, strikethrough, opacity animation
- Delete with swipe gesture, red background, undo snackbar
- Empty state with icon and friendly message

## UI Design
- Gradient background (light blue to purple)
- Rounded corners (16px), soft shadows
- Google Fonts: Poppins for headings, Inter for body
- Color scheme: Primary #4F46E5, Secondary #8B5CF6, Background #F8FAFC
- Translucent app bar with task count badge
- Floating card input with animated add button
- Card-based todo items with custom animated checkbox

## Architecture
- lib/main.dart - App entry point
- lib/models/todo.dart - Data model with id, title, isCompleted, createdAt
- lib/screens/home_screen.dart - Main screen
- lib/widgets/todo_input.dart - Input widget
- lib/widgets/todo_item.dart - Todo item widget
- lib/widgets/empty_state.dart - Empty state widget
- lib/theme/app_theme.dart - Theme configuration

## Technical Requirements
- google_fonts: ^6.1.0 dependency
- StatefulWidget with setState for state management
- Proper TextEditingController disposal
- AnimatedContainer and AnimatedOpacity for transitions
- Responsive max-width 600px layout

${'Additional detailed specification content. ' * 80}

List the files needed with implementation steps for each.
''';

  print('Test 2: Long prompt (~${longPrompt.length} chars) + complex schema');
  try {
    final result = await agent.sendFor<Map<String, dynamic>>(
      longPrompt,
      outputSchema: schema,
    );
    print('  ✅ Success: ${result.output['name']}\n');
  } on Exception catch (e) {
    print('  ❌ Error: $e\n');
  }
}

Steps to Reproduce

  1. Set ANTHROPIC_API_KEY environment variable
  2. Add dartantic_ai: ^1.3.0 and json_schema: ^5.2.1 to pubspec.yaml
  3. Run dart run bug_repro.dart

Expected Behavior

Both tests should succeed, returning parsed JSON matching the schema.

Actual Behavior

Test 1: Short prompt + complex schema
  ✅ Success: Hello World App

Test 2: Long prompt (~4217 chars) + complex schema
  ❌ Error: FormatException: No JSON output found in response. Expected JSON in response.output.

Notes

  • The same long prompt works with a simple flat schema
  • The same complex schema works with a short prompt
  • The issue is specific to the combination of long prompt + complex schema
  • This suggests the JSON extraction logic may have issues when Anthropic returns large structured responses
  • Google (google:gemini-3-pro-preview) works fine with the same prompt/schema combination

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