|

Using Pydantic with Qwen3 VL 235B A22B Instruct on OpenRouter

This tutorial explains how to use a Pydantic schema to extract structured data using Qwen3 VL 235B A22B Instruct.

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-instruct'
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],
      "explanation": "Vaccinated with Engerix B on 6/30/99"
    },
    "regression_onset_date": {
      "year": 2000,
      "month": 3,
      "citations": [45],
      "explanation": "In the spring of 2000, subject suffered neurologic damage and was diagnosed with autism"
    },
    "diagnosis_date": {
      "year": 2001,
      "month": 1,
      "citations": [98],
      "explanation": "Evaluated at autism resource center on 1/4/01 for possible Pervasive Developmental Disorder"
    },
    "age_at_vaccination_months": {
      "value": 16.0,
      "citations": [42],
      "explanation": "Vaccinated on 6/30/99 at age 1.33 years (16 months)"
    },
    "age_at_diagnosis_months": {
      "value": 22,
      "citations": [98],
      "explanation": "Diagnosed at 22 months of age (1/4/01, born 31.5 weeks gestation)"
    }
  },
  "developmental_milestones": [
    {
      "milestone_name": {
        "value": "sitting_without_support",
        "citations": [41],
        "explanation": "Sat without support at 8 months (6 months adjusted for prematurity)"
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 8,
        "citations": [41],
        "explanation": "Sat without support at 8 months"
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": [41],
        "explanation": "No mention of regression for sitting milestone"
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": null,
        "explanation": "No regression reported for sitting milestone"
      },
      "progressed_after_regression": {
        "value": true,
        "citations": [41],
        "explanation": "Milestone was achieved and no regression reported"
      }
    },
    {
      "milestone_name": {
        "value": "walking",
        "citations": [41],
        "explanation": "Walked by 12 months (10 months adjusted)"
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 12,
        "citations": [41],
        "explanation": "Walked by 12 months"
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": [41],
        "explanation": "No mention of regression for walking milestone"
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": null,
        "explanation": "No regression reported for walking milestone"
      },
      "progressed_after_regression": {
        "value": true,
        "citations": [41],
        "explanation": "Milestone was achieved and no regression reported"
      }
    },
    {
      "milestone_name": {
        "value": "single_words",
        "citations": [41],
        "explanation": "Started saying a few single words at around age 17 months (15 months adjusted)"
      },
      "milestone_type": "language",
      "milestone_age_in_months": {
        "value": 17,
        "citations": [41],
        "explanation": "Started saying single words at 17 months"
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [146, 147],
        "explanation": "Mother reported developmental delay and autism symptoms between 21-23 months"
      },
      "regression_start_date_in_months": {
        "value": 21,
        "citations": [146, 147],
        "explanation": "Regression began between 21-23 months"
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [146, 147],
        "explanation": "No progression after regression, continued to show language delays"
      }
    },
    {
      "milestone_name": {
        "value": "pretend_play",
        "citations": [121, 122, 123],
        "explanation": "Demonstrated beginning pretend play skills during evaluation, but parents reported this type of play decreased significantly"
      },
      "milestone_type": "social_communication",
      "milestone_age_in_months": {
        "value": 20,
        "citations": [121, 122],
        "explanation": "Demonstrated pretend play at 20 months during evaluation"
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [123],
        "explanation": "Parents reported pretend play decreased significantly in the past several weeks"
      },
      "regression_start_date_in_months": {
        "value": 20,
        "citations": [123],
        "explanation": "Regression noted around 20 months when pretend play decreased"
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [123],
        "explanation": "No progression after regression, play skills decreased"
      }
    },
    {
      "milestone_name": {
        "value": "joint_attention",
        "citations": [108, 109, 110],
        "explanation": "Responded to name and shared interests with examiner, but parents reported good social behaviors decreased"
      },
      "milestone_type": "social_communication",
      "milestone_age_in_months": {
        "value": 20,
        "citations": [108, 109],
        "explanation": "Demonstrated joint attention at 20 months during evaluation"
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [110],
        "explanation": "Parents reported good social behaviors (including joint attention) decreased"
      },
      "regression_start_date_in_months": {
        "value": 20,
        "citations": [110],
        "explanation": "Regression noted around 20 months when social behaviors decreased"
      },
      "progressed_after_regression": {
        "value": false,
        "citations": [110],
        "explanation": "No progression after regression, social behaviors decreased"
      }
    }
  ],
  "regression_record": {
    "pre_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [41, 83],
      "explanation": "Started saying single words at 17 months, characterized by grunting and screaming with occasional single words"
    },
    "pre_regression_social_communication_level": {
      "value": "reciprocal_interaction",
      "citations": [108, 109],
      "explanation": "Responded to name and initiated sharing interests with examiner at 20 months"
    },
    "pre_regression_adaptive_behavior_level": {
      "value": "basic_self_help",
      "citations": [94, 95],
      "explanation": "Bayley Scales showed social developmental age of 10 months, Vineland showed daily living skills at 12 months"
    },
    "post_regression_language_level": {
      "value": "babbling_single_words",
      "citations": [83, 114, 115],
      "explanation": "Continued to use single words and word approximations after regression, limited verbalizations"
    },
    "post_regression_social_communication_level": {
      "value": "basic_engagement",
      "citations": [108, 109, 110, 113],
      "explanation": "Continued to respond to name and show some social behaviors, but parents reported decreased frequency and limited responses"
    },
    "post_regression_adaptive_behavior_level": {
      "value": "basic_self_help",
      "citations": [128],
      "explanation": "Adaptive behavior assessed as borderline impaired, with domain scores of 15 months for communication and daily living skills"
    },
    "has_language_loss": {
      "value": true,
      "citations": [146, 147],
      "explanation": "Mother reported language difficulties and developmental delay between 21-23 months"
    },
    "has_social_loss": {
      "value": true,
      "citations": [110, 111],
      "explanation": "Parents reported decreased social behaviors and interest in playing with other children"
    },
    "has_adaptive_loss": {
      "value": true,
      "citations": [127, 128],
      "explanation": "Adaptive behavior assessed as borderline impaired at 20 months"
    },
    "has_repetitive_behaviors": {
      "value": true,
      "citations": [126, 148],
      "explanation": "Parents reported lining up toys and objects, which was noted as a new behavior after regression"
    }
  },
  "early_vaccination_symptoms": {
    "had_autism_features": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [45],
      "explanation": "Autism features reported in spring 2000, not within 30 days of vaccination"
    },
    "had_fever": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": null,
      "explanation": "No fever reported within 30 days of vaccination"
    },
    "had_speech_loss": {
      "value": false,
      "ongoing": false,
      "days_to_symptom_onset": null,
      "citations": [41],
      "explanation": "Speech development began at 17 months, no 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": [108],
      "explanation": "Eye contact was present during evaluation at 20 months, no 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": true,
      "ongoing": true,
      "days_to_symptom_onset": null,
      "citations": [82, 83],
      "explanation": "Subject displayed frustration by screaming, characterized by grunting and screaming with occasional single words"
    },
    "had_behavioral_changes": {
      "value": true,
      "ongoing": true,
      "days_to_symptom_onset": null,
      "citations": [70, 82],
      "explanation": "Mother concerned about aggressive behaviors and socialization skills, subject displayed frustration by screaming"
    },
    "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": [103, 104],
      "explanation": "Subject was initially timid but became overbearing once familiar with environment, no withdrawal reported within 30 days"
    },
    "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": [50],
      "explanation": "Flat footed noted at 15 months, but no balance loss reported within 30 days of vaccination"
    },
    "had_appetite_loss": {
      "value": true,
      "ongoing": true,
      "days_to_symptom_onset": null,
      "citations": [69, 70],
      "explanation": "Mother concerned about subject not eating foods well, noted as a developmental delay"
    },
    "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": [50],
      "explanation": "Flat footed noted, but no toe walking reported within 30 days of vaccination"
    },
    "had_gastrointestinal_problems": {
      "value": true,
      "ongoing": true,
      "days_to_symptom_onset": null,
      "citations": [31, 32],
      "explanation": "Experienced feeding intolerance and gastroesophageal reflux during hospitalization"
    },
    "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": true,
      "ongoing": true,
      "days_to_symptom_onset": null,
      "citations": [39, 135],
      "explanation": "Experienced otitis media at 9 months and repeated ear infections later"
    },
    "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"
    }
  },
  "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": [150],
        "explanation": "Diagnosed with autism by autism resource center"
      },
      {
        "value": "Mixed Expressive Receptive Language Disorder",
        "citations": [130],
        "explanation": "Diagnosed with Mixed Expressive Receptive Language Disorder"
      }
    ],
    "diagnosing_professional": [
      {
        "value": "psychologist",
        "citations": [98],
        "explanation": "Evaluated by psychologist at autism resource center"
      },
      {
        "value": "autism trainer",
        "citations": [144],
        "explanation": "Evaluated by autism trainer on 2/13/01"
      }
    ]
  },
  "intervention_record": {
    "interventions": [
      {
        "value": "speech_therapy",
        "citations": [76, 86, 97, 154],
        "explanation": "Speech therapy recommended and initiated"
      },
      {
        "value": "occupational_therapy",
        "citations": [143],
        "explanation": "Occupational therapy recommended"
      },
      {
        "value": "aba",
        "citations": [154],
        "explanation": "Applied behavioral analysis recommended"
      }
    ],
    "is_intervention_ongoing": {
      "value": true,
      "citations": [154],
      "explanation": "Intensive early intervention using applied behavioral analysis, and continued speech and occupational therapy recommended"
    },
    "has_recovery": {
      "value": false,
      "citations": [160, 162],
      "explanation": "Autism persisted as of 15 July 2002"
    }
  },
  "birth_record": {
    "was_normal_pre_vaccination": {
      "value": false,
      "citations": [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37],
      "explanation": "Premature birth at 31.5 weeks, complications including respiratory distress, apnea, bradycardia, thrombocytopenia, jaundice, feeding intolerance, gastroesophageal reflux, and hydrocele"
    },
    "apgar_score": {
      "value": ["8 at 1 minute", "9 at 5 minutes"],
      "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": "Parents reported lining up toys and objects, which was noted as a new behavior after regression"
      }
    ]
  },
  "heavy_metal_tests": [
    {
      "metal_type": "mercury",
      "value": 10,
      "unit": "ug/L",
      "is_elevated": false,
      "citations": [20]
    },
    {
      "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": "other",
      "citations": [32],
      "explanation": "Gastroesophageal reflux disorder, feeding intolerance, jaundice, thrombocytopenia, anemia of prematurity, apnea, otitis media, pharyngitis, respiratory distress, and hydrocele"
    }
  ],
  "environmental_exposures": [
    {
      "value": "unknown",
      "citations": [45],
      "explanation": "Mother alleged neurologic damage from exposure to thimerosal and mercury in vaccines, but specific environmental exposure not identified"
    }
  ],
  "legal_matter": {
    "value": true,
    "citations": [22],
    "explanation": "Report was received as part of litigation proceedings"
  },
  "diagnosis_reporting_delay_reason": {
    "value": "Report was received as part of litigation proceedings",
    "citations": [22],
    "explanation": "Report was received as part of litigation proceedings, suggesting delay due to legal process"
  }
}

Metrics

{
  "elapsed": 125.56362318992615,
  "usage": {
    "prompt_tokens": 13919,
    "completion_tokens": 4912,
    "total_tokens": 18831,
    "prompt_tokens_details": {
      "cached_tokens": 0,
      "audio_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}

Leave a Reply