Commit 040697e
committed
feat: Extend ChatClient to leverage native structured output from ChatModel implementations
Add ChatClient support for utilizing native structured output capabilities provided
by underlying ChatModel implementations (Anthropic, OpenAI, Vertex AI Gemini).
Changes:
- Introduce StructuredOutputChatOptions interface for ChatModels to advertise native support
- Extend ChatClient to detect and utilize native structured output when available
- Add STRUCTURED_OUTPUT_SCHEMA attribute to ChatClientAttributes for schema propagation
- Enhance ChatModelCallAdvisor to configure native structured output instead of prompt-based formatting
- Update DefaultChatClient to capture and propagate output schema to chat options
- Implement StructuredOutputChatOptions in AnthropicChatOptions with outputFormat field
- Implement StructuredOutputChatOptions in OpenAiChatOptions using responseFormat
- Implement StructuredOutputChatOptions in VertexAiGeminiChatOptions with responseSchema
- Update AnthropicApi to include structured-outputs-2025-11-13 beta version
- Modify BeanOutputConverter to mark all fields as required by default
- Add comprehensive integration tests across all providers
When nativeStructuredOutput=true is set in chat options, ChatClient automatically
configures the underlying ChatModel's native structured output capabilities instead
of using traditional prompt-based JSON formatting instructions.
Closes #4463
Fixes #4889
Part of #2787
Signed-off-by: Christian Tzolov <[email protected]>1 parent 10bc0a7 commit 040697e
File tree
12 files changed
+354
-26
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
- spring-ai-model/src/main/java/org/springframework/ai
- converter
- model/tool
12 files changed
+354
-26
lines changedLines changed: 62 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 | + | |
| 126 | + | |
| 127 | + | |
118 | 128 | | |
119 | 129 | | |
120 | 130 | | |
| |||
141 | 151 | | |
142 | 152 | | |
143 | 153 | | |
| 154 | + | |
| 155 | + | |
144 | 156 | | |
145 | 157 | | |
146 | 158 | | |
| |||
325 | 337 | | |
326 | 338 | | |
327 | 339 | | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
328 | 371 | | |
329 | 372 | | |
330 | 373 | | |
| |||
351 | 394 | | |
352 | 395 | | |
353 | 396 | | |
| 397 | + | |
| 398 | + | |
354 | 399 | | |
355 | 400 | | |
356 | 401 | | |
| |||
359 | 404 | | |
360 | 405 | | |
361 | 406 | | |
362 | | - | |
| 407 | + | |
363 | 408 | | |
364 | 409 | | |
365 | 410 | | |
| |||
501 | 546 | | |
502 | 547 | | |
503 | 548 | | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
504 | 564 | | |
505 | 565 | | |
506 | 566 | | |
| |||
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: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
121 | 139 | | |
122 | 140 | | |
123 | 141 | | |
| |||
Lines changed: 42 additions & 2 deletions
| 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 | | |
| |||
294 | 296 | | |
295 | 297 | | |
296 | 298 | | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
297 | 302 | | |
298 | 303 | | |
299 | 304 | | |
| |||
339 | 344 | | |
340 | 345 | | |
341 | 346 | | |
| 347 | + | |
342 | 348 | | |
343 | 349 | | |
344 | 350 | | |
| |||
675 | 681 | | |
676 | 682 | | |
677 | 683 | | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
678 | 706 | | |
679 | 707 | | |
680 | 708 | | |
| |||
688 | 716 | | |
689 | 717 | | |
690 | 718 | | |
691 | | - | |
| 719 | + | |
| 720 | + | |
692 | 721 | | |
693 | 722 | | |
694 | 723 | | |
| |||
726 | 755 | | |
727 | 756 | | |
728 | 757 | | |
| 758 | + | |
729 | 759 | | |
730 | 760 | | |
731 | 761 | | |
| |||
871 | 901 | | |
872 | 902 | | |
873 | 903 | | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
874 | 909 | | |
875 | 910 | | |
876 | 911 | | |
| |||
1009 | 1044 | | |
1010 | 1045 | | |
1011 | 1046 | | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
1012 | 1052 | | |
1013 | 1053 | | |
1014 | 1054 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
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