Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Problem when using structured response with OpenAI #3758

Unanswered
monkiki asked this question in Q&A
Discussion options

I'm trying to use the structured response API from OpenAI but always get this error:

org.springframework.ai.retry.NonTransientAiException: 400 - {
 "error": {
 "message": "Invalid schema for response_format 'custom_schema': In context=(), 'required' is required to be supplied and to be an array including every key in properties. Missing 'customer_address'.",
 "type": "invalid_request_error",
 "param": "response_format",
 "code": null
 }
}

This the sample code:

String text = "<text extracted from PDF invoice>";
String userPrompt = "Extract this invoice metadata in json format. The date fields need to be in \"yyyyMMddHHmmss\" format. " +
				"The fields to extract are described in the provided json schema."
String fullPrompt = userPrompt + " : " + text;
BeanOutputConverter<?> outputConverter = new BeanOutputConverter<>(InvoiceMetadata.class);
Prompt prompt = new Prompt(fullPrompt,
		OpenAiChatOptions.builder()
			.responseFormat(ResponseFormat.builder()
					.type(ResponseFormat.Type.JSON_SCHEMA)
					.jsonSchema(outputConverter.getJsonSchema())
					.build())
			.build());
ChatResponse response = chatModel.call(prompt);
return response.getResult().getOutput().getText();

This is the bean used to get the structured data:

@JsonIgnoreProperties(ignoreUnknown = true)
public static class InvoiceMetadata {
	public String invoice_number;
	public String invoice_date;
	public String invoice_amount;
	public String customer_cif;
	public String customer_name;
	public String customer_address;
}
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /