|

Using Pydantic with GPT-5 Codex on OpenRouter

This tutorial explains how to use a Pydantic schema to extract structured data using GPT-5 Codex.

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 = 'openai/gpt-5-codex'
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:

{
  "timeline": {
    "date_of_vaccination": {
      "year": 1999,
      "month": 6,
      "citations": [
        3,
        42
      ],
      "explanation": "Engerix-B dose administered on 30 June 1999."
    },
    "regression_onset_date": {
      "year": 2000,
      "month": null,
      "citations": [
        45,
        147,
        148
      ],
      "explanation": "Neurologic/autism symptoms were first alleged in the spring of 2000, approximately 21–23 months after birth."
    },
    "diagnosis_date": {
      "year": 2001,
      "month": 1,
      "citations": [
        98,
        150
      ],
      "explanation": "He was evaluated for autism at an autism resource center on 4 January 2001 and was subsequently described as diagnosed with autism."
    },
    "age_at_vaccination_months": {
      "value": null,
      "citations": null,
      "explanation": "Age at the 30 June 1999 vaccination was not explicitly reported."
    },
    "age_at_diagnosis_months": {
      "value": null,
      "citations": null,
      "explanation": "Exact age in months at diagnosis was not provided."
    }
  },
  "developmental_milestones": [
    {
      "milestone_name": {
        "value": "sitting_without_support",
        "citations": [
          41
        ],
        "explanation": "He sat without support at 8 months (6 months adjusted for prematurity)."
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 8,
        "citations": [
          41
        ],
        "explanation": null
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": null,
        "explanation": "No regression was reported for this milestone."
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": null,
        "explanation": "No regression was reported."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": null,
        "explanation": "No regression occurred."
      }
    },
    {
      "milestone_name": {
        "value": "walking",
        "citations": [
          41
        ],
        "explanation": "He walked by 12 months of age (10 months adjusted for prematurity)."
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 12,
        "citations": [
          41
        ],
        "explanation": null
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": null,
        "explanation": "No regression was reported for this milestone."
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": null,
        "explanation": "No regression was reported."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": null,
        "explanation": "No regression occurred."
      }
    },
    {
      "milestone_name": {
        "value": "single_words",
        "citations": [
          41
        ],
        "explanation": "He started saying a few single words at around 17 months (15 months adjusted)."
      },
      "milestone_type": "language",
      "milestone_age_in_months": {
        "value": 17,
        "citations": [
          41
        ],
        "explanation": null
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": null,
        "explanation": "Loss of this milestone was not described."
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": null,
        "explanation": "No regression was reported."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": null,
        "explanation": "No regression occurred."
      }
    }
  ],
  "regression_record": {
    "pre_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [
        41
      ],
      "explanation": "Before regression he was producing a few single words."
    },
    "pre_regression_social_communication_level": {
      "value": "reciprocal_interaction",
      "citations": [
        88,
        103,
        108,
        109
      ],
      "explanation": "He responded well to social interaction, shared interests, and greeted his father."
    },
    "pre_regression_adaptive_behavior_level": {
      "value": "moderate_independence",
      "citations": [
        56,
        57,
        58,
        71,
        75
      ],
      "explanation": "Developmental clinic visits showed near age-appropriate fine, gross motor, and self-care skills before later concerns emerged."
    },
    "post_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [
        83,
        84,
        85,
        114,
        115
      ],
      "explanation": "After regression he continued to use occasional single words with mild receptive and expressive delays."
    },
    "post_regression_social_communication_level": {
      "value": "basic_engagement",
      "citations": [
        110,
        111,
        112,
        126,
        127,
        128,
        132
      ],
      "explanation": "Parents reported decreased social behaviors and evaluators noted borderline adaptive social scores, though he still engaged at a basic level."
    },
    "post_regression_adaptive_behavior_level": {
      "value": "basic_self_help",
      "citations": [
        68,
        69,
        139,
        140,
        141,
        142,
        127,
        128
      ],
      "explanation": "Self-care skills were slightly below average with sensory defensiveness and borderline adaptive scores after regression."
    },
    "has_language_loss": {
      "value": false,
      "citations": [
        83,
        84,
        85,
        114,
        115
      ],
      "explanation": "He continued to use single words rather than losing speech altogether."
    },
    "has_social_loss": {
      "value": true,
      "citations": [
        110,
        111,
        132
      ],
      "explanation": "Parents observed a decline in previously seen social behaviors and interest in peers."
    },
    "has_adaptive_loss": {
      "value": false,
      "citations": [
        139,
        140,
        141,
        142
      ],
      "explanation": "Adaptive difficulties were described as mild delays rather than a clear loss of previously acquired skills."
    },
    "has_repetitive_behaviors": {
      "value": true,
      "citations": [
        126,
        148,
        151,
        152,
        153
      ],
      "explanation": "He began lining up toys and demonstrated autism-related repetitive behaviors."
    }
  },
  "early_vaccination_symptoms": {
    "had_autism_features": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        45,
        147,
        148
      ],
      "explanation": "Autistic features were first alleged in spring 2000, more than 30 days after vaccination."
    },
    "had_fever": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "No fever within 30 days of vaccination was reported."
    },
    "had_speech_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        83,
        84,
        85,
        114,
        115,
        147,
        148
      ],
      "explanation": "Speech delays emerged months later; there was no loss reported within 30 days of vaccination."
    },
    "had_irritability": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Irritability was not described within 30 days of vaccination."
    },
    "had_eye_contact_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        108,
        109,
        110,
        111
      ],
      "explanation": "Concerns about joint attention decreased months later; no eye-contact loss was noted within 30 days of vaccination."
    },
    "had_diarrhea": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Diarrhea was not reported after vaccination."
    },
    "had_seizures": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "No seizures were reported."
    },
    "had_lethargy": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Lethargy within 30 days of vaccination was not described."
    },
    "had_screaming": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "No abnormal screaming was reported within 30 days of vaccination."
    },
    "had_behavioral_changes": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        147,
        148
      ],
      "explanation": "Behavioral changes were first noted many months after vaccination."
    },
    "had_rash": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "No rash within 30 days of vaccination was mentioned."
    },
    "had_head_banging": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Head banging or self-injury was not reported."
    },
    "had_social_withdrawal": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        110,
        111,
        147,
        148
      ],
      "explanation": "Social withdrawal appeared months after vaccination, not within the first 30 days."
    },
    "had_injection_site_reaction": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "No injection-site reaction was documented."
    },
    "had_balance_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        50
      ],
      "explanation": "Flat feet were observed later but no acute balance loss within 30 days of vaccination."
    },
    "had_appetite_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        69
      ],
      "explanation": "Feeding concerns arose much later; no appetite loss within 30 days of vaccination was reported."
    },
    "had_staring_spells": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Staring spells were not described."
    },
    "had_hand_flapping": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Hand flapping was not reported."
    },
    "had_toe_walking": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        50
      ],
      "explanation": "Tibial torsion was noted later, but toe walking within 30 days of vaccination was not reported."
    },
    "had_gastrointestinal_problems": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        32
      ],
      "explanation": "Feeding intolerance and reflux were neonatal issues predating the vaccination; no new GI problems within 30 days afterward were recorded."
    },
    "had_hyperactivity": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Hyperactivity was not mentioned."
    },
    "had_ear_infections": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [
        39
      ],
      "explanation": "An otitis media episode occurred at 9 months, outside the 30-day window."
    },
    "had_vomiting": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Vomiting was not reported."
    },
    "had_sleep_problems": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "Sleep problems within 30 days of vaccination were not described."
    },
    "had_encephalopathy": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "There was no report of encephalopathy within 30 days of vaccination."
    }
  },
  "diagnosis_record": {
    "asd_severity": {
      "value": 2,
      "citations": [
        151,
        152,
        153
      ],
      "explanation": "Autism Treatment Evaluation Checklist scores placed him in the moderate autism category."
    },
    "diagnosis_name": [
      {
        "value": "Autism",
        "citations": [
          21,
          45,
          150,
          151,
          152,
          153
        ],
        "explanation": null
      },
      {
        "value": "Mixed receptive-expressive language disorder",
        "citations": [
          96,
          129,
          130,
          131
        ],
        "explanation": null
      }
    ],
    "diagnosing_professional": [
      {
        "value": "Autism resource center psychologist",
        "citations": [
          98,
          99,
          100,
          129,
          130,
          131,
          150
        ],
        "explanation": null
      },
      {
        "value": "Early intervention psychologist",
        "citations": [
          87,
          88,
          94,
          96,
          97
        ],
        "explanation": null
      }
    ]
  },
  "intervention_record": {
    "interventions": [
      {
        "value": "speech_therapy",
        "citations": [
          76,
          77,
          78,
          85,
          149,
          154
        ],
        "explanation": "Speech therapy was recommended and initiated to address communication delays."
      },
      {
        "value": "occupational_therapy",
        "citations": [
          134,
          138,
          143,
          154
        ],
        "explanation": "Occupational therapy was recommended for fine motor and sensory processing concerns."
      },
      {
        "value": "aba",
        "citations": [
          154
        ],
        "explanation": "Intensive early intervention using applied behavioral analysis was recommended."
      }
    ],
    "is_intervention_ongoing": {
      "value": false,
      "citations": null,
      "explanation": "The record does not document whether therapies were ongoing at last follow-up."
    },
    "has_recovery": {
      "value": false,
      "citations": [
        160,
        161,
        162
      ],
      "explanation": "Follow-up reports stated that autism persisted."
    }
  },
  "birth_record": {
    "was_normal_pre_vaccination": {
      "value": false,
      "citations": [
        24,
        32,
        40
      ],
      "explanation": "He was a premature infant with multiple neonatal complications, and motor milestones were already outside normal limits."
    },
    "apgar_score": [
      {
        "value": "Apgar 1 minute: 8",
        "citations": [
          29
        ],
        "explanation": null
      },
      {
        "value": "Apgar 5 minutes: 9",
        "citations": [
          29
        ],
        "explanation": null
      }
    ]
  },
  "behavior_record": {
    "repetitive_behaviors": [
      {
        "value": "Lined up toys and other objects rather than engaging in varied play",
        "citations": [
          126,
          148
        ],
        "explanation": null
      },
      {
        "value": "Used language repetitively",
        "citations": [
          148
        ],
        "explanation": null
      }
    ]
  },
  "heavy_metal_tests": [
    {
      "metal_type": "lead",
      "value": 16,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ]
    },
    {
      "metal_type": "arsenic",
      "value": 60,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [
        20
      ]
    }
  ],
  "comorbidities": [
    {
      "value": "asthma",
      "citations": [
        155
      ],
      "explanation": "Reactive airway disease was documented after the autism onset."
    }
  ],
  "environmental_exposures": [],
  "legal_matter": {
    "value": true,
    "citations": [
      22
    ],
    "explanation": "The report was received as part of litigation proceedings."
  },
  "diagnosis_reporting_delay_reason": null
}

Metrics

{
  "elapsed": 167.81387495994568,
  "usage": {
    "prompt_tokens": 13383,
    "completion_tokens": 13739,
    "total_tokens": 27122
  }
}

Leave a Reply