aifree.dev

Vì sao nó làm hỏng JSON của tôi?

Structured output (giải mã có ràng buộc) · Thực hành · 5 phút · sampling, llm · học sau Dự đoán token tiếp theo, Temperature và lấy mẫu

Mô hình viết JSON từng token một, nên không có gì chặn một token làm hỏng nó. Chế độ structured output đưa schema của bạn cho API và chặn mọi token không khớp; giá trị thì bạn vẫn phải tự kiểm tra.

Trước khi bắt đầu: bạn giải thích được chuyện này cho bạn bè không?

1 = hoàn toàn không · 5 = dễ dàng

  1. 1 Đoán trước

    Bạn yêu cầu JSON và câu trả lời mở đầu bằng “Sure! Here is your JSON:”. Vì sao?
  2. 2 Xem

    One token at a time

    1 / 4

    Prompt: “Return the user as JSON with name and age.”

    Sure! Here is the JSON:
    ```json
    {"name": "Ada", "age": thirty-six}
    ```

    JSON.parse → SyntaxError

    illustrative failure, not a recorded output

    Asked for JSON, it adds a friendly sentence and writes thirty-six. The parser gives up.

    Mỗi token là một lựa chọn giữa vài khả năng, trong đó có cái làm hỏng JSON; có schema, API loại các lựa chọn đó trước khi chọn, nên định dạng đứng vững nhưng giá trị vẫn cần kiểm tra.

  3. Constrained decoding (giải mã có ràng buộc) nghĩa là API biết schema của bạn và ở mỗi bước chỉ cho phép những token còn dẫn tới output hợp lệ. Anthropic và Gemini mô tả structured output của họ theo cách này. OpenAI tách JSON mode (JSON hợp lệ, hình dạng tuỳ ý) khỏi Structured Outputs, thứ đi theo schema của bạn.

  4. 4 Bạn là model

    Bạn là bộ lọc. Schema đã cố định; ở mỗi bước, hãy chọn token kế tiếp duy nhất mà nó cho phép.

    schema: {name: string, age: integer, plan: "free" | "pro"} · no extra keys

    (empty)▍

    candidate tokens illustrative

    You are the filter: which next token does the schema allow?
  5. 5 Kiểm tra

    Schema ghi age là số nguyên. Constrained decoding chặn token kế tiếp nào?
  6. 6 Nói lại bằng lời bạn

  7. 7 Nhiệm vụ · 5 phút

    Với một API key Gemini miễn phí, nhờ Gemini 3.8 Flash trích một công thức nấu ăn hai lần: một lần có JSON schema trong response_format (như tài liệu structured output của Gemini), một lần chỉ xin JSON trong prompt. Parse cả hai. Rồi thêm maxLength cho một trường và xem nó có được áp dụng không.

Ba câu nhanh

Rút ngẫu nhiên từ bộ câu hỏi của bài; lần sau sẽ khác.

Xong chưa?

Còn bây giờ: bạn giải thích được cho bạn bè chưa?

1 = hoàn toàn không · 5 = dễ dàng

Trả lời ba câu nhanh trước đã.

Nguồn

  • https://ai.google.dev/gemini-api/docs/structured-output (updated 2026-10-20, checked 2026-10-21): response_format with mime_type application/json and a schema; examples use gemini-3.8-flash; “While output is syntactically correct JSON, always validate values in your application”; minLength, maxLength, minProperties, maxProperties “not enforced during constrained decoding and are ignored by the server”
  • https://developers.openai.com/api/docs/guides/structured-outputs (checked 2026-10-21; no update date): “only Structured Outputs ensure schema adherence” (not JSON mode); text.format json_schema with strict: true; available “starting with GPT-4o”; refusals in a separate field
  • https://platform.claude.com/docs/en/build-with-claude/structured-outputs (checked 2026-10-21; no update date): output_config.format json_schema; constrained decoding on the listed models (e.g. claude-sonnet-4-6, claude-haiku-4-5); refusal and max_tokens stops may not match the schema; enum values may differ in capitalization
  • https://ai.google.dev/gemini-api/docs/pricing (updated 2026-10-19, checked 2026-10-21): Gemini 3.8 Flash input free of charge on the free tier
  • The broken answer and the token odds in the picture are illustrative, labelled as such

Đăng Oct 21, 2026 · cập nhật Oct 21, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel
Join @aifree_dev and get new free AI offers and news right in your Telegram feed.
Browser notifications
Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.
Email newsletter
A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

+

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer