gnodet-bot commented on code in PR #30451:
URL: https://github.com/apache/camel/pull/30451#discussion_r4634565627
##########
components/camel-spring-parent/camel-spring-ai/camel-spring-ai-chat/src/main/java/org/apache/camel/component/springai/chat/SpringAiChatProducer.java:
##########
@@ -904,12 +913,22 @@ private void populateResponseMetadata(ChatResponse
response, Exchange exchange)
responseMetadata.getId());
}
- // Finish reason is on the Generation metadata, not the Response
metadata
- if (response.getResult() != null &&
response.getResult().getMetadata() != null) {
- String finishReason =
response.getResult().getMetadata().getFinishReason();
+ // Finish reason is on the Generation metadata, not the Response
metadata. Its category is the same for every
+ // provider, unlike the finish reason. A finish reason of an
earlier call does not stay next to the category
+ // of this one
+ ChatGenerationMetadata generationMetadata
+ = response.getResult() != null ?
response.getResult().getMetadata() : null;
+ String finishReason = null;
+ String finishReasonCategory = null;
+ if (generationMetadata != null) {
+ finishReason = generationMetadata.getFinishReason();
+ finishReasonCategory =
generationMetadata.getFinishReasonCategory().name();
if (finishReason != null) {
exchange.getMessage().setHeader(SpringAiChatConstants.FINISH_REASON,
finishReason);
+ } else {
+
exchange.getMessage().removeHeader(SpringAiChatConstants.FINISH_REASON);
}
+
exchange.getMessage().setHeader(SpringAiChatConstants.FINISH_REASON_CATEGORY,
finishReasonCategory);
}
Review Comment:
**Stale `FINISH_REASON_CATEGORY` when `generationMetadata` is null** — when
`response.getResult()` returns null, the entire `if`-block is skipped.
`FINISH_REASON` now has an `else` branch that removes the stale header from an
earlier call (line 929–930), but `FINISH_REASON_CATEGORY` has no equivalent
cleanup. The comment above says "A finish reason of an earlier call does not
stay next to the category of this one" — that's only partially true for this
header.
The `REASONING` header uses the right pattern (unconditional set/remove
outside any null guard). Consider applying the same here:
```java
if (generationMetadata != null) {
// ...
exchange.getMessage().setHeader(SpringAiChatConstants.FINISH_REASON_CATEGORY,
finishReasonCategory);
} else {
exchange.getMessage().removeHeader(SpringAiChatConstants.FINISH_REASON_CATEGORY);
}
```
Non-blocking — `getFinishReasonCategory()` never returns null per Spring AI
2.1.0 sources, so the null-result path is rare in practice.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]