Replies: 2 comments
-
Hey @vank3f3! 👋 I'm here to help you with any bugs, questions, or contribution ideas you have. I'm Dosu, a bot designed to assist you while you wait for a human maintainer. Let's tackle this structured outputs challenge together! I found a similar unsolved discussion: Support for OpenAI Structured Outputs [1]. Additionally, there was a closed issue: OpenAI compatible API JSON format error which suggested toggling off certain options to resolve the issue [2]. To avoid the "Bad Request Error" with code 400 related to the missing parameter 'response_format.json_schema', you need to ensure that the Here is the correct way to use OpenAI's structured outputs:
Here is an example of how to set this up: import json
model_parameters = {
"response_format": "json_schema",
"json_schema": '{"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}}}'
}
# Ensure the json_schema is valid
try:
schema = json.loads(model_parameters["json_schema"])
except:
raise ValueError(f"not correct json_schema format: {model_parameters['json_schema']}")
# Update model_parameters to include the structured response format
model_parameters["response_format"] = {"type": "json_schema", "json_schema": schema}
model_parameters.pop("json_schema")
# Now you can invoke the model with the updated model_parameters
response = client.chat.completions.create(
messages=[self._convert_prompt_message_to_dict(m) for m in prompt_messages],
model=model,
stream=stream,
**model_parameters,
**extra_model_kwargs,
) This ensures that the |
Beta Was this translation helpful? Give feedback.
-
We have just updated the help documentation to explain how this feature works in Dify. Please note that this feature comes from a community pull request, so some edge cases are not yet supported in Dify:https://docs.dify.ai/learn-more/extended-reading/how-to-use-json-schema-in-dify |
Beta Was this translation helpful? Give feedback.
-
Self Checks
Dify version
0.8.0-beta1
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
I tried a variety of json structures to use this structured outputs feature, but they all told me I was wrong.
What is the right way to use structured outputs .
✔️ Expected Behavior
Correctly output Json structure
❌ Actual Behavior
An error has occurred
Beta Was this translation helpful? Give feedback.
All reactions