Back to discover
ArticlesBuild with AICurioFuse editorial

DeepSeek API: turn a short note into JSON

Request a structured answer with an explicit format and allow for empty responses.

A blue mug and blank paper beside a glass prism, two small blue mugs in a wooden tray, and a red bowl outside it.
CurioFuse · AI-generated illustration
In this article

Prepare a small Python request

This exercise is for readers who can edit a Python script. Follow DeepSeek’s setup example: obtain an API key, install the OpenAI SDK, and configure the client with https://api.deepseek.com. Read the key from DEEPSEEK_API_KEY as shown there. Use deepseek-flash with stream=False; the example reads the returned answer from response.choices[0].message.content. This is the developer API workflow.

Specify the output shape

The format guide requires response_format={"type":"json_object"}, the word “json” in a system or user message, and an example of the desired structure. Our practice input is “Item: blue mug; quantity: two.” Request item and quantity fields, showing {"item":"red bowl","quantity":1} as a format example. Set max_tokens reasonably; empty content can still occur. This is an illustrative task, not a measured result.

Inspect a cut-off response

The API reference warns that omitting the JSON instruction can produce whitespace until the token limit. It also explains that finish_reason="length" can accompany output cut off by max_tokens or the conversation’s context limit. Our suggested check: inspect that field before treating a response as complete. Raising the output allowance does not remove the shared input-and-output context limit.

Check the decoded values

Python’s JSON decoder converts text with json.loads; parsing failures can raise JSONDecodeError. By default, it also accepts NaN and Infinity values outside the JSON specification. Successful decoding therefore does not prove strict JSON validity. An object becomes a dictionary. Our suggested content check remains separate: item should describe the blue mug and quantity should be the number 2. Reject the copied red-bowl example. Parsing success does not establish faithful extraction.

Sources(4)

Original CurioFuse editorial. A guide to explore and evaluate AI, without an implied product test.

Sources & standardsReport a correction