json_schema not working

#18
by LaferriereJC - opened

tool use? yet doesn't adhere to json_schema

Qwen3-Coder-480B-A35B-Instruct is a Mixture-of-Experts (MoE) code generation model developed by the Qwen team. It is optimized for agentic coding tasks such as function calling, tool use, and long-context reasoning over repositories. The model features 480 billion total parameters, with 35 billion active per forward pass (8 out of 160 experts).

Pricing for the Alibaba endpoints varies by context length. Once a request is greater than 128k input tokens, the higher pricing is used.

here is the code I'm using

import requests
import json
import os

api_key = os.getenv("OPENROUTER_API_KEY")

response = requests.post(
  url="https://openrouter.ai/api/v1/chat/completions",
  headers={
    "Authorization": f"""Bearer {api_key}""",
    "HTTP-Referer": "<YOUR_SITE_URL>", # Optional. Site URL for rankings on openrouter.ai.
    "X-Title": "<YOUR_SITE_NAME>", # Optional. Site title for rankings on openrouter.ai.
    "Content-Type": "application/json"
  },
  data=json.dumps({
    "model": "qwen/qwen3-coder",
    "messages": [
      {
        "role": "system",
        "content": "You provide responses in json schema with comprehensive analysis using Six Thinking Hats methodology and systematic reasoning."
      },
      {
        "role": "user",
        "content": "Provide code for Conway's Game of Life with comprehensive analysis."
      }
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "comprehensive_analysis_extraction",
        "strict": True,
        "schema": {
          "type": "object",
          "properties": {
            # Core extraction
            "entities": {
              "type": "array",
              "items": {"type": "string"},
              "maxItems": 3,
              "description": "Key actors, components or concepts of user instruction"
            },
            "predicates": {
              "type": "array", 
              "items": {"type": "string"},
              "maxItems": 5,
              "description": "Core interactions and relationships of user instruction"
            },
            "intent": {
              "type": "string",
              "description": "Objective of user instruction."
            },
            "features": {
              "type": "array",
              "items": {"type": "string"},
              "maxItems": 13,
              "description": "Necessary features (i.e. factors or ideas) to achieve user instruction."
            },
            
            # Six Thinking Hats dimensions
            "white_hat": {
              "type": "string",
              "description": "White Hat: Facts, data and known information about user instruction."
            },
            "blue_hat": {
              "type": "string", 
              "description": "Blue Hat: Prioritizes critical path for achieving user instruction."
            },
            "black_hat": {
              "type": "string",
              "description": "Black Hat: Identify potential risks and limitations to plan for user instruction."
            },
            "red_hat": {
              "type": "string",
              "description": "Red Hat: Unbiased critique of pitfalls to achieve user instruction."
            },
            "yellow_hat": {
              "type": "string",
              "description": "Yellow Hat: Create solution identifier to user instruction."
            },
            "green_hat": {
              "type": "string",
              "description": "Green Hat: Contingencies to achieve objective of user instruction."
            },
            
            "datasets": {
              "type": "array",
              "items": {"type": "string"},
              "maxItems": 3,
              "description": "Presumed or required initial conditions"
            },
            
            # Implementation
            "outline": {
              "type": "string",
              "description": "General ordering of ideas."
            },
            "methods": {
              "type": "array",
              "items": {"type": "string"},
              "maxItems": 8,
              "description": "Functions definition headers based on predicates and features."
            },
            "abstract_classes": {
              "type": "array",
              "items": {"type": "string"},
              "maxItems": 3,
              "description": "Semantic containers for entities, and their properties as member variables."
            },
            
            # Assessment
            "evaluation": {
              "type": "string",
              "description": "Expected outcomes and metrics"
            },
            "unclear": {
              "type": "string",
              "description": "Unanswered questions."
            },
            
            # Original code field
            "code": {
              "type": "string"
            }
          },
          "required": [
            "entities", "predicates", "intent", "features",
            "white_hat", "blue_hat", "black_hat", "red_hat", "yellow_hat", "green_hat",
            "datasets", "outline", "methods", "evaluation", "unclear", "code"
          ],
          "additionalProperties": False
        }
      }
    }
  })
)

# Parse the response
result = response.json()
structured_data = json.loads(result['choices'][0]['message']['content'])
print(json.dumps(structured_data, indent=2))

just makes up it's own keys

Sign up or log in to comment