Class MessageConverter

java.lang.Object
com.google.adk.models.springai.MessageConverter

public class MessageConverter extends Object
Converts between ADK and Spring AI message, request, and response formats in both directions.

This converter handles the translation between ADK's Content/Part and LlmRequest/LlmResponse formats and Spring AI's Message and Prompt/ChatResponse formats.

  • Constructor Summary

    Constructors
    Constructor
    Description
    MessageConverter(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets tool registry from ADK tools for internal tracking.
    com.google.genai.types.Content
    toAdkContent(org.springframework.ai.chat.messages.Message message)
    Converts a Spring AI Message to an ADK Content.
    org.springframework.ai.chat.model.ChatResponse
    toChatResponse(@Nullable LlmResponse llmResponse)
    Converts an ADK LlmResponse to a Spring AI ChatResponse.
    org.springframework.ai.chat.prompt.Prompt
    toLlmPrompt(LlmRequest llmRequest)
    Converts an ADK LlmRequest to a Spring AI Prompt.
    org.springframework.ai.chat.prompt.Prompt
    toLlmPrompt(LlmRequest llmRequest, @Nullable org.springframework.ai.chat.prompt.ChatOptions modelDefaultOptions)
    Converts an ADK LlmRequest to a Spring AI Prompt, using the target model's own default options as the base for the prompt options.
    toLlmRequest(org.springframework.ai.chat.prompt.Prompt prompt)
    Converts a Spring AI Prompt to an ADK LlmRequest.
    toLlmResponse(@Nullable org.springframework.ai.chat.model.ChatResponse chatResponse)
    Converts a Spring AI ChatResponse to an ADK LlmResponse.
    toLlmResponse(@Nullable org.springframework.ai.chat.model.ChatResponse chatResponse, boolean isStreaming)
    Converts a Spring AI ChatResponse to an ADK LlmResponse with streaming context.
    com.google.common.collect.ImmutableList<org.springframework.ai.chat.messages.Message>
    toSpringAiMessages(com.google.genai.types.Content content)
    Converts an ADK Content to one or more Spring AI Messages.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MessageConverter

      public MessageConverter(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
  • Method Details

    • toLlmPrompt

      public org.springframework.ai.chat.prompt.Prompt toLlmPrompt(LlmRequest llmRequest)
      Converts an ADK LlmRequest to a Spring AI Prompt.
      Parameters:
      llmRequest - The ADK request to convert
      Returns:
      A Spring AI Prompt
    • toLlmPrompt

      public org.springframework.ai.chat.prompt.Prompt toLlmPrompt(LlmRequest llmRequest, @Nullable org.springframework.ai.chat.prompt.ChatOptions modelDefaultOptions)
      Converts an ADK LlmRequest to a Spring AI Prompt, using the target model's own default options as the base for the prompt options.

      Provider-specific chat models (for example Spring AI OpenAI 2.0.0) cast Prompt.getOptions() directly to their own options type (e.g. OpenAiChatOptions) in createRequest(...). Passing provider-neutral options such as DefaultToolCallingChatOptions therefore triggers a ClassCastException. To stay compatible with any provider, the prompt options are built on top of the model's own default options (obtained via ChatModel.getOptions()) so the resulting options keep the concrete type the provider expects, while overlaying the ADK tools and generation config.

      Parameters:
      llmRequest - The ADK request to convert
      modelDefaultOptions - The target model's default options, or null if unavailable
      Returns:
      A Spring AI Prompt
    • toLlmRequest

      public LlmRequest toLlmRequest(org.springframework.ai.chat.prompt.Prompt prompt)
      Converts a Spring AI Prompt to an ADK LlmRequest.

      System messages are extracted into the request's system instructions, non-system messages are converted to Content turns, and Prompt.getOptions() is mapped to GenerateContentConfig, model name, and ADK tools.

      Parameters:
      prompt - The Spring AI prompt to convert
      Returns:
      An ADK LlmRequest
    • toSpringAiMessages

      public com.google.common.collect.ImmutableList<org.springframework.ai.chat.messages.Message> toSpringAiMessages(com.google.genai.types.Content content)
      Converts an ADK Content to one or more Spring AI Messages.
      Parameters:
      content - The ADK content to convert
      Returns:
      An immutable list of Spring AI messages
    • toAdkContent

      public com.google.genai.types.Content toAdkContent(org.springframework.ai.chat.messages.Message message)
      Converts a Spring AI Message to an ADK Content.
      Parameters:
      message - The Spring AI message to convert
      Returns:
      The converted ADK Content
    • toLlmResponse

      public LlmResponse toLlmResponse(@Nullable org.springframework.ai.chat.model.ChatResponse chatResponse)
      Converts a Spring AI ChatResponse to an ADK LlmResponse.
      Parameters:
      chatResponse - The Spring AI response to convert
      Returns:
      An ADK LlmResponse
    • toLlmResponse

      public LlmResponse toLlmResponse(@Nullable org.springframework.ai.chat.model.ChatResponse chatResponse, boolean isStreaming)
      Converts a Spring AI ChatResponse to an ADK LlmResponse with streaming context.
      Parameters:
      chatResponse - The Spring AI response to convert
      isStreaming - Whether this is part of a streaming response
      Returns:
      An ADK LlmResponse
    • toChatResponse

      public org.springframework.ai.chat.model.ChatResponse toChatResponse(@Nullable LlmResponse llmResponse)
      Converts an ADK LlmResponse to a Spring AI ChatResponse.
      Parameters:
      llmResponse - The ADK response to convert
      Returns:
      A Spring AI ChatResponse
    • getToolRegistry

      public Map<String,ToolConverter.ToolMetadata> getToolRegistry(LlmRequest llmRequest)
      Gets tool registry from ADK tools for internal tracking.
      Parameters:
      llmRequest - The ADK request containing tools
      Returns:
      Map of tool metadata for tracking available tools