Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson Parse Error for OpenAI API ChatCompletion#usage #1369

Open
the-mgi opened this issue Sep 16, 2024 · 2 comments · May be fixed by #1394
Open

Jackson Parse Error for OpenAI API ChatCompletion#usage #1369

the-mgi opened this issue Sep 16, 2024 · 2 comments · May be fixed by #1394

Comments

@the-mgi
Copy link

the-mgi commented Sep 16, 2024

Unreconized field "completion_tokens_details" coming in response from openai api. the error is occuring at class org.springframework.ai.openai.api.OpenAiApi.ChatCompletion#usage and in Usage we have no Usage#completion_tokens_details

Bug description
there's not @JsonIgnoreProperties(ignoreUnkown=true)

Environment
macOS spring framework ai version 1.0.0-M2

Steps to reproduce
Call OpenAiChatModel#call

Expected behavior
it should not throw an error if there's an unmapped field

Minimal Complete Reproducible example

PromptTemplate template = new PromptTemplate(promptString);
                template.add("desc", product.getMetaDescription());
                template.add("name", product.getProductName());
                String desc = this.aiChatModel.call(template.create()).getResult().getOutput().getContent();
                a.setDesc(desc);

encountered a similar bug a while back which was resolved here #1178

stack trace
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "completion_tokens_details" (class org.springframework.ai.openai.api.OpenAiApi$Usage), not marked as ignorable (3 known properties: "completion_tokens", "prompt_tokens", "total_tokens"])
at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION disabled); line: 22, column: 35] (through reference chain: org.springframework.ai.openai.api.OpenAiApi$ChatCompletion["usage"]->org.springframework.ai.openai.api.OpenAiApi$Usage["completion_tokens_details"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:1153)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:2241)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1793)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1771)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:279)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:470)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1493)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:545)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:576)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:446)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1493)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2125)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1501)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:395)
... 18 common frames omitted

@kashyappvb
Copy link

kashyappvb commented Sep 20, 2024

Even we are facing exact same issue
tried using 1.0.0-M2, 1.0.0-SNAPSHOT version for below

org.springframework.ai:spring-ai-bom

org.springframework.ai:spring-ai-openai-spring-boot-starter

Code : ChatResponse chatResponse = chatClient.prompt().user(payload).call().chatResponse();

Temporary solution worked for us:

@bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}

@dafriz
Copy link
Contributor

dafriz commented Sep 22, 2024

An alternative to the above work around - If you are using Spring Boot and want to keep using the existing auto configured ObjectMapper, but change this one property you can use:

@Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> builder.failOnUnknownProperties(false);
}

completion_tokens_details - it's a new field added by OpenAI with the release of the o1 models with a nested reasoning_tokens field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants