Package com.tnsai.llm.format
Class ResponseFormat
java.lang.Object
com.tnsai.llm.format.ResponseFormat
Represents the desired output format for LLM responses.
Supports three format types:
ResponseFormat.Type.TEXT- Default text output (no special formatting)ResponseFormat.Type.JSON_OBJECT- Generic JSON object outputResponseFormat.Type.JSON_SCHEMA- Structured output with JSON Schema validation
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
| Provider | JSON_OBJECT | JSON_SCHEMA |
|---|---|---|
| OpenAI | Yes | Yes (GPT-4o, GPT-4-turbo) |
| Anthropic | No (use tool_use) | No (use tool_use) |
| Gemini | Yes | Yes |
| Ollama | Depends on model | No |
- Author:
- TnsAI Team
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionbooleanGets the JSON schema (only for JSON_SCHEMA type).Gets the schema name (only for JSON_SCHEMA type).getType()Gets the format type.inthashCode()booleanReturns true if this has a schema.booleanisJson()Returns true if this requires JSON output.booleanisStrict()Returns whether strict schema validation is enabled.booleanisText()Returns true if this is a text format.static ResponseFormatCreates a JSON object format.static ResponseFormatjsonSchema(String name, Class<?> clazz) Creates a JSON Schema format from a class.static ResponseFormatjsonSchema(String name, Map<String, Object> schema) Creates a JSON Schema format with the given schema.static ResponseFormatjsonSchema(String name, Map<String, Object> schema, boolean strict) Creates a JSON Schema format with optional strict mode.static ResponseFormattext()Creates a text format (default).Converts this format to Gemini API format.Converts this format to Ollama API format.Converts this format to OpenAI API format.toString()
-
Method Details
-
text
Creates a text format (default). -
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
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
Creates a JSON Schema format with optional strict mode.- Parameters:
name- Schema name (for API reference)schema- JSON Schema as a Mapstrict- Whether to enforce strict schema validation- Returns:
- ResponseFormat with JSON_SCHEMA type
-
jsonSchema
Creates a JSON Schema format from a class.Uses reflection to generate a basic JSON Schema from the class structure.
- Parameters:
name- Schema nameclazz- The class to generate schema from- Returns:
- ResponseFormat with JSON_SCHEMA type
-
getType
Gets the format type. -
getSchemaName
Gets the schema name (only for JSON_SCHEMA type). -
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
Converts this format to OpenAI API format.- Returns:
- Map representation for OpenAI API
-
toGeminiFormat
Converts this format to Gemini API format.- Returns:
- Map representation for Gemini API
-
toOllamaFormat
Converts this format to Ollama API format.- Returns:
- Map representation for Ollama API
-
toString
-
equals
-
hashCode
public int hashCode()
-