Skip to content

feat: Add metadata support to PromptUserSpec and PromptSystemSpec in ChatClient #3989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025

Conversation

YunKuiLu
Copy link
Contributor

@YunKuiLu YunKuiLu commented Aug 4, 2025

  • Add metadata methods to PromptUserSpec and PromptSystemSpec interfaces
  • Update DefaultChatClientRequestSpec to handle user and system metadata
  • Modify DefaultChatClientUtils to include metadata in SystemMessage and UserMessage builders
  • Add comprehensive test coverage for metadata functionality

This PR allows users to set the metadata field for UserMessage and SystemMessage through Consumer<PromptUserSpec> and Consumer<PromptSystemSpec>.

Users can set information like message ID through metadata, and then get them in ChatMemoryRepository.
Example as follows:

  private static final ChatMemoryRepository myChatMemoryRepository = new ChatMemoryRepository() {
      @Override
      public void saveAll(String conversationId, List<Message> messages) {
          log.info("messages={}", messages);
          log.info("id={}", messages.getLast().getMetadata().get("id"));
      }
  };
  
  @GetMapping("/ai/tool")
  public String tool() {
      String userMessageId = UUID.randomUUID().toString();
  
      ChatResponse result = chatClient.prompt()
              .system(s -> s.text("You are a helpful assistant.")
                           // Allow setting metadata for SystemMessage.
                           .metadata("version", "v1.0"))
              .tools(this)
              .user(u -> u.text("What is the weather in New York?")
                          // Allow setting metadata for UserMessage.
                          .metadata("id", userMessageId))
              .advisors(MessageChatMemoryAdvisor.builder(
                      MessageWindowChatMemory.builder()
                              .chatMemoryRepository(myChatMemoryRepository)
                              .maxMessages(10).build()
              ).build())
              .call()
              .chatResponse();
  
      return result.getResult().getOutput().getText();
  }

- Add metadata methods to PromptUserSpec and PromptSystemSpec interfaces
- Update DefaultChatClientRequestSpec to handle user and system metadata
- Modify DefaultChatClientUtils to include metadata in SystemMessage and UserMessage builders
- Add comprehensive test coverage for metadata functionality

Signed-off-by: YunKui Lu <[email protected]>
@YunKuiLu YunKuiLu changed the title feat: Add metadata support to user and system messages in ChatClient feat: Add metadata support to PromptUserSpec and PromptSystemSpec in ChatClient Aug 4, 2025
@sobychacko sobychacko added this to the 1.1.0.M1 milestone Aug 6, 2025
@sobychacko sobychacko merged commit e547661 into spring-projects:main Aug 6, 2025
2 checks passed
@sobychacko
Copy link
Contributor

@YunKuiLu Merged upstream. Also, I added a section in the reference documentation for this feature. See this commit: c328ef6

@YunKuiLu YunKuiLu deleted the user_metadata branch August 7, 2025 14:08
juntae6942 pushed a commit to juntae6942/spring-ai that referenced this pull request Aug 7, 2025
…ent (spring-projects#3989)

- Add metadata methods to PromptUserSpec and PromptSystemSpec interfaces
- Update DefaultChatClientRequestSpec to handle user and system metadata
- Modify DefaultChatClientUtils to include metadata in SystemMessage and UserMessage builders
- Add comprehensive test coverage for metadata functionality

Signed-off-by: YunKui Lu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants