fix(GH-5922): Preserve OpenAI response metadata#5929
Open
yaner-here wants to merge 1 commit intospring-projects:mainfrom
Open
fix(GH-5922): Preserve OpenAI response metadata#5929yaner-here wants to merge 1 commit intospring-projects:mainfrom
yaner-here wants to merge 1 commit intospring-projects:mainfrom
Conversation
Member
ilayaperumalg
left a comment
There was a problem hiding this comment.
Thanks for the PR! It's a great catch to preserve these additional properties.
However, there is one significant issue with leaking SDK-specific types into Spring AI's generic abstractions, and one minor refactoring suggestion:
- Leaking
com.openai.core.JsonValue:
By doingresult._additionalProperties().forEach(metadataBuilder::keyValue);, the map is populated with OpenAI SDK'sJsonValueobjects. As shown in the added test (metadata.<JsonValue>get(\non_null_field\)), this forces users to cast values to an OpenAI-specific class. This breaks Spring AI's portability—if a user switches to another provider, their code expectingJsonValuewill break.
Recommendation: Unwrap the JsonValue into standard Java types (String, Number, Boolean, List, Map) before adding them. You can use Jackson for this:
result._additionalProperties().forEach((key, jsonValue) -> {
try {
Object unmarshalled = ModelOptionsUtils.JSON_MAPPER.readValue(jsonValue.toString(), Object.class);
metadataBuilder.keyValue(key, unmarshalled);
} catch (Exception e) {
metadataBuilder.keyValue(key, jsonValue.toString());
}
});- Minor Map Copying Simplification:
Infrom(ChatResponseMetadata chatResponseMetadata, Usage usage), creating a temporary map via Streams is fine, but you could simplify it by iterating directly over the entries:
ChatResponseMetadata.Builder builder = ChatResponseMetadata.builder()
.id(chatResponseMetadata.getId())
.usage(usage)
.model(chatResponseMetadata.getModel());
chatResponseMetadata.entrySet().forEach(e -> builder.keyValue(e.getKey(), e.getValue()));
return builder.build();54748c3 to
3d452d1
Compare
Contributor
Author
Fixes spring-projectsGH-5922 (spring-projects#5922) Signed-off-by: Yaner <yaner@yaner-here.top>
efdd543 to
64fb781
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #5922. Preserves unmapped root-level OpenAI chat completion response fields in
ChatResponseMetadata.The OpenAI SDK exposes provider-specific root fields through
ChatCompletion._additionalProperties(). These fields were previously dropped when Spring AI mappedChatCompletionintoChatResponseMetadata. This change copies those additional properties into the metadata map.