Class ResponseFormat

java.lang.Object
com.tnsai.llm.format.ResponseFormat

public class ResponseFormat extends Object
Represents the desired output format for LLM responses.

Supports three format types:

Quick Start


 // Text format (default)
 ResponseFormat text = ResponseFormat.text();

 // JSON object format
 ResponseFormat json = ResponseFormat.jsonObject();

 // JSON Schema format
 ResponseFormat schema = ResponseFormat.jsonSchema("Person", Map.of(
     "type", "object",
     "properties", Map.of(
         "name", Map.of("type", "string"),
         "age", Map.of("type", "integer")
     ),
     "required", List.of("name", "age")
 ));
 

Provider Support

ProviderJSON_OBJECTJSON_SCHEMA
OpenAIYesYes (GPT-4o, GPT-4-turbo)
AnthropicNo (use tool_use)No (use tool_use)
GeminiYesYes
OllamaDepends on modelNo
Author:
TnsAI Team
  • Method Details

    • text

      public static ResponseFormat text()
      Creates a text format (default).
    • jsonObject

      public static ResponseFormat jsonObject()
      Creates a JSON object format.

      The model will output valid JSON but structure is not enforced.

      Example

      
       ResponseFormat format = ResponseFormat.jsonObject();
       ChatResponse response = client.chat("List 3 colors", Optional.empty(),
           Optional.empty(), Optional.empty(), Optional.of(format));
       // Output: {"colors": ["red", "green", "blue"]}
       
    • jsonSchema

      public static ResponseFormat jsonSchema(String name, Map<String,Object> schema)
      Creates a JSON Schema format with the given schema.
      Parameters:
      name - Schema name (for API reference)
      schema - JSON Schema as a Map
      Returns:
      ResponseFormat with JSON_SCHEMA type
    • jsonSchema

      public static ResponseFormat jsonSchema(String name, Map<String,Object> schema, boolean strict)
      Creates a JSON Schema format with optional strict mode.
      Parameters:
      name - Schema name (for API reference)
      schema - JSON Schema as a Map
      strict - Whether to enforce strict schema validation
      Returns:
      ResponseFormat with JSON_SCHEMA type
    • jsonSchema

      public static ResponseFormat jsonSchema(String name, Class<?> clazz)
      Creates a JSON Schema format from a class.

      Uses reflection to generate a basic JSON Schema from the class structure.

      Parameters:
      name - Schema name
      clazz - The class to generate schema from
      Returns:
      ResponseFormat with JSON_SCHEMA type
    • getType

      public ResponseFormat.Type getType()
      Gets the format type.
    • getSchemaName

      public Optional<String> getSchemaName()
      Gets the schema name (only for JSON_SCHEMA type).
    • getSchema

      public Optional<Map<String,Object>> getSchema()
      Gets the JSON schema (only for JSON_SCHEMA type).
    • isStrict

      public boolean isStrict()
      Returns whether strict schema validation is enabled.
    • isText

      public boolean isText()
      Returns true if this is a text format.
    • isJson

      public boolean isJson()
      Returns true if this requires JSON output.
    • hasSchema

      public boolean hasSchema()
      Returns true if this has a schema.
    • toOpenAIFormat

      public Map<String,Object> toOpenAIFormat()
      Converts this format to OpenAI API format.
      Returns:
      Map representation for OpenAI API
    • toGeminiFormat

      public Map<String,Object> toGeminiFormat()
      Converts this format to Gemini API format.
      Returns:
      Map representation for Gemini API
    • toOllamaFormat

      public Map<String,Object> toOllamaFormat()
      Converts this format to Ollama API format.
      Returns:
      Map representation for Ollama API
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object