| |

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

This tutorial explains how to use a Pydantic schema to extract structured data using Gemini 2.5 Flash Lite 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-lite-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": "Vaccination date listed for multiple vaccines administered includes 1999-06-30"
    },
    "regression_onset_date": {
      "year": 2000,
      "month": 1,
      "citations": [
        146,
        3,
        42,
        43
      ],
      "explanation": "Regression onset cited between 21-23 months [146]. Based on age 15.96 months at vaccination (6/30/99) [3], onset occurred approximately 6 months later in January 2000."
    },
    "diagnosis_date": {
      "year": 2001,
      "month": 1,
      "citations": [
        98
      ],
      "explanation": "Evaluation conducted on 1/4/01 where PDD concern was primary focus."
    },
    "age_at_vaccination_months": {
      "value": 15.96,
      "citations": [
        3
      ],
      "explanation": "Age listed as 1.33 years [3], calculated as 15.96 months."
    },
    "age_at_diagnosis_months": {
      "value": 21,
      "citations": [
        99
      ],
      "explanation": "Parents became concerned about language delay around 18 months [99]. Regression onset around 21-23 months [146]."
    }
  },
  "developmental_milestones": [
    {
      "milestone_name": {
        "value": "sitting_without_support",
        "citations": [
          41
        ],
        "explanation": "Subject sat without support at 8 months (6 adjusted for prematurity)"
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 8,
        "citations": [
          41
        ],
        "explanation": "Sat without support at 8 months (6 adjusted)"
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [
          110,
          132
        ],
        "explanation": "Developmental regression noted"
      },
      "regression_start_date_in_months": {
        "value": 22,
        "citations": [
          146
        ],
        "explanation": "Regression onset between 21-23 months"
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [
          162
        ],
        "explanation": "Autism persisted as of July 2002, no mention of regression reversal."
      }
    },
    {
      "milestone_name": {
        "value": "walking",
        "citations": [
          41
        ],
        "explanation": "Walked by 12 months (10 adjusted)"
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 12,
        "citations": [
          41
        ],
        "explanation": "Walked by 12 months (10 adjusted)"
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [
          110,
          132
        ],
        "explanation": "Developmental regression noted"
      },
      "regression_start_date_in_months": {
        "value": 22,
        "citations": [
          146
        ],
        "explanation": "Regression onset between 21-23 months"
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [
          162
        ],
        "explanation": "Autism persisted as of July 2002, no mention of regression reversal."
      }
    },
    {
      "milestone_name": {
        "value": "single_words",
        "citations": [
          41
        ],
        "explanation": "Started saying a few single words at around age 17 months (15 adjusted)"
      },
      "milestone_type": "language",
      "milestone_age_in_months": {
        "value": 17,
        "citations": [
          41
        ],
        "explanation": "Reported starting single words around 17 months (15 adjusted)"
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [
          99,
          114
        ],
        "explanation": "Language appeared delayed at 18 months [99], later engaged in limited verbalizations, mostly single word approximations [114]"
      },
      "regression_start_date_in_months": {
        "value": 22,
        "citations": [
          146
        ],
        "explanation": "Regression onset between 21-23 months"
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [
          160
        ],
        "explanation": "Autism persisted as of 9/30/02"
      }
    }
  ],
  "regression_record": {
    "pre_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [
        41
      ],
      "explanation": "Reported developing single words around 17 months prior to regression onset."
    },
    "pre_regression_social_communication_level": {
      "value": "reciprocal_interaction",
      "citations": [
        54,
        63
      ],
      "explanation": "General assessments suggested the child was doing well developmentally prior to noticeable delays."
    },
    "pre_regression_adaptive_behavior_level": {
      "value": "moderate_independence",
      "citations": [
        41
      ],
      "explanation": "Achieved major gross motor milestones (walking) by 12 months (10 adjusted)"
    },
    "post_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [
        84,
        94
      ],
      "explanation": "Language scores were assessed around 15 month equivalent [84] or 12 month developmental age [94] after regression onset."
    },
    "post_regression_social_communication_level": {
      "value": "basic_engagement",
      "citations": [
        95
      ],
      "explanation": "Socialization scored at 10 month level on Vineland [95]."
    },
    "post_regression_adaptive_behavior_level": {
      "value": "basic_self_help",
      "citations": [
        95
      ],
      "explanation": "Daily living skills scored at 12 month level on Vineland [95]."
    },
    "has_language_loss": {
      "value": true,
      "citations": [
        99,
        114
      ],
      "explanation": "Concerns about lack of speech started around 18 months, showing regression in flexible phrase speech [99, 114]."
    },
    "has_social_loss": {
      "value": true,
      "citations": [
        110,
        111
      ],
      "explanation": "Parents reported good social behaviors (joint attention, gestures) no longer happening frequently, and decreased interest in playing with others [110, 111]."
    },
    "has_adaptive_loss": {
      "value": true,
      "citations": [
        127
      ],
      "explanation": "Overall adaptive behavior assessed as Borderline Impaired range relative to age."
    },
    "has_repetitive_behaviors": {
      "value": true,
      "citations": [
        126,
        148
      ],
      "explanation": "Began demonstrating lining up toys and objects shortly before contact, and repetitive language use was noted [126, 148]."
    }
  },
  "early_vaccination_symptoms": {
    "had_autism_features": {
      "value": false,
      "ongoing": false,
      "citations": null,
      "explanation": "Autism features/regression began 21-23 months after vaccination, not within 30 days."
    },
    "had_fever": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_speech_loss": {
      "value": false,
      "ongoing": false,
      "citations": null,
      "explanation": "Language delays emerged much later (around 18 months)."
    },
    "had_irritability": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_eye_contact_loss": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_diarrhea": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_seizures": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_lethargy": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_screaming": {
      "value": false,
      "ongoing": false,
      "citations": null,
      "explanation": "Screaming due to frustration noted later [82], not acute post-vaccination."
    },
    "had_behavioral_changes": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_rash": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_head_banging": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_social_withdrawal": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_injection_site_reaction": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_balance_loss": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_appetite_loss": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_staring_spells": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_hand_flapping": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_toe_walking": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_gastrointestinal_problems": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_hyperactivity": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_ear_infections": {
      "value": false,
      "ongoing": false,
      "citations": null,
      "explanation": "Otitis media occurred at 9 months, outside the 30-day window."
    },
    "had_vomiting": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_sleep_problems": {
      "value": false,
      "ongoing": false,
      "citations": null
    },
    "had_encephalopathy": {
      "value": false,
      "ongoing": false,
      "citations": null
    }
  },
  "diagnosis_record": {
    "asd_severity": {
      "value": 2,
      "citations": [
        151
      ],
      "explanation": "Autism Treatment Evaluation Checklist rated subject in the 'moderate' autism category."
    },
    "diagnosis_name": [
      {
        "value": "Autism",
        "citations": [
          21,
          150
        ],
        "explanation": "Report conclusion states subject is autistic"
      },
      {
        "value": "Mixed Expressive Receptive Language Disorder",
        "citations": [
          96
        ],
        "explanation": "Psychologist's Dx on 10/9/00"
      },
      {
        "value": "Pervasive Developmental Disorder (rule out)",
        "citations": [
          130
        ],
        "explanation": "PDD could not be ruled out with the present evaluation."
      }
    ],
    "diagnosing_professional": [
      {
        "value": "Psychologist",
        "citations": [
          96,
          98
        ],
        "explanation": "Evaluations conducted by psychologists"
      },
      {
        "value": "Autism Trainer",
        "citations": [
          144
        ],
        "explanation": "Evaluated by an autism trainer"
      }
    ]
  },
  "intervention_record": {
    "interventions": [
      {
        "value": "speech_therapy",
        "citations": [
          86,
          154
        ],
        "explanation": "Speech therapy strongly recommended [86] and recommended later [154]"
      },
      {
        "value": "occupational_therapy",
        "citations": [
          143,
          154
        ],
        "explanation": "Occupational therapy was recommended [143] and later mentioned in recommendations [154]"
      },
      {
        "value": "aba",
        "citations": [
          154
        ],
        "explanation": "Intensive early intervention using applied behavioral analysis recommended [154]"
      },
      {
        "value": "social_skills",
        "citations": [
          131,
          131
        ],
        "explanation": "Structured interactions w/typical peers [131] recommended to address social skills."
      }
    ],
    "is_intervention_ongoing": {
      "value": true,
      "citations": [
        152,
        154
      ],
      "explanation": "Intensive intervention recommended to overcome delays."
    },
    "has_recovery": {
      "value": false,
      "citations": [
        162
      ],
      "explanation": "Autism persisted as of 15July02."
    }
  },
  "birth_record": {
    "was_normal_pre_vaccination": {
      "value": true,
      "citations": [
        33
      ],
      "explanation": "Neurobehavioral assessments were appropriate for age throughout hospitalization, suggesting normal development immediately post-birth and early infancy."
    },
    "apgar_score": [
      {
        "value": "8 at one minute",
        "citations": [
          29
        ],
        "explanation": "Aspar scores were 8 and 9 at one and five minutes, respectively."
      },
      {
        "value": "9 at five minutes",
        "citations": [
          29
        ],
        "explanation": "Aspar scores were 8 and 9 at one and five minutes, respectively."
      }
    ]
  },
  "behavior_record": {
    "repetitive_behaviors": [
      {
        "value": true,
        "citations": [
          126
        ],
        "explanation": "Subject recently began demonstrating lining up toys and objects",
        "ongoing": true
      }
    ]
  },
  "heavy_metal_tests": [
    {
      "metal_type": "lead",
      "value": 16.0,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ]
    },
    {
      "metal_type": "arsenic",
      "value": 60.0,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ]
    },
    {
      "metal_type": "mercury",
      "value": 10.0,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ]
    }
  ],
  "comorbidities": [
    {
      "value": "other",
      "citations": [
        16
      ],
      "explanation": "Includes various preexisting conditions like bradycardia neonatal, feeding intolerance, GERD, anemia, otitis media, pharyngitis, thrombocytopenia, and viral infection."
    }
  ],
  "environmental_exposures": [
    {
      "value": "unknown",
      "citations": [
        45
      ],
      "explanation": "Allegation relates to mercury exposure via vaccines, but no confirmed environmental source matching the schema required types was identified."
    }
  ],
  "legal_matter": {
    "value": true,
    "citations": [
      22
    ],
    "explanation": "Report received as part of litigation proceedings."
  },
  "diagnosis_reporting_delay_reason": null
}
```

Metrics

{
  "elapsed": 34.612513065338135,
  "usage": {
    "prompt_tokens": 15393,
    "completion_tokens": 12205,
    "total_tokens": 27598,
    "prompt_tokens_details": {
      "cached_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 7969,
      "image_tokens": 0
    }
  }
}

Leave a Reply