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

public class GLMFormatter extends OpenAIChatFormatter
Formatter for Zhipu AI (Z.ai) GLM models (glm-5.2, glm-5.1, glm-5, glm-4.7, glm-4.6, etc.).

Adapted to the latest Zhipu open platform Chat Completions API (https://open.bigmodel.cn/api/paas/v4/chat/completions):

  • At least one user message is required (error 1214 otherwise)
  • Does NOT support the name parameter in messages
  • tool_choice only supports "auto"; forced choices are degraded, while none removes tools to preserve the no-tool-call contract
  • Does NOT support the strict parameter in tool definitions
  • Does NOT support frequency_penalty / presence_penalty / thinking_budget; they are stripped from the request
  • Only supports max_tokens; max_completion_tokens is mapped to max_tokens when the latter is not set
  • temperature range is [0.0, 1.0] (OpenAI allows up to 2.0) and top_p range is [0.01, 1.0]; out-of-range values are clamped

Thinking mode: GLM-4.5 and later models accept a thinking object (e.g. {"type": "enabled"}). GLM-4.7 and GLM-5 series enable thinking by default. It can be controlled through GenerateOptions.additionalBodyParam("thinking", Map.of("type", "disabled")), which is passed through to the request body as-is. GLM-5.2 additionally supports the standard reasoning_effort option (GenerateOptions.reasoningEffort, default max) and streaming tool-call arguments via GenerateOptions.additionalBodyParam("tool_stream", true).

Usage:


 OpenAIChatModel.builder()
     .formatter(new GLMFormatter())
     .modelName("glm-5.2")
     .baseUrl("https://open.bigmodel.cn/api/paas/v4")
     .apiKey(apiKey)
     .build();
 
See Also:
  • Constructor Details

    • GLMFormatter

      public GLMFormatter()
  • Method Details

    • doFormat

      protected List<OpenAIMessage> doFormat(List<io.agentscope.core.message.Msg> msgs)
      Overrides:
      doFormat in class OpenAIChatFormatter
    • convertMessage

      protected OpenAIMessage convertMessage(io.agentscope.core.message.Msg msg, boolean hasMedia)
      Description copied from class: OpenAIChatFormatter
      Convert one AgentScope message to OpenAI message format.

      Provider-specific subclasses may override this when the target API supports a slightly different message shape.

      Overrides:
      convertMessage in class OpenAIChatFormatter
      Parameters:
      msg - the message to convert
      hasMedia - whether the message contains media content
      Returns:
      converted OpenAI message
    • 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)
    • ensureUserMessage

      protected static List<OpenAIMessage> ensureUserMessage(List<OpenAIMessage> messages)
      Ensure at least one user message exists in the conversation.

      GLM API requires at least one user message. If no user message exists, a placeholder is added at the end (error 1214 otherwise).

      This method is static to allow sharing with GLMMultiAgentFormatter.

      Parameters:
      messages - the messages to check
      Returns:
      messages with a user message ensured
    • applyGLMToolChoice

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

      Per the latest Zhipu API reference, tool_choice defaults to and only supports "auto". Unsupported forced choices are degraded to "auto". For ToolChoice.None, tools are removed instead.

      This method is static to allow sharing with GLMMultiAgentFormatter.

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

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

      Applied adaptations:

      • frequency_penalty / presence_penalty / thinking_budget are not supported and removed
      • max_completion_tokens is not supported; it is mapped to max_tokens when max_tokens is not set
      • temperature is clamped to [0.0, 1.0] and top_p to [0.01, 1.0]

      This method is static to allow sharing with GLMMultiAgentFormatter.

      Parameters:
      request - the request to sanitize