The chatgpt api does not follow the conditions

I am using gpt-4.1 to make a diet based on user data. And everything seems to be fine, but I can’t get the right answer if the number of calories required is very high. He gives me back my diet, as if it were for the average person.

At the same time, by sending all the same data to playground, I get the correct result after several identical requests. But if I send a request from my app, it’s always incorrect. That is, if I need a 10,000-calorie ration, it returns me a maximum 2,000-calorie ration. Which is a big margin of error. Although if you go to chatgpt separately and send the simplest prompt, it immediately gives the correct result, with an error of several percent.

I changed everything in my queries and made suggestions, but I couldn’t get the right result.
What am I doing wrong?

I use such data:

system role: You’re a nutrition expert. Your task is to create an accurate nutrition plan for the day based on the user’s data, which corresponds to his daily needs for calories, proteins, fats and carbohydrates. The user specified their preferences, eating restrictions, and type of diet. You should offer 4 ready meals (breakfast, lunch, dinner and snack), balanced in terms of macronutrients and total calories

(approximate) user role: 10150 calories,650 proteins,550 fats,650 carbs.balanced,low-carb,vegetarianism diet.American,Russian,Chinese cuisine.Without soy,sesame,onion,wheat,fish,mushrooms

json_schema:

{
    "name": "meal_plan",
    "strict": true,
    "schema": {
        "type": "object",
        "properties": {
            "meals": {
                "type": "array",
                "description": "A collection of planned meals for the day, including breakfast, lunch, dinner, and snacks",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "description": "The only name of the dish"
                        },
                        "description": {
                            "type": "string",
                            "description": "A description of the dish, including ingredients and their approximate amounts (in grams)"
                        },
                        "nutritional_info": {
                            "type": "object",
                            "properties": {
                                "proteins": {
                                    "type": "number",
                                    "description": "The amount of proteins in grams"
                                },
                                "fats": {
                                    "type": "number",
                                    "description": "The amount of fats in grams"
                                },
                                "carbohydrates": {
                                    "type": "number",
                                    "description": "The amount of carbohydrates in grams"
                                }
                            },
                            "required": [
                                "proteins",
                                "fats",
                                "carbohydrates"
                            ],
                            "additionalProperties": false
                        }
                    },
                    "required": [
                        "name",
                        "description",
                        "nutritional_info"
                    ],
                    "additionalProperties": false
                }
            }
        },
        "required": [
            "meals"
        ],
        "additionalProperties": false
    }
}

I won’t bother posting this machine-translated to human language, because it still is unhelpful and content-unaware, a fabrication of fact that does not relate to what is seen in the topic.


What the symptom indicates to me is that the AI is not receiving your user input properly, and may just be proceeding to a default output.

What I would recommend to start with:

Since you attest that the Prompts Playground and the combination of system message and user input works for you there, I would remove any assistant response after you test, press the “get code” button in the user interface, and select a programming language you are familiar with.

Then you’ll receive a script that you can run, which should return quite similar results.

You can then work backwards, capturing the parameters that your application is sending to the API, and discovering how messages or other parameters do not conform to the expected patterns and the patterns you sent.

gpt-4.1 is quite fine to start with, especially when you need system instructions followed.

Thanks for the reply. I have checked and the api accepts all input data correctly and the data sending is also correct. And in fact, playground tests are also wrong, and here’s what else I found out

It turns out that it is important to follow the order of the json_schema structure, i.e. in my case, I have two objects - a description of the dish, which indicates the weight of the ingredients and nutrients (proteins, fats, carbohydrates)
, and if I set the description in json higher than the nutrients, then the number of nutrients adjusts to the description. But their number is reduced by 2 times compared to the required amount of nutrients specified by the user.
If the nutrients are higher than the description, then they adjust to the required amount of nutrients that the user has set. But the description indicates the incorrect weight of the ingredients.

Both of these options are inaccurate in their own way. The best option is when the nutrients are above the description. But still, the description lives its own life.

It seems that gpt’s thought is lost when it divides it all into several arrays and into different objects.

Honestly, I’m already tired because I can’t fix it. The margin of error still seems to be about 50%, which is a lot.

Maybe there is some guru here who can tell you how to make a json_schema correctly, where there would be an error of at least 10% in terms of nutrients and the description would show the correct weight of the ingredients.

I tried getting a json_object in the response for a more linear response, but it still doesn’t work. This is the system I used:

You are a nutritionist’s assistant with artificial intelligence. Based on the user’s diet type, unloved ingredients, preferred cuisine, and daily nutrition metrics (calories, protein, fat, carbohydrates), create a daily meal plan as a JSON object containing exactly four parameters: breakfast, lunch, dinner, and snack. Russian Russian version Each key contains an object with the following fields: dish_name (name of the dish), protein_g (amount of protein in grams), fat_g (amount of fat in grams), carbs_g (amount of carbohydrates in grams), description (approximate number of ingredients in grams, where the volume of dishes corresponded to about 25% of the caloric content of the declared by the user of the required caloric content). Make sure that the total number of macros for all dishes corresponds to the indicators of the user’s query, try to distribute the volume of the dish in equal amounts between all four meals, about 20-30% of the total caloric content stated by the user, exclude unloved ingredients, adhere to diet and culinary preferences, use portion sizes based on user data and respond only using the JSON object and without additional text

Here is something to consider: The model produces predictive language one token at a time. It does not have any place to plan out its thoughts in writing that you do not offer it. The AI must follow the structured output format that you provided, in the same order.

So the very first thing you are having it write is to open an array, and fill in a value “name” for (the only name of the dish), and then the amount of proteins.

It has no context for why it is writing that.

What you should do in your JSON schema is over more planning outputs initially. Things that the AI can easily produce, and then reflect on.

For example, you could have top-level properties first:

  • target daily calories
  • number of daily meals requested, needed
  • calories planned per meal
  • thinking/reasoning discussion: write your preliminary ideas of how to build this dietary requirement.

In that manner, you can directly have the AI repeat its understanding and compliance with your desires, and then build on understanding the structure and the array length it will use before it even gets to the JSON keys that are to relay the data.

Or: you can use a reasoning model such as o4-mini that is allowed to do this itself in its own hidden language before it commits to a JSON being produced.

1 Like

Thanks for the tips, they were really helpful.

I fixed json_schema and gpt became more aware of what I want from it. However, I had to divide it into two queries - making a diet and counting nutrients. But that’s okay, the most important thing is that I’m getting good results now.