Explicit Formatting Constraints for Laravel AI SDK Structured Output

📖 2 minutes read

When using Laravel AI SDK’s structured output, you might notice the AI produces different formatting styles across multiple runs—even with the same prompt. One run might return markdown with **bold** and bullet lists, while another returns plain text.

Why This Happens

Large language models interpret prompts probabilistically. Without explicit constraints, the AI makes formatting decisions based on context and training data patterns. This leads to output variance that’s hard to predict.

The Solution: Explicit Format Constraints

Add formatting instructions directly in your schema field descriptions:

use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Ai\Contracts\HasStructuredOutput;

class ReportGenerator implements HasStructuredOutput
{
    public function schema(JsonSchema $schema): array
    {
        return [
            'summary' => $schema->string()
                ->description('
                    Write the summary naturally.
                    
                    Format: One paragraph per section.
                    Plain text only - no markdown or formatting.
                    Use newlines for structure.
                    
                    Express times in 24-hour format (0600, 1400, 2000).
                ')
                ->required(),
        ];
    }
}

Key Constraints to Specify

Format type:

  • “Plain text only – no markdown”
  • “Use markdown formatting”
  • “Return as HTML”

Structure:

  • “Use newlines for structure”
  • “One item per line”
  • “Separate sections with double newlines”

Consistency rules:

  • “Express times in HHMM format”
  • “Use sentence case for headings”
  • “No bullet points or numbered lists”

Real-World Example

In a data extraction system, the schema description evolved from generic guidance to explicit constraints:

// Before (inconsistent)
'schedule' => $schema->string()
    ->description('Extract the itinerary from the source.')
    ->required(),

// After (consistent)
'schedule' => $schema->string()
    ->description('
        Extract the itinerary from the source.
        
        Format: List each day with activities one per line.
        Show time (HHMM) and description.
        Plain text only - no markdown or formatting.
        Use newlines for structure.
    ')
    ->required(),

The Result

After adding explicit formatting constraints:

  • Output became consistent across multiple runs
  • No more surprise markdown in plain-text fields
  • Easier to parse and display in templates

When to Use This

Always specify format constraints when:

  • Output will be displayed directly to users
  • You’re parsing the output programmatically
  • Consistency matters more than creativity
  • Multiple AI runs need identical formatting

You can be more lenient when:

  • The AI is generating creative content
  • Formatting flexibility is desired
  • You’re post-processing the output anyway

Bottom Line

Don’t assume the AI will infer your formatting preferences. Explicit beats implicit—especially when dealing with probabilistic systems. Two sentences in your schema description can save hours of debugging inconsistent output.

Daryle De Silva

VP of Technology

11+ years building and scaling web applications. Writing about what I learn in the trenches.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *