| |

Using Pydantic with Gemini 2.5 Flash Preview 09-2025 on OpenRouter

This tutorial explains how to use a Pydantic schema to extract structured data using Gemini 2.5 Flash Preview 09-2025.

Please read this tutorial first to understand the basic setup

Code:

import os
import json
import requests
import datetime
from inputs import get_input_text
from system import system_instruction_1 as system_instruction
from schema import VAERSReport
from dotenv import load_dotenv
import time
load_dotenv()
model_name = 'google/gemini-2.5-flash-preview-09-2025'
api_key = os.getenv("OPENROUTER_API_KEY")

json_schema_text = json.dumps(VAERSReport.model_json_schema(), indent=2)
input_text, sentence_map = get_input_text()

prompt = f"""

Report: {input_text}

JSON Schema:
{json_schema_text}
"""
url = "https://openrouter.ai/api/v1/chat/completions"

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json",
    "HTTP-Referer": "https://botflo.com/",  # Optional. Site URL for rankings on openrouter.ai.
    "X-Title": "LLM Accuracy Comparisons for Structured Outputs",  # Optional
}

payload = {
    "model": model_name,
    "messages": [
        {"role": "system", "content": f'{system_instruction}'},
        {"role": "user", "content": f'{prompt}'}
    ],
    "reasoning": {
        "effort": "high"
    }

}
before = time.time()
response_full = requests.post(url, headers=headers, data=json.dumps(payload))
elapsed = time.time() - before

response = response_full.json()
metadata = {
    "metrics": {
        "elapsed": elapsed,
        "usage": response['usage']
    },
    "text": {
        "system_instruction": system_instruction,
        "sentence_map": sentence_map,
        "json_schema_text": json_schema_text
    }
}
response["metadata"] = metadata

response_file_name = f"responses/response_{model_name.replace('/', '-')}.json"
with open(response_file_name, 'w+') as f:
    json.dump(response, f, indent=2)

output_text = response.get('choices')[0].get('message').get('content')
print(output_text)

input_file_name = f"inputs/input_{model_name.replace('/', '-')}.txt"
with open(input_file_name, 'w+') as f:
    f.write(input_text)

output_file_name = f"outputs/output_{model_name.replace('/', '-')}.txt"
with open(output_file_name, 'w+') as f:
    f.write(output_text)

LLM Response:

```json
{
  "timeline": {
    "date_of_vaccination": {
      "year": 1999,
      "month": 6,
      "citations": [
        3
      ],
      "explanation": null
    },
    "regression_onset_date": {
      "year": 2000,
      "month": 10,
      "citations": [
        147
      ],
      "explanation": "Symptoms of developmental delay and autism began between 21-23 months. Calculation based on 22 months estimated using narrative timeline."
    },
    "diagnosis_date": {
      "year": 2001,
      "month": 1,
      "citations": [
        98,
        150
      ],
      "explanation": "Evaluation at the autism resource center occurred on 1/4/01, where PDD could not be ruled out, and the diagnosis of autism was reported as 'recent' in Feb 2001."
    },
    "age_at_vaccination_months": {
      "value": 15.96,
      "citations": [
        3
      ],
      "explanation": "Reported age is 1.33 years (1.33 * 12 = 15.96 months)."
    },
    "age_at_diagnosis_months": {
      "value": 25,
      "citations": [
        61,
        98
      ],
      "explanation": "The subject was 20 months on 8/22/00 [61]; the primary diagnostic evaluation occurred on 1/4/01, approximately 25 months later."
    }
  },
  "developmental_milestones": [
    {
      "milestone_name": {
        "value": "sitting_without_support",
        "citations": [
          41
        ],
        "explanation": null
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 8,
        "citations": [
          41
        ],
        "explanation": "Sat w/out support at 8 mts (or 6 months adjusted for prematurity)."
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": [
          40
        ],
        "explanation": "Motor milestones were delayed, but not reported lost."
      },
      "regression_start_date_in_months": {
        "value": 0,
        "citations": null,
        "explanation": null
      },
      "progressed_after_regression": {
        "value": false,
        "citations": null,
        "explanation": null
      }
    },
    {
      "milestone_name": {
        "value": "walking",
        "citations": [
          41
        ],
        "explanation": null
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 12,
        "citations": [
          41
        ],
        "explanation": "Walked by 12 mts (or 10 months adjusted for prematurity)."
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": null,
        "explanation": null
      },
      "regression_start_date_in_months": {
        "value": 0,
        "citations": null,
        "explanation": null
      },
      "progressed_after_regression": {
        "value": false,
        "citations": null,
        "explanation": null
      }
    },
    {
      "milestone_name": {
        "value": "single_words",
        "citations": [
          41
        ],
        "explanation": null
      },
      "milestone_type": "language",
      "milestone_age_in_months": {
        "value": 17,
        "citations": [
          41
        ],
        "explanation": "Started saying a few single words at around age 17 mts."
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [
          147,
          115
        ],
        "explanation": "Symptoms began 21-23 months, and the child lost flexible phrase speech and rarely used speech without prompting."
      },
      "regression_start_date_in_months": {
        "value": 22,
        "citations": [
          147
        ],
        "explanation": "Symptoms of developmental delay and autism began between 21-23 mts."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": null,
        "explanation": "Autism persisted as of the last update."
      }
    },
    {
      "milestone_name": {
        "value": "pretend_play",
        "citations": [
          121,
          123
        ],
        "explanation": null
      },
      "milestone_type": "social_communication",
      "milestone_age_in_months": {
        "value": null,
        "citations": [
          121
        ],
        "explanation": "Demonstrated some beginning pretend play skills prior to the recently noted decrease in this type of play."
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [
          123
        ],
        "explanation": "Type of play has decreased significantly in the past several wks (reported Jan 2001)."
      },
      "regression_start_date_in_months": {
        "value": 25,
        "citations": [
          123
        ],
        "explanation": "Loss was reported as happening 'in the past several wks' relative to the 1/4/01 evaluation (~25 months)."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": null,
        "explanation": "Autism persisted as of the last update."
      }
    }
  ],
  "regression_record": {
    "pre_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [
        41,
        62
      ],
      "explanation": "Child started single words at 17 months and was assessed at a 13-15 month language level at 20 months, prior to the major symptomatic onset at 21-23 months."
    },
    "pre_regression_social_communication_level": {
      "value": "reciprocal_interaction",
      "citations": [
        62
      ],
      "explanation": "Social development level was assessed at 18-20 months at 20 months of age, suggesting approaching age-appropriate interaction."
    },
    "pre_regression_adaptive_behavior_level": {
      "value": "moderate_independence",
      "citations": [
        41,
        75
      ],
      "explanation": "Gross motor was age equivalent (walking achieved at 12 months)."
    },
    "post_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [
        83,
        84,
        128
      ],
      "explanation": "Post-regression (25 months), language comprehension and expression were assessed at a 15-month level, characterized by grunting and occasional single words."
    },
    "post_regression_social_communication_level": {
      "value": "basic_engagement",
      "citations": [
        110,
        128
      ],
      "explanation": "Parents noted a decrease in good social behaviors (joint attention, gestures), with socialization age assessed around 14 months at 25 months old."
    },
    "post_regression_adaptive_behavior_level": {
      "value": "moderate_independence",
      "citations": [
        128,
        140
      ],
      "explanation": "Daily living skills were assessed at 15 months (at 25 months old), indicating stagnation or delay, not evident loss of previously acquired concrete adaptive skills."
    },
    "has_language_loss": {
      "value": true,
      "citations": [
        99,
        115,
        147
      ],
      "explanation": "Symptoms developed 21-23 months, and flexible phrase speech was lost/not achieved."
    },
    "has_social_loss": {
      "value": true,
      "citations": [
        110,
        111,
        123
      ],
      "explanation": "Parents reported decrease in frequency of joint attention/gestures and loss of complex play."
    },
    "has_adaptive_loss": {
      "value": false,
      "citations": [
        140
      ],
      "explanation": "Adaptive behaviors were noted as below average or delayed (stagnation), but acute loss of motor/self-care skills was not prominent."
    },
    "has_repetitive_behaviors": {
      "value": true,
      "citations": [
        126,
        148
      ],
      "explanation": "Began demonstrating new behaviors such as lining up toys and objects, and used language repetitively."
    }
  },
  "early_vaccination_symptoms": {
    "had_autism_features": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Autism symptoms began several months later (21-23 months of age)."
    },
    "had_fever": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_speech_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_irritability": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_eye_contact_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_diarrhea": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_seizures": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_lethargy": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_screaming": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_behavioral_changes": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_rash": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null
      
    },
    "had_head_banging": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_social_withdrawal": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_injection_site_reaction": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_balance_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_appetite_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_staring_spells": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_hand_flapping": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_toe_walking": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_gastrointestinal_problems": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_hyperactivity": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_ear_infections": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Otitis media occurred at 9 months of age, which is outside the acute 30-day window following the vaccination on 6/30/99 (when the child was 16 months old by VAERS data)."
    },
    "had_vomiting": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_sleep_problems": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    },
    "had_encephalopathy": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": null
    }
  },
  "diagnosis_record": {
    "asd_severity": {
      "value": 2,
      "citations": [
        151,
        153
      ],
      "explanation": "Rated in the 'moderate' autism category, though symptoms were described as 'mild' but interfering with learning."
    },
    "diagnosis_name": [
      {
        "value": "Autism",
        "citations": [
          21,
          45,
          150
        ],
        "explanation": null
      },
      {
        "value": "Mixed Expressive Receptive Language Disorder",
        "citations": [
          96,
          130
        ],
        "explanation": null
      }
    ],
    "diagnosing_professional": [
      {
        "value": "Psychologist/Autism Resource Center Staff",
        "citations": [
          98,
          150
        ],
        "explanation": "Diagnosed by the autism resource center."
      }
    ]
  },
  "intervention_record": {
    "interventions": [
      {
        "value": "speech_therapy",
        "citations": [
          76,
          97,
          154
        ],
        "explanation": null
      },
      {
        "value": "occupational_therapy",
        "citations": [
          143,
          154
        ],
        "explanation": null
      },
      {
        "value": "aba",
        "citations": [
          154
        ],
        "explanation": "Recommended intensive early intervention using applied behavioral analysis (ABA)."
      },
      {
        "value": "other",
        "citations": [
          79,
          97
        ],
        "explanation": "Special instruction was recommended."
      }
    ],
    "is_intervention_ongoing": {
      "value": true,
      "citations": [
        154
      ],
      "explanation": "Continued therapies were recommended."
    },
    "has_recovery": {
      "value": false,
      "citations": [
        11,
        162
      ],
      "explanation": "Autism persisted as of 15July02."
    }
  },
  "birth_record": {
    "was_normal_pre_vaccination": {
      "value": false,
      "citations": [
        16,
        24,
        40
      ],
      "explanation": "Child was premature (31.5 wks), had existing conditions (apnea, bradycardia), and motor milestones were noted as not w/in normal limits."
    },
    "apgar_score": [
      {
        "value": "8",
        "citations": [
          29
        ],
        "explanation": "At one minute"
      },
      {
        "value": "9",
        "citations": [
          29
        ],
        "explanation": "At five minutes"
      }
    ]
  },
  "behavior_record": {
    "repetitive_behaviors": [
      {
        "value": "Lining up toys and objects (e.g., blocks, chairs)",
        "citations": [
          126
        ],
        "explanation": "A new behavior reported to have begun recently prior to 1/4/01."
      },
      {
        "value": "Repetitive use of language",
        "citations": [
          148
        ],
        "explanation": "Symptom listed as observed alongside developmental delay."
      }
    ]
  },
  "heavy_metal_tests": [
    {
      "metal_type": "lead",
      "value": 16.0,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ],
      "explanation": "Absolute concentration was within reference range (0-79)."
    },
    {
      "metal_type": "arsenic",
      "value": 60.0,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ],
      "explanation": "Absolute concentration was within reference range (0-99)."
    },
    {
      "metal_type": "mercury",
      "value": 10.0,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ],
      "explanation": "Value was reported as 'less than 10ug/L', which is within the reference range (0-19.9). Using 10.0 as approximate upper bound."
    }
  ],
  "comorbidities": [
    {
      "value": "asthma",
      "citations": [
        155
      ],
      "explanation": "Diagnosed with reactive airway disease post-regression."
    },
    {
      "value": "other",
      "citations": [
        16,
        135
      ],
      "explanation": "Repeated ear infections (Otitis media)."
    },
    {
      "value": "other",
      "citations": [
        155
      ],
      "explanation": "Pneumonia."
    }
  ],
  "environmental_exposures": [],
  "legal_matter": {
    "value": true,
    "citations": [
      22
    ],
    "explanation": "This report was received as part of litigation proceedings."
  },
  "diagnosis_reporting_delay_reason": null
}
```

Metrics

{
  "elapsed": 69.27981805801392,
  "usage": {
    "prompt_tokens": 15225,
    "completion_tokens": 18324,
    "total_tokens": 33549,
    "prompt_tokens_details": {
      "cached_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 13294,
      "image_tokens": 0
    }
  }
}

Leave a Reply