Commit 5aff1d3
committed
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 <[email protected]>1 parent 10bc0a7 commit 5aff1d3
File tree
14 files changed
+509
-25
lines changed- models
- spring-ai-anthropic/src
- main/java/org/springframework/ai/anthropic
- api
- test/java/org/springframework/ai/anthropic/client
- 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-model/src/main/java/org/springframework/ai
- converter
- model/tool
14 files changed
+509
-25
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 | | |
| |||
141 | 149 | | |
142 | 150 | | |
143 | 151 | | |
| 152 | + | |
144 | 153 | | |
145 | 154 | | |
146 | 155 | | |
| |||
325 | 334 | | |
326 | 335 | | |
327 | 336 | | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
328 | 358 | | |
329 | 359 | | |
330 | 360 | | |
| |||
351 | 381 | | |
352 | 382 | | |
353 | 383 | | |
| 384 | + | |
354 | 385 | | |
355 | 386 | | |
356 | 387 | | |
| |||
359 | 390 | | |
360 | 391 | | |
361 | 392 | | |
362 | | - | |
| 393 | + | |
363 | 394 | | |
364 | 395 | | |
365 | 396 | | |
| |||
501 | 532 | | |
502 | 533 | | |
503 | 534 | | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
504 | 545 | | |
505 | 546 | | |
506 | 547 | | |
| |||
Lines changed: 24 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 | | |
| |||
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
533 | | - | |
| 533 | + | |
| 534 | + | |
534 | 535 | | |
535 | 536 | | |
536 | 537 | | |
537 | 538 | | |
538 | | - | |
| 539 | + | |
| 540 | + | |
539 | 541 | | |
540 | 542 | | |
541 | 543 | | |
542 | 544 | | |
543 | 545 | | |
544 | | - | |
| 546 | + | |
545 | 547 | | |
546 | 548 | | |
547 | 549 | | |
| |||
552 | 554 | | |
553 | 555 | | |
554 | 556 | | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
555 | 566 | | |
556 | 567 | | |
557 | 568 | | |
| |||
619 | 630 | | |
620 | 631 | | |
621 | 632 | | |
| 633 | + | |
| 634 | + | |
622 | 635 | | |
623 | 636 | | |
624 | 637 | | |
| |||
636 | 649 | | |
637 | 650 | | |
638 | 651 | | |
| 652 | + | |
639 | 653 | | |
640 | 654 | | |
641 | 655 | | |
| |||
713 | 727 | | |
714 | 728 | | |
715 | 729 | | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
716 | 735 | | |
717 | 736 | | |
718 | 737 | | |
719 | | - | |
| 738 | + | |
720 | 739 | | |
721 | 740 | | |
722 | 741 | | |
| |||
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: 20 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| 44 | + | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
| |||
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
58 | | - | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| |||
675 | 677 | | |
676 | 678 | | |
677 | 679 | | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
678 | 692 | | |
679 | 693 | | |
680 | 694 | | |
| |||
871 | 885 | | |
872 | 886 | | |
873 | 887 | | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
874 | 893 | | |
875 | 894 | | |
876 | 895 | | |
| |||
Lines changed: 32 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
100 | | - | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
198 | 213 | | |
199 | 214 | | |
200 | 215 | | |
| |||
210 | 225 | | |
211 | 226 | | |
212 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
213 | 244 | | |
214 | 245 | | |
215 | 246 | | |
| |||
0 commit comments