11# Migration Guide: Updating Message Handling
22
33This guide outlines the necessary changes to migrate your code from the previous message handling structure (using the
4- ` nl.dannyj.mistral.models.completion. Message` class) to the new structure introduced to support multi-modal content and
5- tool calls, based on the abstract ` nl.dannyj.mistral.models.completion. ChatMessage` class.
4+ ` Message ` class) to the new structure introduced to support multi-modal content and
5+ tool calls, based on the abstract ` ChatMessage ` class.
66
77## Core Changes
88
9- 1 . ** ` Message ` Class Removed:** The old ` nl.dannyj.mistral.models.completion. Message` class has been removed.
10- 2 . ** ` ChatMessage ` Hierarchy:** A new abstract base class ` nl.dannyj.mistral.models.completion. ChatMessage` has been
9+ 1 . ** Message Class Removed:** The old ` Message ` class has been removed.
10+ 2 . ** ChatMessage Hierarchy:** A new abstract base class ` ChatMessage ` has been
1111 introduced. Specific message types now extend this class:
12- * ` nl.dannyj.mistral.models.completion.message. SystemMessage`
13- * ` nl.dannyj.mistral.models.completion.message. UserMessage`
14- * ` nl.dannyj.mistral.models.completion.message. AssistantMessage`
15- * ` nl.dannyj.mistral.models.completion.message. ToolMessage`
12+ - ` SystemMessage `
13+ - ` UserMessage `
14+ - ` AssistantMessage `
15+ - ` ToolMessage `
16163 . ** Multi-Modal Content:** Messages (primarily ` UserMessage ` , ` AssistantMessage ` , ` ToolMessage ` ) now use a
1717 ` List<ContentChunk> ` for their ` content ` field to support text, images, etc. The
18- ` nl.dannyj.mistral.models.completion.content. ContentChunk` interface has implementations like ` TextChunk ` ,
18+ ` ContentChunk ` interface has implementations like ` TextChunk ` ,
1919 ` ImageURLChunk ` , etc.
20- * Convenience constructors/methods are provided for creating messages with simple text content.
21- 4 . ** Tool Calls:** ` AssistantMessage ` now includes a ` List<ToolCall> toolCalls ` field, and ` ToolMessage ` includes
22- ` toolCallId ` and ` name ` fields to support function calling/tools.
20+ - ` getTextContent() ` helper methods were introduced to prevent having to loop over ` ContentChunk ` s to find ` TextChunk ` s
21+ 4 . ** Tool Calls:** ` AssistantMessage ` now includes a ` List<ToolCall> toolCalls ` field, and a ` ToolMessage ` message type was introduced to support function calling/tools.
23225 . ** Streaming Deltas:** The ` DeltaChoice ` class (used in streaming responses via ` MessageChunk ` ) now contains a
24- ` nl.dannyj.mistral.models.completion.message. DeltaMessage` object (accessible via ` getDelta() ` ). This ` DeltaMessage `
23+ ` DeltaMessage ` object (accessible via ` getDelta() ` ). This ` DeltaMessage `
2524 object, in turn, holds the partial delta fields: ` role ` , ` content ` (as ` List<ContentChunk> ` ), and ` toolCalls ` .
2625
2726## Migration Steps
@@ -108,9 +107,7 @@ an `AssistantMessage`.
108107ChatCompletionResponse response = client. createChatCompletion(request);
109108Message responseMsg = response. getChoices(). get(0 ). getMessage();
110109String content = responseMsg. getContent();
111- System . out.
112-
113- println(content);
110+ System . out. println(content);
114111```
115112
116113** After:**
@@ -122,36 +119,24 @@ import nl.dannyj.mistral.models.completion.content.TextChunk;
122119
123120// ...
124121ChatCompletionResponse response = client. createChatCompletion(request);
125- AssistantMessage responseMsg = response. getChoices(). get(0 ). getMessage();
126-
127- // Extract text content (handle potential multi-modal content)
128- String textContent = " " ;
129- if (responseMsg.
130-
131- getContent() != null ){
132- for (
133- ContentChunk chunk : responseMsg.
134-
135- getContent()){
136- if (chunk instanceof
137- TextChunk textChunk){
138- textContent += textChunk.
139-
140- getText(); // Append text from TextChunks
122+ AssistantMessage responseMsg = response. getChoices(). get(0 ). getMessage();
123+
124+ // Extract text content (handle potential multi-modal content)
125+ String textContent = " " ;
126+ if (responseMsg. getContent() != null ) {
127+ for (ContentChunk chunk : responseMsg. getContent()) {
128+ if (chunk instanceof TextChunk textChunk) {
129+ textContent += textChunk. getText(); // Append text from TextChunks
141130 }
142- // Handle other chunk types (ImageURLChunk, etc.) if necessary
143- }
144- }
145- System . out.
146-
147- println(textContent);
131+ // Handle other chunk types (ImageURLChunk, etc.) if necessary
132+ }
133+ }
134+ System . out. println(textContent);
148135
149136// Check for tool calls if applicable
150- if (responseMsg.
151-
152- getToolCalls() != null ){
153- // Process tool calls
154- }
137+ if (responseMsg. getToolCalls() != null ) {
138+ // Process tool calls
139+ }
155140```
156141
157142### 4. Update Handling of Streaming Responses (` ChatCompletionChunkCallback ` )
@@ -164,7 +149,7 @@ a `DeltaChoice`) has changed. Access `role`, `content`, and `toolCalls` directly
164149``` java
165150client. createChatCompletionStream(request, new ChatCompletionChunkCallback () {
166151 @Override
167- public void onChunkReceived (MessageChunk chunk ){
152+ public void onChunkReceived (MessageChunk chunk ) {
168153 // Old structure assumed delta was nested in a Message object
169154 String contentDelta = chunk. getChoices(). get(0 ). getMessage(). getContent();
170155 if (contentDelta != null ) {
@@ -186,7 +171,7 @@ import nl.dannyj.mistral.models.completion.content.TextChunk;
186171// ...
187172client. createChatCompletionStream(request, new ChatCompletionChunkCallback () {
188173 @Override
189- public void onChunkReceived (MessageChunk chunk ){
174+ public void onChunkReceived (MessageChunk chunk ) {
190175 if (chunk. getChoices() == null || chunk. getChoices(). isEmpty()) return ;
191176
192177 DeltaChoice deltaChoice = chunk. getChoices(). get(0 );
@@ -202,105 +187,39 @@ client.createChatCompletionStream(request, new ChatCompletionChunkCallback() {
202187 }
203188 // ... onComplete, onError
204189});
190+ ```
205191
206- ### 5.
207- Update Handling
208- of `Choice `
209- Object in `ChatCompletionResponse `
210-
211- The `nl.dannyj.mistral.models.completion. Choice `class,
212- which is
213- part of
214- the `ChatCompletionResponse .
215-
216- getChoices()`,
217- has the
218- following breaking
219- changes:
220-
221- *** `finishReason`
222- Type Change : **
223- * The `finishReason`
224- field is
225- now of
226- type `nl.dannyj.mistral.models.completion. FinishReason ` (an enum)
227- instead of
228- a raw `String `.
229- *** Before : ** `
230- String finishReason = choice. getFinishReason();`
231- *** After : ** `
232- FinishReason finishReasonEnum = choice. getFinishReason();`
233- *
234- To get
235- the underlying
236-
237- string value(e. g . , " stop" ," length" ),you can call `finishReasonEnum.
238-
239- getReason()`.
240- ***
241- Action Required : **
242- Update your
243- code to
244- work with
245- the `FinishReason `enum.
246- This typically
247- involves changing
248- variable types
249- and how
250- you compare
251-
252- the reason(e. g . , using `== ` with enum constants or comparing the string from `getReason()`).
253-
254- **Example:Handling `finishReason` (
255- within the
256- context of
257- iterating choices
258- from `ChatCompletionResponse`)**
259-
260- **
261-
262- Before(inside loop processing `choice`):**
263- ```java
264- String reason = choice.getFinishReason();
265- if("stop".
192+ ### 5. Update Handling of ` Choice ` Object in ` ChatCompletionResponse `
266193
267- equals(reason)){
194+ The ` Choice ` class, which is part of the ` ChatCompletionResponse.getChoices() ` , has the following breaking changes:
195+
196+ #### ` finishReason ` Type Change
197+ - The ` finishReason ` field is now of type ` FinishReason ` (an enum) instead of a raw ` String ` .
198+ - ** Before:** ` String finishReason = choice.getFinishReason(); `
199+ - ** After:** ` FinishReason finishReason = choice.getFinishReason(); `
200+ - To get the underlying string value (e.g., "stop", "length"), you can call ` finishReason.getReason() ` .
201+
202+ #### Example: Handling ` finishReason ` (within the context of iterating choices from ` ChatCompletionResponse ` )
203+
204+ ** Before(inside loop processing ` choice ` ):**
205+ ``` java
206+ String reason = choice. getFinishReason();
207+ if (" stop" . equals(reason)) {
268208 // ...
269209 }
270- ```
271-
272- **
210+ ```
273211
274- After(inside loop processing `choice`): **
275- ```java
212+ ** After(inside loop processing ` choice ` ):**
213+ ``` java
276214 import nl.dannyj.mistral.models.completion.FinishReason ; // Ensure import
277215// ...
278216FinishReason reason = choice. getFinishReason();
279- if (reason == FinishReason . STOP ){
280- // ...
281- }
282- // Or, to compare with the string value:
283- // if ("stop".equals(reason.getReason())) { ... }
284- ```
285-
286- *** `logProbs`
287- Field Removal : **
288- * The `logProbs`
289-
290- field(previously a `String `) has been removed from the `Choice `
291-
292- class as it is no longer returned by the API.
293- ***
294- Action Required:**
295- If your
296- code was
297- accessing `choice.
298-
299- getLogProbs()`,this
300- will now
301- result in
302- a compilation
303- error.You must
304- remove such
305- accesses .
217+ if (reason == FinishReason . STOP ) {
218+ // ...
219+ }
220+ // Or, to compare with the string value:
221+ // if ("stop".equals(reason.getReason())) { ... }
306222```
223+
224+ #### ` logProbs ` Field Removal
225+ - The ` logProbs ` field (previously a ` String ` ) has been removed from the ` Choice ` class as it is no longer returned by the API.
0 commit comments