Skip to content

Commit 830cb2b

Browse files
committed
Add JsonIgnoreProperties to Model Response objects
- For all the model response API objects, add `@JsonIgnoreProperties(ignoreUnknown = true)` which provides flexibility to ignore any unknown properties which come as part of the response Resolves #3026 Signed-off-by: Ilayaperumal Gopinathan <[email protected]>
1 parent 548abed commit 830cb2b

File tree

14 files changed

+123
-2
lines changed

14 files changed

+123
-2
lines changed

models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.function.Consumer;
2525
import java.util.function.Predicate;
2626

27+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2728
import com.fasterxml.jackson.annotation.JsonInclude;
2829
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2930
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -699,6 +700,7 @@ public record AnthropicMessage(
699700
* response.
700701
*/
701702
@JsonInclude(Include.NON_NULL)
703+
@JsonIgnoreProperties(ignoreUnknown = true)
702704
public record ContentBlock(
703705
// @formatter:off
704706
@JsonProperty("type") Type type,
@@ -958,6 +960,7 @@ public record Tool(
958960
* @param usage Input and output token usage.
959961
*/
960962
@JsonInclude(Include.NON_NULL)
963+
@JsonIgnoreProperties(ignoreUnknown = true)
961964
public record ChatCompletionResponse(
962965
// @formatter:off
963966
@JsonProperty("id") String id,
@@ -980,6 +983,7 @@ public record ChatCompletionResponse(
980983
* @param outputTokens The number of output tokens which were used. completion).
981984
*/
982985
@JsonInclude(Include.NON_NULL)
986+
@JsonIgnoreProperties(ignoreUnknown = true)
983987
public record Usage(
984988
// @formatter:off
985989
@JsonProperty("input_tokens") Integer inputTokens,
@@ -1078,6 +1082,7 @@ public String toString() {
10781082
* @param contentBlock The content block body.
10791083
*/
10801084
@JsonInclude(Include.NON_NULL)
1085+
@JsonIgnoreProperties(ignoreUnknown = true)
10811086
public record ContentBlockStartEvent(
10821087
// @formatter:off
10831088
@JsonProperty("type") EventType type,
@@ -1100,6 +1105,7 @@ public interface ContentBlockBody {
11001105
* @param input The tool use input.
11011106
*/
11021107
@JsonInclude(Include.NON_NULL)
1108+
@JsonIgnoreProperties(ignoreUnknown = true)
11031109
public record ContentBlockToolUse(
11041110
@JsonProperty("type") String type,
11051111
@JsonProperty("id") String id,
@@ -1113,6 +1119,7 @@ public record ContentBlockToolUse(
11131119
* @param text The text content.
11141120
*/
11151121
@JsonInclude(Include.NON_NULL)
1122+
@JsonIgnoreProperties(ignoreUnknown = true)
11161123
public record ContentBlockText(
11171124
@JsonProperty("type") String type,
11181125
@JsonProperty("text") String text) implements ContentBlockBody {
@@ -1130,6 +1137,7 @@ public record ContentBlockText(
11301137
* @param delta The content block delta body.
11311138
*/
11321139
@JsonInclude(Include.NON_NULL)
1140+
@JsonIgnoreProperties(ignoreUnknown = true)
11331141
public record ContentBlockDeltaEvent(
11341142
// @formatter:off
11351143
@JsonProperty("type") EventType type,
@@ -1153,6 +1161,7 @@ public interface ContentBlockDeltaBody {
11531161
* @param text The text content.
11541162
*/
11551163
@JsonInclude(Include.NON_NULL)
1164+
@JsonIgnoreProperties(ignoreUnknown = true)
11561165
public record ContentBlockDeltaText(
11571166
@JsonProperty("type") String type,
11581167
@JsonProperty("text") String text) implements ContentBlockDeltaBody {
@@ -1164,6 +1173,7 @@ public record ContentBlockDeltaText(
11641173
* @param partialJson The partial JSON content.
11651174
*/
11661175
@JsonInclude(Include.NON_NULL)
1176+
@JsonIgnoreProperties(ignoreUnknown = true)
11671177
public record ContentBlockDeltaJson(
11681178
@JsonProperty("type") String type,
11691179
@JsonProperty("partial_json") String partialJson) implements ContentBlockDeltaBody {
@@ -1174,6 +1184,8 @@ public record ContentBlockDeltaJson(
11741184
* @param type The content block type.
11751185
* @param thinking The thinking content.
11761186
*/
1187+
@JsonInclude(Include.NON_NULL)
1188+
@JsonIgnoreProperties(ignoreUnknown = true)
11771189
public record ContentBlockDeltaThinking(
11781190
@JsonProperty("type") String type,
11791191
@JsonProperty("thinking") String thinking) implements ContentBlockDeltaBody {
@@ -1184,6 +1196,8 @@ public record ContentBlockDeltaThinking(
11841196
* @param type The content block type.
11851197
* @param signature The signature content.
11861198
*/
1199+
@JsonInclude(Include.NON_NULL)
1200+
@JsonIgnoreProperties(ignoreUnknown = true)
11871201
public record ContentBlockDeltaSignature(
11881202
@JsonProperty("type") String type,
11891203
@JsonProperty("signature") String signature) implements ContentBlockDeltaBody {
@@ -1200,6 +1214,7 @@ public record ContentBlockDeltaSignature(
12001214
* @param index The index of the content block.
12011215
*/
12021216
@JsonInclude(Include.NON_NULL)
1217+
@JsonIgnoreProperties(ignoreUnknown = true)
12031218
public record ContentBlockStopEvent(
12041219
// @formatter:off
12051220
@JsonProperty("type") EventType type,
@@ -1214,6 +1229,7 @@ public record ContentBlockStopEvent(
12141229
* @param message The message body.
12151230
*/
12161231
@JsonInclude(Include.NON_NULL)
1232+
@JsonIgnoreProperties(ignoreUnknown = true)
12171233
public record MessageStartEvent(// @formatter:off
12181234
@JsonProperty("type") EventType type,
12191235
@JsonProperty("message") ChatCompletionResponse message) implements StreamEvent {
@@ -1228,6 +1244,7 @@ public record MessageStartEvent(// @formatter:off
12281244
* @param usage The message delta usage.
12291245
*/
12301246
@JsonInclude(Include.NON_NULL)
1247+
@JsonIgnoreProperties(ignoreUnknown = true)
12311248
public record MessageDeltaEvent(
12321249
// @formatter:off
12331250
@JsonProperty("type") EventType type,
@@ -1239,6 +1256,7 @@ public record MessageDeltaEvent(
12391256
* @param stopSequence The stop sequence.
12401257
*/
12411258
@JsonInclude(Include.NON_NULL)
1259+
@JsonIgnoreProperties(ignoreUnknown = true)
12421260
public record MessageDelta(
12431261
@JsonProperty("stop_reason") String stopReason,
12441262
@JsonProperty("stop_sequence") String stopSequence) {
@@ -1249,6 +1267,7 @@ public record MessageDelta(
12491267
* @param outputTokens The output tokens.
12501268
*/
12511269
@JsonInclude(Include.NON_NULL)
1270+
@JsonIgnoreProperties(ignoreUnknown = true)
12521271
public record MessageDeltaUsage(
12531272
@JsonProperty("output_tokens") Integer outputTokens) {
12541273
}
@@ -1261,6 +1280,7 @@ public record MessageDeltaUsage(
12611280
* @param type The event type.
12621281
*/
12631282
@JsonInclude(Include.NON_NULL)
1283+
@JsonIgnoreProperties(ignoreUnknown = true)
12641284
public record MessageStopEvent(
12651285
//@formatter:off
12661286
@JsonProperty("type") EventType type) implements StreamEvent {
@@ -1277,6 +1297,7 @@ public record MessageStopEvent(
12771297
* @param error The error body.
12781298
*/
12791299
@JsonInclude(Include.NON_NULL)
1300+
@JsonIgnoreProperties(ignoreUnknown = true)
12801301
public record ErrorEvent(
12811302
// @formatter:off
12821303
@JsonProperty("type") EventType type,
@@ -1288,6 +1309,7 @@ public record ErrorEvent(
12881309
* @param message The error message.
12891310
*/
12901311
@JsonInclude(Include.NON_NULL)
1312+
@JsonIgnoreProperties(ignoreUnknown = true)
12911313
public record Error(
12921314
@JsonProperty("type") String type,
12931315
@JsonProperty("message") String message) {
@@ -1304,6 +1326,7 @@ public record Error(
13041326
* @param type The event type.
13051327
*/
13061328
@JsonInclude(Include.NON_NULL)
1329+
@JsonIgnoreProperties(ignoreUnknown = true)
13071330
public record PingEvent(
13081331
// @formatter:off
13091332
@JsonProperty("type") EventType type) implements StreamEvent {

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.nio.charset.StandardCharsets;
2323
import java.time.Duration;
2424

25-
import com.fasterxml.jackson.annotation.JsonInclude;
25+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;import com.fasterxml.jackson.annotation.JsonInclude;
2626
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2727
import com.fasterxml.jackson.annotation.JsonProperty;
2828
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -331,6 +331,7 @@ protected Flux<SO> internalInvocationStream(I request, Class<SO> clazz) {
331331
* @param invocationLatency The time in milliseconds between the request being sent and the response being received.
332332
*/
333333
@JsonInclude(Include.NON_NULL)
334+
@JsonIgnoreProperties(ignoreUnknown = true)
334335
public record AmazonBedrockInvocationMetrics(
335336
@JsonProperty("inputTokenCount") Long inputTokenCount,
336337
@JsonProperty("firstByteLatency") Long firstByteLatency,

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/cohere/api/CohereEmbeddingBedrockApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020
import java.util.List;
2121

22+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2223
import com.fasterxml.jackson.annotation.JsonInclude;
2324
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2425
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -233,6 +234,7 @@ public enum Truncate {
233234
* doesn't return invocationMetrics for the cohere embedding model.
234235
*/
235236
@JsonInclude(Include.NON_NULL)
237+
@JsonIgnoreProperties(ignoreUnknown = true)
236238
public record CohereEmbeddingResponse(@JsonProperty("id") String id,
237239
@JsonProperty("embeddings") List<float[]> embeddings, @JsonProperty("texts") List<String> texts,
238240
@JsonProperty("response_type") String responseType,

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/titan/api/TitanEmbeddingBedrockApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020
import java.util.Map;
2121

22+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2223
import com.fasterxml.jackson.annotation.JsonInclude;
2324
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2425
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -174,6 +175,7 @@ public TitanEmbeddingRequest build() {
174175
* @param message No idea what this is.
175176
*/
176177
@JsonInclude(Include.NON_NULL)
178+
@JsonIgnoreProperties(ignoreUnknown = true)
177179
public record TitanEmbeddingResponse(
178180
@JsonProperty("embedding") float[] embedding,
179181
@JsonProperty("inputTextTokenCount") Integer inputTextTokenCount,

models/spring-ai-minimax/src/main/java/org/springframework/ai/minimax/api/MiniMaxApi.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.function.Predicate;
2424

2525
import com.fasterxml.jackson.annotation.JsonIgnore;
26+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2627
import com.fasterxml.jackson.annotation.JsonInclude;
2728
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2829
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -654,6 +655,7 @@ public record ResponseFormat(
654655
* {@link Role#ASSISTANT} role and null otherwise.
655656
*/
656657
@JsonInclude(Include.NON_NULL)
658+
@JsonIgnoreProperties(ignoreUnknown = true)
657659
public record ChatCompletionMessage(
658660
@JsonProperty("content") Object rawContent,
659661
@JsonProperty("role") Role role,
@@ -719,6 +721,7 @@ public enum Role {
719721
* images by adding multiple image_url content parts.
720722
*/
721723
@JsonInclude(Include.NON_NULL)
724+
@JsonIgnoreProperties(ignoreUnknown = true)
722725
public record MediaContent(
723726
@JsonProperty("type") String type,
724727
@JsonProperty("text") String text,
@@ -748,6 +751,7 @@ public MediaContent(ImageUrl imageUrl) {
748751
* @param detail Specifies the detail level of the image.
749752
*/
750753
@JsonInclude(Include.NON_NULL)
754+
@JsonIgnoreProperties(ignoreUnknown = true)
751755
public record ImageUrl(
752756
@JsonProperty("url") String url,
753757
@JsonProperty("detail") String detail) {
@@ -766,6 +770,7 @@ public ImageUrl(String url) {
766770
* @param function The function definition.
767771
*/
768772
@JsonInclude(Include.NON_NULL)
773+
@JsonIgnoreProperties(ignoreUnknown = true)
769774
public record ToolCall(
770775
@JsonProperty("id") String id,
771776
@JsonProperty("type") String type,
@@ -779,6 +784,7 @@ public record ToolCall(
779784
* @param arguments The arguments that the model expects you to pass to the function.
780785
*/
781786
@JsonInclude(Include.NON_NULL)
787+
@JsonIgnoreProperties(ignoreUnknown = true)
782788
public record ChatCompletionFunction(
783789
@JsonProperty("name") String name,
784790
@JsonProperty("arguments") String arguments) {
@@ -800,6 +806,7 @@ public record ChatCompletionFunction(
800806
* @param usage Usage statistics for the completion request.
801807
*/
802808
@JsonInclude(Include.NON_NULL)
809+
@JsonIgnoreProperties(ignoreUnknown = true)
803810
public record ChatCompletion(
804811
@JsonProperty("id") String id,
805812
@JsonProperty("choices") List<Choice> choices,
@@ -821,6 +828,7 @@ public record ChatCompletion(
821828
* @param logprobs Log probability information for the choice.
822829
*/
823830
@JsonInclude(Include.NON_NULL)
831+
@JsonIgnoreProperties(ignoreUnknown = true)
824832
public record Choice(
825833
@JsonProperty("finish_reason") ChatCompletionFinishReason finishReason,
826834
@JsonProperty("index") Integer index,
@@ -829,6 +837,8 @@ public record Choice(
829837
@JsonProperty("logprobs") LogProbs logprobs) {
830838
}
831839

840+
@JsonInclude(Include.NON_NULL)
841+
@JsonIgnoreProperties(ignoreUnknown = true)
832842
public record BaseResponse(
833843
@JsonProperty("status_code") Long statusCode,
834844
@JsonProperty("status_msg") String message
@@ -841,6 +851,7 @@ public record BaseResponse(
841851
* @param content A list of message content tokens with log probability information.
842852
*/
843853
@JsonInclude(Include.NON_NULL)
854+
@JsonIgnoreProperties(ignoreUnknown = true)
844855
public record LogProbs(
845856
@JsonProperty("content") List<Content> content) {
846857

@@ -858,6 +869,7 @@ public record LogProbs(
858869
* requested top_logprobs returned.
859870
*/
860871
@JsonInclude(Include.NON_NULL)
872+
@JsonIgnoreProperties(ignoreUnknown = true)
861873
public record Content(
862874
@JsonProperty("token") String token,
863875
@JsonProperty("logprob") Float logprob,
@@ -875,6 +887,7 @@ public record Content(
875887
* text representation. Can be null if there is no bytes representation for the token.
876888
*/
877889
@JsonInclude(Include.NON_NULL)
890+
@JsonIgnoreProperties(ignoreUnknown = true)
878891
public record TopLogProbs(
879892
@JsonProperty("token") String token,
880893
@JsonProperty("logprob") Float logprob,
@@ -891,6 +904,7 @@ public record TopLogProbs(
891904
* @param totalTokens Total number of tokens used in the request (prompt + completion).
892905
*/
893906
@JsonInclude(Include.NON_NULL)
907+
@JsonIgnoreProperties(ignoreUnknown = true)
894908
public record Usage(
895909
@JsonProperty("completion_tokens") Integer completionTokens,
896910
@JsonProperty("prompt_tokens") Integer promptTokens,
@@ -912,6 +926,7 @@ public record Usage(
912926
* @param object The object type, which is always 'chat.completion.chunk'.
913927
*/
914928
@JsonInclude(Include.NON_NULL)
929+
@JsonIgnoreProperties(ignoreUnknown = true)
915930
public record ChatCompletionChunk(
916931
@JsonProperty("id") String id,
917932
@JsonProperty("choices") List<ChunkChoice> choices,
@@ -929,6 +944,7 @@ public record ChatCompletionChunk(
929944
* @param logprobs Log probability information for the choice.
930945
*/
931946
@JsonInclude(Include.NON_NULL)
947+
@JsonIgnoreProperties(ignoreUnknown = true)
932948
public record ChunkChoice(
933949
@JsonProperty("finish_reason") ChatCompletionFinishReason finishReason,
934950
@JsonProperty("index") Integer index,
@@ -1021,6 +1037,7 @@ public EmbeddingRequest(List<String> texts, EmbeddingType type) {
10211037
* @param totalTokens Usage tokens the request.
10221038
*/
10231039
@JsonInclude(Include.NON_NULL)
1040+
@JsonIgnoreProperties(ignoreUnknown = true)
10241041
public record EmbeddingList(
10251042
@JsonProperty("vectors") List<float[]> vectors,
10261043
@JsonProperty("model") String model,

0 commit comments

Comments
 (0)