I am working on a Q&A application where I want to generate structured responses from the OpenAI API in a specified JSON format. In some cases, I also need to generate images to accompany the response when required (e.g., to visually explain a concept). To achieve this, I’ve implemented both function calling (to generate images) and a custom response format. However, I encounter a 400 Client Error: Bad Request
when making the API call to:
https://api.openai.com/v1/chat/completions
Is it possible to use both response_format
and functions
simultaneously, or is there an issue in my approach? Below is the relevant code I am using:
def get_function_definitions():
"""Define available functions for the model."""
return [{
"name": "generate_image",
"description": "Generate an image based on the given description",
"parameters": {
"type": "object",
"properties": {
"description": {
"type": "string",
"description": "A detailed description of the image"
}
},
"required": ["description"]
}
}]
def get_response_format():
"""Define the expected response format."""
return {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"topic": {"type": "string"},
"questions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"question_number": {"type": "integer"},
"question": {"type": "string"},
"answer": {"type": "string"},
"image_url": {"type": "string", "optional": True}
},
"required": ["question_number", "question", "answer"]
}
},
"summary": {"type": "string"}
},
"required": ["topic", "questions", "summary"]
}
}
def make_api_call(system_prompt, user_prompt, messages=None):
"""Make API call with both function calling and response format."""
if messages is None:
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
request_data = {
"model": "gpt-4o",
"messages": messages,
"functions": get_function_definitions(),
"function_call": "auto",
"response_format": get_response_format(), # Combining functions & response format
"temperature": 0.3
}
try:
response = requests.post(
url="https://api.openai.com/v1/chat/completions",
headers={
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json"
},
json=request_data
)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error making API call: {str(e)}")
def main():
system_prompt = """You are an educational assistant. Provide responses in the
specified JSON format. When explaining complex concepts, generate images only
if necessary through the provided function."""
user_prompt = "Explain OOP concepts. Include at least one question that would benefit from a visual explanation."
response = make_api_call(system_prompt, user_prompt)
if "error" not in response:
print("\nFormatted Response:")
print(json.dumps(response, indent=2))
else:
print(f"\nError occurred: {response['error']}")
if __name__ == "__main__":
main()