Can I get only a javascript object in response and no more text related?

I wanted to get only an object as a response from API, so I tried several prompts like “… Make sure only return a javascript object as JSON format and nothing else”, but sometimes it still returned some unnecessary words. How I can force the API to do that?

I don’t think you can 100% force it, if you need to use natural language you can say things like
your output starts with {" and ends with "}
and also add safe parsing in your code, so you disregard all text until the first { and then discard all text after the last } before you try to decode it

but if it’s a fixed structure, you could make an api function for it, and in the api call set ‘function_call’ to your function name to force the model to use the function and thus to give you properly json formatted arguments


Thank you for your solution. I think function calling is the best for now, but I do not really understand the logic and complex structure, so I’m still working on it.

Adding some few shot examples also works wonders.

Work with not against. I’m sure there’s different opinions but rules should be a last resort. You can usually set the structure of the response with a couple examples instead of telling it what to do.