All Implemented Interfaces:
io.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse,OpenAIRequest>

public class KimiFormatter extends OpenAIChatFormatter
Formatter for Kimi (Moonshot AI) models (kimi-k3, kimi-k2.7-code, kimi-k2.6, kimi-k2.5, moonshot-v1 series).

Adapted to the latest Kimi open platform Chat Completions API (https://api.moonshot.cn/v1/chat/completions):

  • On kimi-* models, temperature / top_p / n / frequency_penalty / presence_penalty are fixed by the platform and the API rejects requests carrying other values; these parameters are stripped from the request. The moonshot-v1 series still accepts them, so they are kept there.
  • reasoning_effort ("low" / "high" / "max", default "max") is only supported by kimi-k3; it is stripped for other models
  • thinking_budget is not a Kimi parameter and is always stripped; K2.x thinking is controlled through the thinking body parameter instead (see below)
  • Only max_completion_tokens is documented; max_tokens is mapped to max_completion_tokens when the latter is not set. Reasoning tokens count towards max_completion_tokens, so the official guide recommends max_completion_tokens >= 16000 for thinking models
  • tool_choice supports auto / none on all models; required is degraded to auto on the K2.x series; forcing a specific function is incompatible with thinking enabled (HTTP 400), so it is degraded to auto on always-thinking models (kimi-k3, kimi-k2.7-code) and on kimi-k2.6 / kimi-k2.5 unless thinking is explicitly disabled
  • The strict parameter in tool definitions is not documented by Kimi and is not sent
  • Assistant reasoning_content is preserved in the request history (inherited from the base message converter), as required by Preserved Thinking on kimi-k3 / kimi-k2.7-code and by thinking.keep = "all" on kimi-k2.6

Thinking mode: kimi-k2.6 / kimi-k2.5 accept a thinking body parameter (e.g. {"type": "disabled"}), which can be passed through GenerateOptions.additionalBodyParam("thinking", Map.of("type", "disabled")). kimi-k3 uses the top-level reasoning_effort option (GenerateOptions.reasoningEffort) instead. JSON mode is enabled via response_format = {"type": "json_object"}; json_schema is not supported.

Usage:


 OpenAIChatModel.builder()
     .formatter(new KimiFormatter())
     .modelName("kimi-k3")
     .baseUrl("https://api.moonshot.cn/v1")
     .apiKey(apiKey)
     .build();
 
See Also:
  • Constructor Details

    • KimiFormatter

      public KimiFormatter()
  • Method Details

    • supportsStrict

      protected boolean supportsStrict()
      Description copied from class: OpenAIChatFormatter
      Returns whether this formatter's target API supports the strict parameter in tool definitions.

      Subclasses can override this method to disable strict parameter for providers that don't support it (e.g., DeepSeek).

      Overrides:
      supportsStrict in class OpenAIChatFormatter
      Returns:
      true if strict parameter is supported, false otherwise
    • applyOptions

      public void applyOptions(OpenAIRequest request, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions)
      Description copied from class: OpenAIBaseFormatter
      Apply generation options to the request. Subclasses implement provider-specific option handling.
      Specified by:
      applyOptions in interface io.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse,OpenAIRequest>
      Overrides:
      applyOptions in class OpenAIChatFormatter
      Parameters:
      request - OpenAI request DTO
      options - Generation options to apply
      defaultOptions - Default options to use if options parameter is null
    • applyToolChoice

      public void applyToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice)
      Description copied from class: OpenAIBaseFormatter
      Apply tool choice configuration to the request. Subclasses implement provider-specific tool choice handling.
      Specified by:
      applyToolChoice in interface io.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse,OpenAIRequest>
      Overrides:
      applyToolChoice in class OpenAIChatFormatter
      Parameters:
      request - OpenAI request DTO
      toolChoice - Tool choice configuration (null means auto)
    • sanitizeKimiRequest

      protected static void sanitizeKimiRequest(OpenAIRequest request)
      Sanitize the request according to the Kimi Chat Completions API reference.

      Applied adaptations:

      • temperature / top_p / n / frequency_penalty / presence_penalty are removed on kimi-* models (fixed by the platform; other values are rejected)
      • reasoning_effort is removed on models other than kimi-k3
      • thinking_budget is always removed (not a Kimi parameter)
      • max_tokens is mapped to max_completion_tokens when the latter is not set

      This method is static to allow sharing with KimiMultiAgentFormatter.

      Parameters:
      request - the request to sanitize
    • applyKimiToolChoice

      protected static void applyKimiToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice)
      Apply Kimi-specific tool choice handling.

      Per the official tool choice guide:

      • auto / none are supported on all models
      • required is only supported by kimi-k3; it is degraded to auto on the K2.x series, which rejects it
      • Forcing a specific function ({"type": "function", ...}) is incompatible with thinking enabled (HTTP 400); it is degraded to auto on always-thinking models and on kimi-k2.6 / kimi-k2.5 unless thinking is explicitly disabled via the thinking body parameter

      This method is static to allow sharing with KimiMultiAgentFormatter.

      Parameters:
      request - the request to apply tool choice to
      toolChoice - the requested tool choice