Skip to content
Guides

Tool Calling & Structured Output ​

Connect LLMs to internal APIs, databases, and external workflows with strict JSON Schema tool definitions and guaranteed structured responses.

Function / Tool Calling Loop ​

  1. Send your user message together with tools (JSON Schema parameter definitions).
  2. Inspect message.tool_calls (or tool_use blocks in Anthropic Messages).
  3. Execute the function locally in your application and append the role: "tool" result referencing tool_call_id.
  4. Send the updated message array back to receive the final grounded answer.
python
import json, os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["VANIX_API_KEY"],
    base_url="https://api.vanix.io/v1",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_order_status",
            "description": "Retrieve live shipment status by order ID.",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {"type": "string", "description": "Order ID such as ORD-9042"}
                },
                "required": ["order_id"],
                "additionalProperties": False,
            },
            "strict": True,
        },
    }
]

resp = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Where is order ORD-9042?"}],
    tools=tools,
    tool_choice="auto",
)

for call in resp.choices[0].message.tool_calls or []:
    print(call.function.name, json.loads(call.function.arguments))

Strict Structured Output (json_schema) ​

Guarantee schema-compliant JSON without fragile regex parsing:

bash
curl https://api.vanix.io/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VANIX_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "user", "content": "Classify ticket: Billing invoice PDF returns 403 after login."}
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "ticket_triage",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "category": {"type": "string", "enum": ["billing", "auth", "api", "other"]},
            "severity": {"type": "string", "enum": ["p1", "p2", "p3"]},
            "summary": {"type": "string"}
          },
          "required": ["category", "severity", "summary"],
          "additionalProperties": false
        }
      }
    }
  }'

Strict Mode Checklist

When "strict": true is enabled on OpenAI schemas, every object level must declare "additionalProperties": false and list all keys in "required".

Vanix AI Unified Model Gateway · https://api.vanix.io