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.
1 Đoán trước
2 Xem
One token at a time
1 / 4Prompt: “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.
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 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 Kiểm tra
6 Nói lại bằng lời bạn
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?
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