Using Pydantic with Qwen3 VL 235B A22B Thinking on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using Qwen3 VL 235B A22B Thinking.
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 = 'qwen/qwen3-vl-235b-a22b-thinking'
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": [42, 43],
"explanation": "Vaccination date of 6/30/99 for Engerix B vaccine"
},
"regression_onset_date": {
"year": 1999,
"month": 12,
"citations": [147],
"explanation": "Report states 'Between 21-23 mts, [he] began to develop Sx's of developmental delay and autism'"
},
"diagnosis_date": {
"year": 2001,
"month": 1,
"citations": [98, 149],
"explanation": "Evaluation at autism resource center on 1/4/01 with autism diagnosis"
},
"age_at_vaccination_months": {
"value": 16,
"citations": [3, 42],
"explanation": "Age of 1.33 years equals approximately 16 months"
},
"age_at_diagnosis_months": {
"value": 24,
"citations": [98, 149],
"explanation": "Diagnosis at approximately 24 months of age based on timeline"
}
},
"developmental_milestones": [
{
"milestone_name": {
"value": "sitting_without_support",
"citations": [41],
"explanation": "Subject sat without support at 8 months (6 months adjusted for prematurity)"
},
"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": null,
"explanation": "No regression mentioned for this milestone"
},
"regression_start_date_in_months": {
"value": null,
"citations": null,
"explanation": "No regression occurred for this milestone"
},
"progressed_after_regression": {
"value": true,
"citations": null,
"explanation": "No regression occurred, so milestone continued to progress"
}
},
{
"milestone_name": {
"value": "walking",
"citations": [41],
"explanation": "Subject walked by 12 months (10 months adjusted)"
},
"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": null,
"explanation": "No regression mentioned for this milestone"
},
"regression_start_date_in_months": {
"value": null,
"citations": null,
"explanation": "No regression occurred for this milestone"
},
"progressed_after_regression": {
"value": true,
"citations": null,
"explanation": "No regression occurred, so milestone continued to progress"
}
},
{
"milestone_name": {
"value": "single_words",
"citations": [41],
"explanation": "Subject started saying a few single words at around age 17 months"
},
"milestone_type": "language",
"milestone_age_in_months": {
"value": 17,
"citations": [41],
"explanation": "Achieved at 17 months (15 months adjusted)"
},
"milestone_has_regressed": {
"value": true,
"citations": [118, 119, 120],
"explanation": "Exhibited unusual receptive language difficulties with confusion in understanding directions"
},
"regression_start_date_in_months": {
"value": 22,
"citations": [147],
"explanation": "Regression began between 21-23 months"
},
"progressed_after_regression": {
"value": false,
"citations": [152, 153],
"explanation": "Report states 'Sx's of autism, while mild, still interfere w/his ability to learn'"
}
},
{
"milestone_name": {
"value": "naming_objects",
"citations": [84, 85],
"explanation": "Subject scored at 15-month level for language using Preschool Language Scale"
},
"milestone_type": "language",
"milestone_age_in_months": {
"value": 20,
"citations": [84, 85],
"explanation": "Assessment at 20 months showing language skills at 15-month level"
},
"milestone_has_regressed": {
"value": true,
"citations": [118, 119, 120],
"explanation": "Exhibited unusual receptive language difficulties with confusion in understanding directions"
},
"regression_start_date_in_months": {
"value": 22,
"citations": [147],
"explanation": "Regression began between 21-23 months"
},
"progressed_after_regression": {
"value": false,
"citations": [152, 153],
"explanation": "Report states 'Sx's of autism, while mild, still interfere w/his ability to learn'"
}
}
],
"regression_record": {
"pre_regression_language_level": {
"value": "babbling_single_words",
"citations": [41, 53, 57, 62],
"explanation": "Subject was saying single words and had language skills appropriate for age before regression"
},
"pre_regression_social_communication_level": {
"value": "reciprocal_interaction",
"citations": [54, 58, 63],
"explanation": "Neonatologists consistently reported the subject was developing normally with appropriate social interactions"
},
"pre_regression_adaptive_behavior_level": {
"value": "age_appropriate",
"citations": [54, 58, 63],
"explanation": "Subject was described as 'doing well developmentally' before regression"
},
"post_regression_language_level": {
"value": "babbling_single_words",
"citations": [85, 94, 114, 115],
"explanation": "Mild delays in receptive and expressive language skills with use of single words only"
},
"post_regression_social_communication_level": {
"value": "basic_engagement",
"citations": [103, 104, 105, 106],
"explanation": "Limited social engagement with some awareness but poor interaction skills"
},
"post_regression_adaptive_behavior_level": {
"value": "basic_self_help",
"citations": [127, 128],
"explanation": "Subject's level of adaptive behavior was assessed as 'Borderline Impaired range'"
},
"has_language_loss": {
"value": true,
"citations": [118, 119, 120],
"explanation": "Reported 'unusual receptive language difficulties' with confusion in understanding directions"
},
"has_social_loss": {
"value": true,
"citations": [103, 104, 105, 106],
"explanation": "Parents reported decreased interest in playing with other children and limited social responses"
},
"has_adaptive_loss": {
"value": true,
"citations": [127, 128],
"explanation": "Adaptive behavior assessed as 'Borderline Impaired range' with specific domain scores below age level"
},
"has_repetitive_behaviors": {
"value": true,
"citations": [126, 148],
"explanation": "Reported 'lining up toys and objects' and repetitive language use"
}
},
"diagnosis_record": {
"asd_severity": {
"value": 2,
"citations": [151],
"explanation": "Rated in the 'moderate' autism category using Autism Treatment Evaluation Checklist"
},
"diagnosis_name": [
{
"value": "autism",
"citations": [45, 149],
"explanation": "Report states 'subject to a reasonable degree of medical certainty has suffered injury and is autistic'"
}
],
"diagnosing_professional": [
{
"value": "autism resource center",
"citations": [98],
"explanation": "Evaluation conducted at autism resource center of a tertiary care center"
}
]
},
"intervention_record": {
"interventions": [
{
"value": "speech_therapy",
"citations": [76, 86, 97],
"explanation": "Recommended and initiated for language delays"
},
{
"value": "occupational_therapy",
"citations": [143],
"explanation": "Recommended for fine motor delays and sensory processing issues"
},
{
"value": "aba",
"citations": [154],
"explanation": "Recommended as 'intensive early intervention using applied behavioral analysis'"
}
],
"is_intervention_ongoing": {
"value": true,
"citations": [159, 160, 162],
"explanation": "Autism persisted as of 15July02 with ongoing therapy"
},
"has_recovery": {
"value": false,
"citations": [159, 160, 162],
"explanation": "Most recent information indicated autism persisted as of 15July02"
}
},
"birth_record": {
"was_normal_pre_vaccination": {
"value": false,
"citations": [24, 25, 26, 27, 28, 29, 30, 31, 32, 33],
"explanation": "Subject was premature quadruplet with multiple complications including respiratory distress, apnea, bradycardia, jaundice, and feeding issues"
},
"apgar_score": {
"value": ["8 and 9"],
"citations": [29],
"explanation": "Aspar scores were 8 and 9 at one and five minutes, respectively"
}
},
"behavior_record": {
"repetitive_behaviors": [
{
"value": "lining up toys",
"citations": [126, 148],
"explanation": "Reported 'lining up toys and objects' and 'he used language repetitively'"
}
]
},
"legal_matter": {
"value": true,
"citations": [22],
"explanation": "This report was received as part of litigation proceedings"
},
"early_vaccination_symptoms": {
"had_autism_features": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "Autism symptoms developed much later (21-23 months), not within 30 days of vaccination"
},
"had_fever": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No fever symptoms reported within 30 days of vaccination"
},
"had_speech_loss": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No speech loss reported within 30 days of vaccination"
},
"had_irritability": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No irritability reported within 30 days of vaccination"
},
"had_eye_contact_loss": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No eye contact loss reported within 30 days of vaccination"
},
"had_diarrhea": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No diarrhea reported within 30 days of vaccination"
},
"had_seizures": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No seizures reported within 30 days of vaccination"
},
"had_lethargy": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No lethargy reported within 30 days of vaccination"
},
"had_screaming": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No screaming reported within 30 days of vaccination"
},
"had_behavioral_changes": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No behavioral changes reported within 30 days of vaccination"
},
"had_rash": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No rash reported within 30 days of vaccination"
},
"had_head_banging": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No head banging reported within 30 days of vaccination"
},
"had_social_withdrawal": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No social withdrawal reported within 30 days of vaccination"
},
"had_injection_site_reaction": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No injection site reaction reported within 30 days of vaccination"
},
"had_balance_loss": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No balance loss reported within 30 days of vaccination"
},
"had_appetite_loss": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No appetite loss reported within 30 days of vaccination"
},
"had_staring_spells": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No staring spells reported within 30 days of vaccination"
},
"had_hand_flapping": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No hand flapping reported within 30 days of vaccination"
},
"had_toe_walking": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No toe walking reported within 30 days of vaccination"
},
"had_gastrointestinal_problems": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No GI problems reported within 30 days of vaccination"
},
"had_hyperactivity": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No hyperactivity reported within 30 days of vaccination"
},
"had_ear_infections": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "Ear infections were reported later at 9 months, not within 30 days of vaccination"
},
"had_vomiting": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No vomiting reported within 30 days of vaccination"
},
"had_sleep_problems": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No sleep problems reported within 30 days of vaccination"
},
"had_encephalopathy": {
"value": false,
"ongoing": false,
"days_to_symptom_onset": null,
"citations": null,
"explanation": "No encephalopathy reported within 30 days of vaccination"
}
},
"heavy_metal_tests": null,
"comorbidities": [],
"environmental_exposures": [],
"diagnosis_reporting_delay_reason": null
}
Metrics
{
"elapsed": 272.68206000328064,
"usage": {
"prompt_tokens": 13921,
"completion_tokens": 8161,
"total_tokens": 22082,
"prompt_tokens_details": {
"cached_tokens": 0,
"audio_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 3996
}
}
}