|

Using Pydantic with xAI Grok 4 Fast on OpenRouter

This tutorial explains how to use a Pydantic schema to extract structured data using xAI Grok 4 Fast.

The code uses the free model, but you can just change the model name to the paid one once it becomes available.

Please read this tutorial first to understand the basic setup

Code:

import os
import json
import requests
import time
from inputs import get_input_text
from system import system_instruction_1 as system_instruction
from schema import VAERSReport
from dotenv import load_dotenv

load_dotenv()
model_name = 'x-ai/grok-4-fast:free'
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": [4, 42],
      "explanation": "Last vaccination date listed as 1999-06-30 including DTaP, Hep B, Hib, Rotavirus."
    },
    "regression_onset_date": {
      "year": 2000,
      "month": 10,
      "citations": [146, 147, 132],
      "explanation": "Regression reported to begin between 21-23 months of age, around fall 2000."
    },
    "diagnosis_date": {
      "year": 2001,
      "month": 1,
      "citations": [98, 150],
      "explanation": "Diagnosis evaluation at autism resource center in January 2001."
    },
    "age_at_vaccination_months": {
      "value": 6,
      "citations": [42],
      "explanation": "Approximate age at 1999-06-30 vaccination based on birth in early 1999."
    },
    "age_at_diagnosis_months": {
      "value": 24,
      "citations": [98],
      "explanation": "Diagnosis around 24 months in early 2001."
    }
  },
  "developmental_milestones": [
    {
      "milestone_name": {
        "value": "sitting_without_support",
        "citations": [41],
        "explanation": "Sat without support at 8 months."
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 8,
        "citations": [41],
        "explanation": "Achieved at 8 months (6 months adjusted)."
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": [41],
        "explanation": "No regression mentioned for motor milestone."
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": [],
        "explanation": "Not applicable."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [],
        "explanation": "Not applicable."
      }
    },
    {
      "milestone_name": {
        "value": "walking",
        "citations": [41],
        "explanation": "Walked by 12 months."
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 12,
        "citations": [41],
        "explanation": "Achieved at 12 months (10 months adjusted)."
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": [41],
        "explanation": "No regression mentioned for motor milestone."
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": [],
        "explanation": "Not applicable."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [],
        "explanation": "Not applicable."
      }
    },
    {
      "milestone_name": {
        "value": "single_words",
        "citations": [41],
        "explanation": "Started saying a few single words."
      },
      "milestone_type": "language",
      "milestone_age_in_months": {
        "value": 17,
        "citations": [41],
        "explanation": "Achieved at 17 months (15 months adjusted), but regressed later."
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [110, 115, 146],
        "explanation": "Language skills decreased after 18-21 months."
      },
      "regression_start_date_in_months": {
        "value": 21,
        "citations": [146],
        "explanation": "Regression began at 21-23 months."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [160],
        "explanation": "No progression reported; persisted."
      }
    },
    {
      "milestone_name": {
        "value": "eye_contact",
        "citations": [108, 116],
        "explanation": "Demonstrated eye contact during evaluation."
      },
      "milestone_type": "social_communication",
      "milestone_age_in_months": {
        "value": 24,
        "citations": [108],
        "explanation": "Observed at 24 months, but frequency decreased."
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [110],
        "explanation": "Good social behaviors like eye contact decreased in frequency."
      },
      "regression_start_date_in_months": {
        "value": 21,
        "citations": [110],
        "explanation": "Decrease noted after 21 months."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [160],
        "explanation": "No recovery reported."
      }
    },
    {
      "milestone_name": {
        "value": "joint_attention",
        "citations": [108, 110],
        "explanation": "Initiated sharing interests, but decreased."
      },
      "milestone_type": "social_communication",
      "milestone_age_in_months": {
        "value": 18,
        "citations": [99],
        "explanation": "Present until around 18 months."
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [110],
        "explanation": "Initiating joint attention decreased."
      },
      "regression_start_date_in_months": {
        "value": 21,
        "citations": [110],
        "explanation": "Decrease after 21 months."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [160],
        "explanation": "No recovery."
      }
    },
    {
      "milestone_name": {
        "value": "pretend_play",
        "citations": [121, 123],
        "explanation": "Demonstrated beginning pretend play, but decreased."
      },
      "milestone_type": "social_communication",
      "milestone_age_in_months": {
        "value": 24,
        "citations": [121],
        "explanation": "Observed at 24 months, then regressed."
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [123],
        "explanation": "Pretend play decreased significantly in past weeks."
      },
      "regression_start_date_in_months": {
        "value": 24,
        "citations": [123],
        "explanation": "Recent decrease at 24 months."
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [160],
        "explanation": "No recovery."
      }
    }
  ],
  "regression_record": {
    "pre_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [41, 94],
      "explanation": "Single words at 17 months before regression."
    },
    "pre_regression_social_communication_level": {
      "value": "basic_engagement",
      "citations": [54, 108],
      "explanation": "Appropriate social development early on, basic engagement."
    },
    "pre_regression_adaptive_behavior_level": {
      "value": "moderate_independence",
      "citations": [40, 53],
      "explanation": "Motor milestones delayed but moderate independence."
    },
    "post_regression_language_level": {
      "value": "no_speech",
      "citations": [84, 113],
      "explanation": "Limited verbalizations, grunting, occasional single words post-regression."
    },
    "post_regression_social_communication_level": {
      "value": "no_engagement",
      "citations": [104, 111],
      "explanation": "Decreased interest in playing with others, limited responses."
    },
    "post_regression_adaptive_behavior_level": {
      "value": "basic_self_help",
      "citations": [128],
      "explanation": "Borderline impaired adaptive behavior post-regression."
    },
    "has_language_loss": {
      "value": true,
      "citations": [110, 115, 146],
      "explanation": "Expressive language regressed, from single words to limited speech."
    },
    "has_social_loss": {
      "value": true,
      "citations": [110, 111],
      "explanation": "Social behaviors like joint attention and peer play decreased."
    },
    "has_adaptive_loss": {
      "value": true,
      "citations": [132],
      "explanation": "Developmental regression noted, possibly PDD."
    },
    "has_repetitive_behaviors": {
      "value": true,
      "citations": [126, 148],
      "explanation": "Lining up toys and repetitive language use emerged."
    }
  },
  "early_vaccination_symptoms": null,
  "diagnosis_record": {
    "asd_severity": {
      "value": 2,
      "citations": [151],
      "explanation": "Rated in moderate autism category."
    },
    "diagnosis_name": [
      {
        "value": "autism",
        "citations": [1, 45, 150],
        "explanation": "Diagnosed with autism."
      },
      {
        "value": "Mixed Expressive Receptive Language Disorder",
        "citations": [96, 130],
        "explanation": "DS-IV diagnosis, PDD not ruled out."
      }
    ],
    "diagnosing_professional": [
      {
        "value": "psychologist",
        "citations": [98, 130],
        "explanation": "Psychologist at autism resource center."
      },
      {
        "value": "autism trainer",
        "citations": [144, 150],
        "explanation": "Autism trainer confirmed diagnosis."
      }
    ]
  },
  "intervention_record": {
    "interventions": [
      {
        "value": "speech_therapy",
        "citations": [76, 86, 97, 149],
        "explanation": "Recommended and received speech therapy."
      },
      {
        "value": "occupational_therapy",
        "citations": [134, 143, 154],
        "explanation": "Recommended for fine motor and sensory issues."
      },
      {
        "value": "aba",
        "citations": [131, 154],
        "explanation": "Intensive early intervention using applied behavioral analysis recommended."
      },
      {
        "value": "social_skills",
        "citations": [77, 131],
        "explanation": "Structured interactions with peers and social skills focus."
      },
      {
        "value": "other",
        "citations": [79, 97],
        "explanation": "Special instruction for play skills and cognitive stimulation."
      }
    ],
    "is_intervention_ongoing": {
      "value": true,
      "citations": [149, 154],
      "explanation": "Interventions ongoing as of 2001 evaluations."
    },
    "has_recovery": {
      "value": false,
      "citations": [160, 162],
      "explanation": "Autism persisted as of July 2002."
    }
  },
  "birth_record": {
    "was_normal_pre_vaccination": {
      "value": false,
      "citations": [24, 30, 32, 33],
      "explanation": "Premature birth at 31.5 weeks with complications including respiratory distress, apnea, bradycardia, but neurobehavioral assessments appropriate for age."
    },
    "apgar_score": [
      {
        "value": "8 at 1 minute, 9 at 5 minutes",
        "citations": [29],
        "explanation": "Apgar scores reported."
      }
    ]
  },
  "behavior_record": {
    "repetitive_behaviors": [
      {
        "value": "lining up toys and objects",
        "citations": [126, 148],
        "explanation": "Recently began lining up toys, blocks, chairs."
      },
      {
        "value": "repetitive language use",
        "citations": [148],
        "explanation": "Used language repetitively."
      }
    ]
  },
  "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]
    },
    {
      "metal_type": "mercury",
      "value": 10,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [20]
    },
    {
      "metal_type": "lead",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "aluminum",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "arsenic",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "barium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "chromium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "copper",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "gallium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "manganese",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "nickel",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "platinum",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "rubidium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "selenium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "thallium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "tin",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "titanium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "tungsten",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "uranium",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    },
    {
      "metal_type": "zinc",
      "value": null,
      "unit": "ratio to creatinine",
      "is_elevated": true,
      "citations": [20]
    }
  ],
  "comorbidities": [
    {
      "value": "other",
      "citations": [32, 155],
      "explanation": "Otitis media, pharyngitis, pneumonia, reactive airway disease."
    },
    {
      "value": "allergies",
      "citations": [17],
      "explanation": "No allergies reported, but included as none specified; however, viral infection and feeding issues noted."
    }
  ],
  "environmental_exposures": [
    {
      "value": "other_mercury",
      "citations": [45, 161],
      "explanation": "Exposure to thimerosal and mercury in vaccines alleged to cause neurologic damage."
    }
  ],
  "legal_matter": {
    "value": true,
    "citations": [22, 47],
    "explanation": "Report received as part of litigation proceedings."
  },
  "diagnosis_reporting_delay_reason": {
    "value": "litigation proceedings",
    "citations": [22],
    "explanation": "Delay due to involvement in legal matter."
  }
}

Metrics

{
  "elapsed": 22.026559114456177,
  "usage": {
    "prompt_tokens": 13265,
    "completion_tokens": 5270,
    "total_tokens": 18535,
    "prompt_tokens_details": {
      "cached_tokens": 108,
      "audio_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 1401
    }
  }
}

Leave a Reply