Skip to content

Commit 1254c67

Browse files
fpagnybene2k1
andauthored
feat(genapi): add recommendations for structured outputs usage (#4791)
* feat(genapi): add recommendations for structured outputs usage * feat(genapi): update recommandations in troubleshooting * feat(genapi): add link on structured outputs recommandations * Update pages/generative-apis/how-to/use-structured-outputs.mdx --------- Co-authored-by: Benedikt Rollik <[email protected]>
1 parent 28367f1 commit 1254c67

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pages/generative-apis/how-to/use-structured-outputs.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ Output example:
132132
}
133133
```
134134

135+
<Message type="tip">
136+
Structured outputs accuracy may vary between models. For instance, with Llama models, we suggest adding a description of the field looked for in `response_format` and in `system` or `user` messages. In our example this would mean adding a system prompt similar to:
137+
```bash
138+
"content": "The following is a voice message transcript. Provide the message title, summary and action items. Only answer in JSON using '{' as the first character.",
139+
```
140+
For additional optimization or troubleshooting, see [Structured output (e.g., JSON) is not working correctly](/generative-apis/troubleshooting/fixing-common-issues/#structured-output-eg-json-is-not-working-correctly).
141+
</Message>
142+
135143
### Using structured outputs with JSON schema (manual definition)
136144
137145
Alternatively, users can manually define the JSON schema inline when calling the model.

pages/generative-apis/troubleshooting/fixing-common-issues.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,24 @@ Below are common issues that you may encounter when using Generative APIs, their
6363

6464
## Structured output (e.g., JSON) is not working correctly
6565

66-
### Cause
66+
### Description
67+
- Structured output response contains invalid JSON
68+
- Structured output response is valid JSON but content is less relevant
69+
70+
### Causes
6771
- Incorrect field naming in the request, such as using `"format"` instead of the correct `"response_format"` field.
6872
- Lack of a JSON schema, which can lead to ambiguity in the output structure.
73+
- Maximum tokens is lower than what the model response needs to be complete.
74+
- Temperature is not set or set too high.
75+
6976

7077
### Solution
7178
- Ensure the proper field `"response_format"` is used in the query.
7279
- Provide a JSON schema in the request to guide the model's structured output.
80+
- Ensure the `max_tokens` value is higher than the response `completion_tokens` value. If this is not the case, the model answer may be stripped down before it can finish the proper JSON structure (and lack closing JSON brackets `}` for example). Note that if the `max_tokens` value is not set in the query, [default values apply for each models](/generative-apis/reference-content/supported-models/).
81+
- Ensure the `temperature` value is set with a lower range value for the model. If this is not the case, the model answer may output invalid JSON characters. Note that if the `temperature` value is not set in the query, [default values apply for each models](/generative-apis/reference-content/supported-models/). As examples:
82+
- for `llama-3.3-70b-instruct`, `temperature` should be set lower than `0.6`
83+
- for `mistral-nemo-instruct-2407 `, `temperature` should be set lower than `0.3`
7384
- Refer to the [documentation on structured outputs](/generative-apis/how-to/use-structured-outputs/) for examples and additional guidance.
7485

7586

0 commit comments

Comments
 (0)