# Function Calling & Structured JSON — API Store Hub

> VI: Khai báo `tools` và `response_format: json_schema` chuẩn xác trên https://apistorehub.com/v1 để trích xuất dữ liệu ERP, đơn hàng và gọi hàm backend không lỗi parse JSON.
> EN: Configure strict `tools` and `response_format: json_schema` on https://apistorehub.com/v1 for reliable backend tool execution and zero-broken-JSON extraction.

- Canonical VI: https://apistorehub.com/vi/tich-hop/function-calling-structured-json
- Canonical EN: https://apistorehub.com/en/tich-hop/function-calling-structured-json
- Language: TypeScript
- Runtime: Node.js 20+ / Python 3.10+, OpenAI & Anthropic SDK
- Protocol: OpenAI Compatible (/v1/chat/completions) & Anthropic (/v1/messages)
- Install: `npm install openai zod`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Define flat JSON Schemas with `additionalProperties: false` and explicit `required` arrays.
2. Use `response_format: { type: "json_schema", ... }` for pure structured extraction.
3. Declare `tools` with crisp parameter descriptions and units for external actions.
4. Validate parsed JSON via `zod` or `pydantic` before writing to downstream databases.

## Production Code Sample
```
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://apistorehub.com/v1", apiKey: process.env.API_KEY });

const res = await client.chat.completions.create({
  model: "gpt-6.2-sol",
  messages: [{ role: "user", content: "Extract order: 2 keyboards at 450000 VND for Minh." }],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "order_extract",
      strict: true,
      schema: {
        type: "object",
        properties: {
          customer: { type: "string" },
          item: { type: "string" },
          qty: { type: "integer" },
          unit_price_vnd: { type: "integer" }
        },
        required: ["customer", "item", "qty", "unit_price_vnd"],
        additionalProperties: false
      }
    }
  }
});
console.log(JSON.parse(res.choices[0].message.content || "{}"));
```

## Quick cURL Check
```bash
curl https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-6.2-sol","response_format":{"type":"json_object"},"messages":[{"role":"system","content":"Return JSON only."},{"role":"user","content":"Extract city from: Giao hang tai Da Nang"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `gpt-6.2-sol` | 6,700 ST / 1M tokens | 1.1M | https://apistorehub.com/llms/models/gpt-6-2-sol.md |
| `claude-sonnet-5-5` | 7,700 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/claude-sonnet-5-5.md |
| `gemini-4-neon` | 20,400 ST / 1M tokens | 2M | https://apistorehub.com/llms/models/gemini-4-neon.md |

## FAQ
- **When should I pick `json_schema` over `json_object`?**: Use `json_schema` with `strict: true` whenever exact keys and types are mandatory; use `json_object` for flexible payloads.
- **Are parallel tool calls supported on API Store Hub?**: Yes. Modern GPT, Claude, and Gemini models return multiple `tool_calls` in one turn.
- **How do I guard against malformed JSON on legacy models?**: Enable strict `response_format` where supported and validate with Zod or Pydantic plus a single repair retry.
