Structured Output

Structured output is a way to output data in a structured format. This is useful for when you want to output data in a format that is not easily parsable by a human. Since 1.3.0 PuppyChatter supports structured output. Structured output is defined in the current OpenAI specification, so only OpenAI-compatible chatbots will be able to use this feature. In PuppyChatter, such chatbots will now use the OpenAICompatiblePromptParameters class to define their prompt parameters and will inherit from the OpenAICompatiblePuppyChatter class. The current implementation of the OpenrouterPuppyChatter fits the above criteria.

Defining a structured output prompt

To define a structured output prompt, you must define a JSON schema for the output using the setJsonSchema method of the OpenAICompatiblePromptParameters class. The method takes a Class object as an argument. In the class, one has to use the JsonProperty annotation to define the properties of the JSON schema. For example, in the code below, the JSON schema defines a single property called input of type int.

public class FunctionParameters2 {
   @JsonProperty(required = true)
   private int input;
   
   public FunctionParameters2(int input) {
       this.input = input;
   }
   public int getInput() {
       return input;
   }
   public void setInput(int input) {
       this.input = input;
   }
}  

The current implementation relies on the victools/json-schema-generator library to generate the JSON schema. It follows the JSON Schema Draft 2020-12 specification.

Using structured output

To use structured output, you must define a JSON schema for the output using the setJsonSchema method of the OpenAICompatiblePromptParameters class. The code below shows how to use structured output with the OpenrouterPuppyChatter class.

OpenrouterPuppyChatter puppyChatter = new OpenrouterPuppyChatter(apikey);
String sessionId = puppyChatter.createSession();
OpenrouterPromptParameters parameters = new OpenrouterPromptParameters("user");
parameters.setJsonSchema(LanguageDescription.class);
Response response=puppyChatter.bark(sessionId, "model:google/gemini-2.0-flash-001 Please list the name, description, and supported versions of the Java programming language. Add up all the version numbers and append the result to the end of the description. Then respond according to the specified JSON schema.", parameters);
System.out.println(response.getMessage());
puppyChatter.closeSession(sessionId);


public class LanguageDescription {
    @JsonProperty(required = true)
    private String name;
    @JsonProperty(required = true)
    private String description;
    private List<String> versions;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public List<String> getVersions() {
        return versions;
    }
    public void setVersions(List<String> versions) {
        this.versions = versions;
    }
    
}