Can you help me what is wrong with this request?
{
“temperature”: 1,
“model”: “gpt-4o-2024-08-06”,
“messages”: [
{
“role”: “user”,
“content”: [
{
“type”: “text”,
“text”: “Co je dnes zaden?”
}
]
}
],
“max_tokens”: 300,
“response_format”: {
“type”: “json_schema”,
“json_schema”: {
“name”: “SimpleTextResponse”,
“schema”: {
“description”: “Simple text”,
“type”: “object”,
“additionalProperties”: false,
“properties”: {
“content”: {
“type”: [
“string”,
“null”
]
}
}
}
}
}
}
The error message you’re encountering suggests that there is an issue with the JSON schema provided in the response_format. Specifically, it seems that the schema is not correctly structured according to the expected format.
Here’s a corrected version of your JSON schema for the response_format:
{
"temperature": 1,
"model": "gpt-4o-2024-08-06",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Co je dnes zaden?"
}
]
}
],
"max_tokens": 300,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "SimpleTextResponse",
"strict": true,
"schema": {
"type": "object",
"properties": {
"content": {
"type": ["string", "null"]
}
},
"required": ["content"],
"additionalProperties": false
}
}
}
}
Key Corrections:
-
strictField: Added"strict": trueto ensure that all required fields are present and no additional properties are allowed. -
requiredField: Added"required": ["content"]to specify that thecontentfield is required. -
contentType: Thecontentfield is specified to be of type["string", "null"], allowing it to be either a string or null. -
additionalProperties: Set tofalseto prevent any properties other than those defined inproperties.
These changes should help ensure that the JSON schema is valid and correctly structured for the response_format.
The schema alone can be tested on the API playground site:
{
"name": "SimpleTextResponse",
"strict": true,
"schema": {
"type": "object",
"properties": {
"content": {
"type": ["string", "null"]
}
},
"required": ["content"],
"additionalProperties": false
}
}
I did changes and stil getting same response.
{
"temperature": 1.0,
"model": "gpt-4o-2024-08-06",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Co je dnes zaden?"
}
]
}
],
"max_tokens": 300,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "SimpleTextResponse",
"strict": true,
"schema": {
"description": "Simple text",
"type": "object",
"additionalProperties": false,
"properties": { "content": { "type": "string" } },
"required": [ "content" ]
}
}
}
}
This has same error respons. What is wrong?
{
"temperature": 1.0,
"model": "gpt-4o-2024-08-06",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Co je dnes zaden?"
}
]
}
],
"max_tokens": 300,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "SimpleTextResponse",
"strict": true,
"schema": {
"description": "Simple text",
"type": "object",
"additionalProperties": false,
"properties": { "Content": { "type": [ "string", "null" ] } },
"required": [ "Content" ]
}
}
}
}
It depends on how you are attempting to use the object.
If in Python, as a dictionary object instead of a JSON string that is directly sent, the boolean values must be uppercase True keywords, while in JSON, they are in lowercase true, along with other particularities that need transformation and other that simply can’t be interchanged.
Here for example, I make those modification and break down your request object into parts for management. Then you can chat with your schema.
import os, json, requests
model = "gpt-4o-2024-08-06"
user = [{"role": "user", "content": [{"type": "text", "text": "Co je dnes zaden?"}]}]
params_template = {"model": "gpt-4o-2024-08-06", "max_tokens": 300, "temperature": 1.0}
params_template.update(
{
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "SimpleTextResponse",
"strict": True,
"schema": {
"description": "Simple text",
"type": "object",
"additionalProperties": False,
"properties": {"Content": {"type": ["string", "null"]}},
"required": ["Content"],
},
},
}
}
)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
while not user[0]["content"] == "exit":
request = {**params_template, **{"messages": user}}
try:
response = requests.post(
"https://api.openai.com/v1/chat/completions", headers=headers, json=request
)
except Exception as e:
print(f"Error: {e}")
if response.status_code != 200:
break
else:
reply = response.json()["choices"][0]["message"]["content"]
print(reply)
print(response.json()["usage"])
user = [
{"role": "user", "content": [{"type": "text", "text": input("\nPrompt: ")}]}
]
The requests library turns Python’s dict into JSON to send to a restful API with the json= input parameter.
I am using json. The object which i send was object serialized to json.
Thank you, I solved my problem. That was in my httpclient serialization. You was right.