Enforcing Schema Requirements in Prism Structured Output
The Problem
We encountered an issue within the devlog-ist/landing project where the Gemini AI model, when used with Prism, was not consistently returning a required field (mermaid_diagram) in its structured output. This inconsistency caused downstream processing errors and required a solution to ensure data integrity across all Prism schemas.
The Approach
To address this, we modified the Prism structured output schemas to enforce the inclusion of the mermaid_diagram field. This change guarantees that regardless of which schema is used (callPrism, generateFromPrompt, generateVlogPost), the output will always contain the necessary field.
Implementation
The solution involved updating the schema definitions to explicitly mark the mermaid_diagram field as required. This ensures that the AI model adheres to the defined structure and provides complete data. A simplified example of how this might be represented in PHP is shown below:
<?php
class PrismSchema
{
public string $schemaType;
public string $inputPrompt;
public string $outputFormat;
/**
* @required
*/
public string $mermaid_diagram;
}
?>
Key Insight
By enforcing schema requirements, we ensure data consistency and prevent errors caused by missing fields. This approach improves the reliability of our AI-driven processes and reduces the need for manual intervention.