-
Notifications
You must be signed in to change notification settings - Fork 2k
-
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;
}
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment