Package com.tnsai.llm.format
Class JsonModeClient
java.lang.Object
com.tnsai.llm.format.JsonModeClient
- All Implemented Interfaces:
com.tnsai.llm.LLMClient
LLM client wrapper that enforces JSON mode output.
Wraps any LLMClient to provide structured JSON output support. Uses provider-native JSON mode when available, falls back to prompt engineering for providers that don't support it natively.
Quick Start
// Wrap an existing client
LLMClient baseClient = new OpenAIClient("gpt-4o");
JsonModeClient client = JsonModeClient.wrap(baseClient);
// Get JSON response
ChatResponse response = client.chat("List 3 programming languages");
// Response: {"languages": ["Python", "Java", "JavaScript"]}
// Parse to specific type
LanguageList list = client.chatAs(LanguageList.class, "List 3 programming languages");
With JSON Schema
// Define schema
ResponseFormat format = 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")
));
// Create client with schema
JsonModeClient client = JsonModeClient.builder()
.client(baseClient)
.responseFormat(format)
.build();
ChatResponse response = client.chat("Generate a person");
// Response: {"name": "Alice", "age": 30}
- Author:
- TnsAI Team
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for JsonModeClient.static classException thrown when JSON parsing fails. -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonModeClient.Builderbuilder()Creates a new builder.com.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) com.tnsai.llm.ChatResponsechat(List<com.tnsai.models.chat.ContentPart> messageParts, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) <T> TSends a chat message and parses the JSON response to the specified type.<T> TSends a chat message with system prompt and parses the JSON response.<T> TchatAs(Class<T> clazz, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Sends a full chat request and parses the JSON response.com.tnsai.llm.LLMCapabilitiescom.tnsai.llm.LLMClientGets the wrapped delegate client.getModel()Gets the response format being used.float<T> TparseResponse(String json, Class<T> clazz) Parses a JSON response to the specified type.streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) booleanChecks if the underlying provider supports native JSON mode.static JsonModeClientwrap(com.tnsai.llm.LLMClient client) Creates a simple JSON mode wrapper with default settings.static JsonModeClientwrap(com.tnsai.llm.LLMClient client, ResponseFormat format) Creates a JSON mode wrapper with the specified format.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, getProviderName, getTopP, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec, streamChatWithSpec
-
Method Details
-
wrap
Creates a simple JSON mode wrapper with default settings.- Parameters:
client- The LLM client to wrap- Returns:
- JsonModeClient with JSON_OBJECT format
-
wrap
Creates a JSON mode wrapper with the specified format.- Parameters:
client- The LLM client to wrapformat- The response format to use- Returns:
- JsonModeClient with specified format
-
builder
Creates a new builder. -
getModel
- Specified by:
getModelin interfacecom.tnsai.llm.LLMClient
-
getTemperature
public float getTemperature()- Specified by:
getTemperaturein interfacecom.tnsai.llm.LLMClient
-
getMaxTokens
- Specified by:
getMaxTokensin interfacecom.tnsai.llm.LLMClient
-
chat
public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
chatin interfacecom.tnsai.llm.LLMClient
-
chat
public com.tnsai.llm.ChatResponse chat(List<com.tnsai.models.chat.ContentPart> messageParts, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
chatin interfacecom.tnsai.llm.LLMClient
-
streamChat
public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatin interfacecom.tnsai.llm.LLMClient
-
getCapabilities
public com.tnsai.llm.LLMCapabilities getCapabilities()- Specified by:
getCapabilitiesin interfacecom.tnsai.llm.LLMClient
-
chatAs
Sends a chat message and parses the JSON response to the specified type.- Type Parameters:
T- The type to parse to- Parameters:
clazz- The class to parse the response tomessage- The user message- Returns:
- Parsed object
- Throws:
JsonModeClient.JsonParseException- if parsing fails
-
chatAs
Sends a chat message with system prompt and parses the JSON response.- Type Parameters:
T- The type to parse to- Parameters:
clazz- The class to parse the response tomessage- The user messagesystemPrompt- Optional system prompt- Returns:
- Parsed object
- Throws:
JsonModeClient.JsonParseException- if parsing fails
-
chatAs
public <T> T chatAs(Class<T> clazz, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Sends a full chat request and parses the JSON response.- Type Parameters:
T- The type to parse to- Parameters:
clazz- The class to parse the response tomessage- The user messagesystemPrompt- Optional system prompthistory- Optional conversation historytools- Optional tools- Returns:
- Parsed object
- Throws:
JsonModeClient.JsonParseException- if parsing fails
-
parseResponse
Parses a JSON response to the specified type.- Type Parameters:
T- The type to parse to- Parameters:
json- The JSON string to parseclazz- The class to parse to- Returns:
- Parsed object
- Throws:
JsonModeClient.JsonParseException- if parsing fails
-
getDelegate
public com.tnsai.llm.LLMClient getDelegate()Gets the wrapped delegate client. -
getResponseFormat
Gets the response format being used. -
supportsNativeJsonMode
public boolean supportsNativeJsonMode()Checks if the underlying provider supports native JSON mode.
-