|
| 1 | +--- |
| 2 | +title: AI Endpoints - Using Structured Output with LangChain4j |
| 3 | +excerpt: Learn how to use Structured Output with Java, LangChain4j and OVHcloud AI Endpoints |
| 4 | +updated: 2025-08-06 |
| 5 | +--- |
| 6 | + |
| 7 | +> [!primary] |
| 8 | +> |
| 9 | +> AI Endpoints is covered by the **[OVHcloud AI Endpoints Conditions](https://storage.gra.cloud.ovh.net/v1/AUTH_325716a587c64897acbef9a4a4726e38/contracts/48743bf-AI_Endpoints-ALL-1.1.pdf)** and the **[OVHcloud Public Cloud Special Conditions](https://storage.gra.cloud.ovh.net/v1/AUTH_325716a587c64897acbef9a4a4726e38/contracts/d2a208c-Conditions_particulieres_OVH_Stack-WE-9.0.pdf)**. |
| 10 | +> |
| 11 | +
|
| 12 | +## Objective |
| 13 | + |
| 14 | +In this tutorial, we will explore how to use **Structured Output** with OVHcloud AI Endpoints. |
| 15 | + |
| 16 | +To do this, we will use **[LangChain4j](https://github.com/langchain4j/langchain4j)**, Java-based framework inspired by [LangChain](https://github.com/langchain-ai/langchain), designed to simplify the integration of LLMs (Large Language Models) into applications. Note that **LangChain4j** is not officially maintained by the LangChain team, despite the similar name. |
| 17 | + |
| 18 | +Combined with OVHcloud **[AI Endpoints](https://endpoints.ai.cloud.ovh.net/)** which offers both LLM and embedding models, it becomes easy to create advanced, production-ready assistants. |
| 19 | + |
| 20 | +{.thumbnail} |
| 21 | + |
| 22 | +## Definition |
| 23 | + |
| 24 | +- **Structured Output**: Structured output allows you to format output data in a way that makes it easier for machines to interpret and process. |
| 25 | +- **[LangChain4j](https://github.com/langchain4j/langchain4j)**: a Java-based framework inspired by [LangChain](https://github.com/langchain-ai/langchain), designed to simplify the integration of LLMs (Large Language Models) into applications. Note that LangChain4j is not officially maintained by the LangChain team, despite the similar name. |
| 26 | +- **[AI Endpoints](https://endpoints.ai.cloud.ovh.net/)**: A serverless platform by OVHcloud providing easy access to a variety of world-renowned AI models including Mistral, LLaMA, and more. This platform is designed to be simple, secure, and intuitive with data privacy as a top priority. |
| 27 | + |
| 28 | +## Requirements |
| 29 | + |
| 30 | +- A [Public Cloud project](/links/public-cloud/public-cloud) in your OVHcloud account. |
| 31 | +- An access token for **OVHcloud AI Endpoints**. To create an API token, follow the instructions in the [AI Endpoints - Getting Started](/pages/public_cloud/ai_machine_learning/endpoints_guide_01_getting_started) guide. |
| 32 | +- This code example uses JBang, a Java-based tool for creating and running Java programs as scripts. For more information on JBang, please refer to the [JBang documentation](https://www.jbang.dev/documentation/guide/latest/installation.html). |
| 33 | + |
| 34 | +## Instructions |
| 35 | + |
| 36 | +Here is an excerpt of code that shows how to define a structured output format for the responses of the language model: |
| 37 | + |
| 38 | +```java |
| 39 | +// Json schema definition |
| 40 | +ResponseFormat responseFormat = ResponseFormat.builder() |
| 41 | + .type(ResponseFormatType.JSON) |
| 42 | + .jsonSchema(JsonSchema.builder() |
| 43 | + .name("Person") |
| 44 | + .rootElement(JsonObjectSchema.builder() |
| 45 | + .addStringProperty("name") |
| 46 | + .addIntegerProperty("age") |
| 47 | + .addNumberProperty("height") |
| 48 | + .addBooleanProperty("married") |
| 49 | + .required("name", "age", "height", "married") |
| 50 | + .build()) |
| 51 | + .build()) |
| 52 | +.build(); |
| 53 | +``` |
| 54 | + |
| 55 | +In this example, we define a JSON output format with a schema that specifies the name, age, height, and married properties as required. |
| 56 | + |
| 57 | +This example uses the Mistral AI model hosted on OVHcloud AI Endpoints. |
| 58 | + |
| 59 | +To configure the model, you need to set up the API key, base URL, and model name as environment variables. Feel free to use another model, see AI Endpoints catalog. |
| 60 | + |
| 61 | +You can find your access token, model URL, and model name in the OVHcloud AI Endpoints model dashboard. |
| 62 | + |
| 63 | +```java |
| 64 | +// Model definition |
| 65 | +ChatModel chatModel = MistralAiChatModel.builder() |
| 66 | + .apiKey(System.getenv("OVH_AI_ENDPOINTS_ACCESS_TOKEN")) |
| 67 | + .baseUrl(System.getenv("OVH_AI_ENDPOINTS_MODEL_URL")) |
| 68 | + .modelName(System.getenv("OVH_AI_ENDPOINTS_MODEL_NAME")) |
| 69 | + .logRequests(false) |
| 70 | + .logResponses(false) |
| 71 | +.build(); |
| 72 | +``` |
| 73 | + |
| 74 | +### Calling the language model |
| 75 | + |
| 76 | +Thanks to the JSON mode of the LLM, the response from the language model is received as a JSON string: |
| 77 | + |
| 78 | +```java |
| 79 | +// Model call with JSON mode |
| 80 | +UserMessage userMessage = UserMessage.from(""" |
| 81 | + John is 42 years old. |
| 82 | + He stands 1.75 meters tall. |
| 83 | + Currently unmarried. |
| 84 | + """); |
| 85 | + |
| 86 | +ChatRequest chatRequest = ChatRequest.builder() |
| 87 | + .responseFormat(responseFormat) |
| 88 | + .messages(userMessage) |
| 89 | + .build(); |
| 90 | + |
| 91 | +ChatResponse chatResponse = chatModel.chat(chatRequest); |
| 92 | + |
| 93 | +String output = chatResponse.aiMessage().text(); |
| 94 | +System.out.println("Response: \n" + output); |
| 95 | + |
| 96 | + |
| 97 | +// Person is a simple record: record Person(String name, int age, double height, boolean married) {} |
| 98 | +Person person = new ObjectMapper().readValue(output, Person.class); |
| 99 | +System.out.println(person); |
| 100 | +``` |
| 101 | + |
| 102 | +### The full source code |
| 103 | + |
| 104 | +```java |
| 105 | +///usr/bin/env jbang "$0" "$@" ; exit $? |
| 106 | +//JAVA 21+ |
| 107 | +//PREVIEW |
| 108 | +//DEPS dev.langchain4j:langchain4j:1.0.1 dev.langchain4j:langchain4j-mistral-ai:1.0.1-beta6 |
| 109 | + |
| 110 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 111 | +import dev.langchain4j.data.message.UserMessage; |
| 112 | +import dev.langchain4j.model.chat.request.ChatRequest; |
| 113 | +import dev.langchain4j.model.chat.request.ResponseFormat; |
| 114 | +import dev.langchain4j.model.chat.request.ResponseFormatType; |
| 115 | +import dev.langchain4j.model.chat.request.json.JsonObjectSchema; |
| 116 | +import dev.langchain4j.model.chat.request.json.JsonSchema; |
| 117 | +import dev.langchain4j.model.chat.response.ChatResponse; |
| 118 | +import dev.langchain4j.model.mistralai.MistralAiChatModel; |
| 119 | +import dev.langchain4j.model.chat.ChatModel; |
| 120 | + |
| 121 | +record Person(String name, int age, double height, boolean married) { |
| 122 | +} |
| 123 | + |
| 124 | +void main() throws Exception { |
| 125 | + ResponseFormat responseFormat = ResponseFormat.builder() |
| 126 | + .type(ResponseFormatType.JSON) |
| 127 | + .jsonSchema(JsonSchema.builder() |
| 128 | + .name("Person") |
| 129 | + .rootElement(JsonObjectSchema.builder() |
| 130 | + .addStringProperty("name") |
| 131 | + .addIntegerProperty("age") |
| 132 | + .addNumberProperty("height") |
| 133 | + .addBooleanProperty("married") |
| 134 | + .required("name", "age", "height", "married") |
| 135 | + .build()) |
| 136 | + .build()) |
| 137 | + .build(); |
| 138 | + |
| 139 | + UserMessage userMessage = UserMessage.from(""" |
| 140 | + John is 42 years old. |
| 141 | + He stands 1.75 meters tall. |
| 142 | + Currently unmarried. |
| 143 | + """); |
| 144 | + |
| 145 | + ChatRequest chatRequest = ChatRequest.builder() |
| 146 | + .responseFormat(responseFormat) |
| 147 | + .messages(userMessage) |
| 148 | + .build(); |
| 149 | + |
| 150 | + ChatModel chatModel = MistralAiChatModel.builder() |
| 151 | + .apiKey(System.getenv("OVH_AI_ENDPOINTS_ACCESS_TOKEN")) |
| 152 | + .baseUrl(System.getenv("OVH_AI_ENDPOINTS_MODEL_URL")) |
| 153 | + .modelName(System.getenv("OVH_AI_ENDPOINTS_MODEL_NAME")) |
| 154 | + .logRequests(false) |
| 155 | + .logResponses(false) |
| 156 | + .build(); |
| 157 | + |
| 158 | + ChatResponse chatResponse = chatModel.chat(chatRequest); |
| 159 | + |
| 160 | + System.out.println("Prompt: \n" + userMessage.singleText()); |
| 161 | + String output = chatResponse.aiMessage().text(); |
| 162 | + System.out.println("Response: \n" + output); |
| 163 | + |
| 164 | + Person person = new ObjectMapper().readValue(output, Person.class); |
| 165 | + System.out.println(person); |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +### Running the application |
| 170 | + |
| 171 | +```java |
| 172 | +jbang HelloWorld.java |
| 173 | +[jbang] Building jar for HelloWorld.java... |
| 174 | + |
| 175 | +Prompt: |
| 176 | +John is 42 years old. |
| 177 | +He stands 1.75 meters tall. |
| 178 | +Currently unmarried. |
| 179 | + |
| 180 | +Response: |
| 181 | +{"age": 42, "height": 1.75, "married": false, "name": "John"} |
| 182 | +Person[name=John, age=42, height=1.75, married=false] |
| 183 | +``` |
| 184 | + |
| 185 | +## Conclusion |
| 186 | + |
| 187 | +In this article, we have seen how to use Structured Output with OVHcloud AI Endpoints and LangChain4J. |
| 188 | + |
| 189 | +## Go further |
| 190 | + |
| 191 | +You can find the full code example in the [GitHub repository](https://github.com/ovh/public-cloud-examples/tree/main/ai/ai-endpoints/structured-output-langchain4j). |
| 192 | + |
| 193 | +Browse the full [AI Endpoints documentation](/products/public-cloud-ai-and-machine-learning-ai-endpoints) to further understand the main concepts and get started. |
| 194 | + |
| 195 | +To discover how to build complete and powerful applications using AI Endpoints, explore our dedicated [AI Endpoints guides](/products/public-cloud-ai-and-machine-learning-ai-endpoints). |
| 196 | + |
| 197 | +If you need training or technical assistance to implement our solutions, contact your sales representative or click on [this link](/links/professional-services) to get a quote and ask our Professional Services experts for a custom analysis of your project. |
| 198 | + |
| 199 | +## Feedback |
| 200 | + |
| 201 | +Please feel free to send us your questions, feedback, and suggestions regarding AI Endpoints and its features: |
| 202 | + |
| 203 | +- In the #ai-endpoints channel of the OVHcloud [Discord server](https://discord.gg/ovhcloud), where you can engage with the community and OVHcloud team members. |
| 204 | + |
| 205 | +If you need training or technical assistance to implement our solutions, contact your sales representative or click on [this link](/links/professional-services) to get a quote and ask our Professional Services experts for a custom analysis of your project. |
0 commit comments