Getting Stable JSON from deepseek-flash with response_format
The scenario and the pitfalls
When model output feeds a downstream program, format stability matters more than prose. Prompt-only constraints for JSON from deepseek-flash produce uneven results. The official response_format parameter exists for exactly this case. Here is a full walkthrough plus the pitfalls worth knowing.
Basic usage
Add response_format to the request body with type set to json_object, and state the JSON requirement in your prompt together with a field example. The official docs call out a specific failure mode. Without the word json somewhere in the prompt, the API raises an error about possibly generating unbounded whitespace. Both conditions must hold. Sample code below.
from openai import OpenAI
client = OpenAI(
api_key="YOUR_KEY",
base_url="https://api.deepseek.com")
resp = client.chat.completions.create(
model="deepseek-flash",
messages=[{"role": "user", "content": (
"Extract company and funding amount as JSON,"
" fields company and amount:"
" Zhipu closed a new funding round")}],
response_format={"type": "json_object"})
Parsing and fallback
Do not trust the response blindly. Strip stray code fences and padding before json.loads. When parsing fails, log the raw output with the error, then ask the model again with the failure attached. In practice two retries are usually enough.
Lower the temperature as well. For structured extraction keep it between 0.1 and 0.3, which visibly cuts random fields. For complex schemas, include the field definitions plus one positive and one negative example in the prompt rather than a bare description.
Division of labor with function calling
Use function calling when the model must act, JSON output when it only needs to return data. They combine well, restructuring tool results into structured records. Our function calling walkthrough and Python quickstart make good prerequisites. Model names and pricing live in the access quick reference.
Closing notes
Output can be truncated in json_object mode, so split very long structures across calls. The mode does not validate schema either. Constrain types and enums in the prompt, then enforce them again in code. This article is compiled from publicly available online sources.
大模型网摘