Commit 3c3c3eb
feat: Add native structured output support for ChatClient
Implement StructuredOutputChatOptions interface to provide unified structured output
support across AI providers. This enables AI models that provide built-in structured
output to natively generate JSON responses that conform to a specified schema
without additional prompt engineering.
Models that provide structured response should implement the StructuredOutputChatOptions.
To activate the native over the ChatClient prompt-based structured output response**,**
you need to add the AdvisorParams.WITH_NATIVE_STRUCTURED_OUTPUT advisor parameter to your
ChatClient configuration.
- Add StructuredOutputChatOptions interface with getOutputSchema/setOutputSchema methods
- Implement interface in AnthropicChatOptions, OpenAiChatOptions, and VertexAiGeminiChatOptions
- Update AnthropicApi to support output_format parameter and add structured-outputs-2025-11-13 beta version
- Add ChatClientAttributes for STRUCTURED_OUTPUT_SCHEMA and STRUCTURED_OUTPUT_NATIVE
- Enhance ChatModelCallAdvisor to set output schema when native structured output is enabled
- Update DefaultChatClient to handle native structured output via context attributes
- Configure BeanOutputConverter to mark all fields as required in generated JSON schemas
- Add AdvisorParams.WITH_NATIVE_STRUCTURED_OUTPUT for easy activation via ChatClient
- Add integration tests for native structured output across all three providers
Fixes #4889
Addresses #4463
Part of #2787
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Add documentation
feat: add structured output support to Google GenAI chat model
- Implement StructuredOutputChatOptions interface in GoogleGenAiChatOptions
- Add responseSchema field and related getter/setter methods
- Add outputSchema bridge methods for unified Spring AI API
- Update GoogleGenAiChatModel to handle responseSchema configuration
- Add integration tests for both native and unified structured output APIs
- Include tests for ChatClient with native structured output advisor
- Update VertexAI Gemini tests with consistent naming and structured output support
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>1 parent a1f32d1 commit 3c3c3eb
File tree
21 files changed
+772
-49
lines changed- models
- spring-ai-anthropic/src
- main/java/org/springframework/ai/anthropic
- api
- test/java/org/springframework/ai/anthropic/client
- spring-ai-google-genai/src
- main/java/org/springframework/ai/google/genai
- test/java/org/springframework/ai/google/genai
- spring-ai-openai/src
- main/java/org/springframework/ai/openai
- test/java/org/springframework/ai/openai/chat/client
- spring-ai-vertex-ai-gemini/src
- main/java/org/springframework/ai/vertexai/gemini
- test/java/org/springframework/ai/vertexai/gemini
- spring-ai-client-chat/src
- main/java/org/springframework/ai/chat/client
- advisor
- test/java/org/springframework/ai/chat/client
- spring-ai-docs/src/main/antora/modules/ROOT/pages/api
- spring-ai-model/src
- main/java/org/springframework/ai
- converter
- model/tool
- test
- java/org/springframework/ai/converter
- kotlin/org/springframework/ai/converter
21 files changed
+772
-49
lines changedLines changed: 43 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
| |||
51 | 54 | | |
52 | 55 | | |
53 | 56 | | |
54 | | - | |
| 57 | + | |
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
| |||
115 | 118 | | |
116 | 119 | | |
117 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
118 | 126 | | |
119 | 127 | | |
120 | 128 | | |
| |||
150 | 158 | | |
151 | 159 | | |
152 | 160 | | |
| 161 | + | |
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
| |||
343 | 352 | | |
344 | 353 | | |
345 | 354 | | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
346 | 376 | | |
347 | 377 | | |
348 | 378 | | |
| |||
369 | 399 | | |
370 | 400 | | |
371 | 401 | | |
| 402 | + | |
372 | 403 | | |
373 | 404 | | |
374 | 405 | | |
| |||
378 | 409 | | |
379 | 410 | | |
380 | 411 | | |
381 | | - | |
| 412 | + | |
382 | 413 | | |
383 | 414 | | |
384 | 415 | | |
| |||
520 | 551 | | |
521 | 552 | | |
522 | 553 | | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
523 | 564 | | |
524 | 565 | | |
525 | 566 | | |
| |||
Lines changed: 23 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
205 | | - | |
| 205 | + | |
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
| |||
837 | 837 | | |
838 | 838 | | |
839 | 839 | | |
| 840 | + | |
840 | 841 | | |
841 | 842 | | |
842 | 843 | | |
843 | 844 | | |
844 | 845 | | |
845 | 846 | | |
846 | | - | |
| 847 | + | |
847 | 848 | | |
848 | 849 | | |
849 | 850 | | |
850 | 851 | | |
851 | 852 | | |
852 | | - | |
| 853 | + | |
853 | 854 | | |
854 | 855 | | |
855 | 856 | | |
| |||
860 | 861 | | |
861 | 862 | | |
862 | 863 | | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
863 | 873 | | |
864 | 874 | | |
865 | 875 | | |
| |||
929 | 939 | | |
930 | 940 | | |
931 | 941 | | |
| 942 | + | |
| 943 | + | |
932 | 944 | | |
933 | 945 | | |
934 | 946 | | |
| |||
947 | 959 | | |
948 | 960 | | |
949 | 961 | | |
| 962 | + | |
950 | 963 | | |
951 | 964 | | |
952 | 965 | | |
| |||
1036 | 1049 | | |
1037 | 1050 | | |
1038 | 1051 | | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
1039 | 1057 | | |
1040 | 1058 | | |
1041 | 1059 | | |
1042 | | - | |
| 1060 | + | |
1043 | 1061 | | |
1044 | 1062 | | |
1045 | 1063 | | |
| |||
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
121 | 141 | | |
122 | 142 | | |
123 | 143 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
748 | 748 | | |
749 | 749 | | |
750 | 750 | | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
751 | 754 | | |
752 | 755 | | |
753 | 756 | | |
| |||
Lines changed: 43 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
53 | | - | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
101 | 107 | | |
102 | 108 | | |
103 | 109 | | |
| |||
220 | 226 | | |
221 | 227 | | |
222 | 228 | | |
| 229 | + | |
223 | 230 | | |
224 | | - | |
225 | 231 | | |
226 | 232 | | |
227 | 233 | | |
| |||
318 | 324 | | |
319 | 325 | | |
320 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
321 | 335 | | |
322 | 336 | | |
323 | 337 | | |
| |||
472 | 486 | | |
473 | 487 | | |
474 | 488 | | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
475 | 501 | | |
476 | 502 | | |
477 | 503 | | |
| |||
491 | 517 | | |
492 | 518 | | |
493 | 519 | | |
| 520 | + | |
494 | 521 | | |
495 | 522 | | |
496 | 523 | | |
| |||
501 | 528 | | |
502 | 529 | | |
503 | 530 | | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
508 | 535 | | |
509 | 536 | | |
510 | 537 | | |
| |||
591 | 618 | | |
592 | 619 | | |
593 | 620 | | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
594 | 631 | | |
595 | 632 | | |
596 | 633 | | |
| |||
0 commit comments