Commit 195c4fd
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 8a6e39d commit 195c4fd
File tree
21 files changed
+773
-48
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
+773
-48
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 | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| |||
542 | 542 | | |
543 | 543 | | |
544 | 544 | | |
545 | | - | |
| 545 | + | |
| 546 | + | |
546 | 547 | | |
547 | 548 | | |
548 | 549 | | |
549 | 550 | | |
550 | | - | |
| 551 | + | |
| 552 | + | |
551 | 553 | | |
552 | 554 | | |
553 | 555 | | |
554 | 556 | | |
555 | 557 | | |
556 | | - | |
| 558 | + | |
557 | 559 | | |
558 | 560 | | |
559 | 561 | | |
| |||
564 | 566 | | |
565 | 567 | | |
566 | 568 | | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
567 | 578 | | |
568 | 579 | | |
569 | 580 | | |
| |||
631 | 642 | | |
632 | 643 | | |
633 | 644 | | |
| 645 | + | |
| 646 | + | |
634 | 647 | | |
635 | 648 | | |
636 | 649 | | |
| |||
648 | 661 | | |
649 | 662 | | |
650 | 663 | | |
| 664 | + | |
651 | 665 | | |
652 | 666 | | |
653 | 667 | | |
| |||
725 | 739 | | |
726 | 740 | | |
727 | 741 | | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
728 | 747 | | |
729 | 748 | | |
730 | 749 | | |
731 | | - | |
| 750 | + | |
732 | 751 | | |
733 | 752 | | |
734 | 753 | | |
| |||
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 | |
|---|---|---|---|
| |||
707 | 707 | | |
708 | 708 | | |
709 | 709 | | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
710 | 713 | | |
711 | 714 | | |
712 | 715 | | |
| |||
Lines changed: 42 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
100 | 106 | | |
101 | 107 | | |
102 | 108 | | |
| |||
199 | 205 | | |
200 | 206 | | |
201 | 207 | | |
| 208 | + | |
202 | 209 | | |
203 | | - | |
204 | 210 | | |
205 | 211 | | |
206 | 212 | | |
| |||
295 | 301 | | |
296 | 302 | | |
297 | 303 | | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
298 | 312 | | |
299 | 313 | | |
300 | 314 | | |
| |||
433 | 447 | | |
434 | 448 | | |
435 | 449 | | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
436 | 462 | | |
437 | 463 | | |
438 | 464 | | |
| |||
450 | 476 | | |
451 | 477 | | |
452 | 478 | | |
| 479 | + | |
453 | 480 | | |
454 | 481 | | |
455 | 482 | | |
| |||
461 | 488 | | |
462 | 489 | | |
463 | 490 | | |
464 | | - | |
465 | | - | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
466 | 494 | | |
467 | 495 | | |
468 | 496 | | |
| |||
548 | 576 | | |
549 | 577 | | |
550 | 578 | | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
551 | 589 | | |
552 | 590 | | |
553 | 591 | | |
| |||
0 commit comments