Skip to main content

Overview

When logging requests that use structured outputs (JSON schemas), you need to include the schema configuration in the parameters field of your /log-request call. This allows PromptLayer to properly track and display your structured output configurations alongside your request history.

When to Use This

Use structured output logging when you’re:
  • Making API calls with JSON schema validation (OpenAI, Anthropic, Google, etc.)
  • Tracking requests that enforce specific response formats
  • Analyzing how different schema configurations affect model outputs
  • Building applications that require reliable, parseable JSON responses

Basic Structure

The structured output configuration goes in the parameters field of your log request, using the response_format key with json_schema configuration:

Complete Examples

OpenAI with Structured Outputs

Google Gemini with Structured Outputs

JavaScript/TypeScript Example

REST API Example

If you’re calling the /log-request endpoint directly via REST API:

Schema Configuration Details

Key Fields

  • name (required): A descriptive name for your schema (e.g., “Recipe”, “ContactInfo”)
  • description (optional): Explains what the schema represents
  • schema (required): The actual JSON schema definition following JSON Schema specification
  • strict (optional): When true, enforces strict validation (supported by some providers like OpenAI)

Schema Best Practices

  1. Use clear property names: Make your schema self-documenting
  2. Specify required fields: Use the required array to mark mandatory properties
  3. Set additionalProperties: false: Prevents unexpected fields in the response
  4. Use descriptive types: Leverage JSON Schema’s type system (string, number, array, object, boolean)
  5. Add validation constraints: Use minLength, maxLength, minimum, maximum, etc. when appropriate

Tracking Schema Variations

Use tags and metadata to track different schema versions:

Common Issues and Solutions

Issue: Schema not showing in PromptLayer dashboard

Solution: Ensure the response_format is nested correctly within the parameters field, not at the top level of your log request.

Issue: Provider rejects the schema

Solution: Different providers have different schema support levels:
  • OpenAI: Full support with strict: true mode
  • Anthropic: Supports basic JSON mode
  • Google: Supports schemas with some limitations
Check your provider’s documentation for specific schema requirements.

Issue: Response doesn’t match schema

Solution:
  1. Verify your schema is valid JSON Schema
  2. Test with strict: true if your provider supports it
  3. Check that your prompt clearly instructs the model about the expected format
  4. Review logged requests in PromptLayer to debug schema mismatches

See Also