Repository navigation
CAMEL-26514: camel-langchain4j-embeddings - set the CamelLangChain4jEmbeddingsResponseModel header - #29654
Conversation
…mbeddingsResponseModel header The producer now calls embed(EmbeddingRequest), whose EmbeddingResponse gives the model name. The test models implement embedAll, as the default methods of LangChain4j need. Co-authored-by: Claude <noreply@anthropic.com>
gnodet-bot
left a comment
There was a problem hiding this comment.
The PR migrates from Response<Embedding> to EmbeddingResponse API, but removeResultHeaders was not updated to match.
Bug: Stale RESPONSE_MODEL header
In populateHeaders, the PR replaces:
message.setHeader(LangChain4jEmbeddingsHeaders.FINISH_REASON, result.finishReason().toString());with:
message.setHeader(LangChain4jEmbeddingsHeaders.RESPONSE_MODEL, result.modelName());However, removeResultHeaders (called at the top of both populateHeaders and populateBatchHeaders to clear stale values) was NOT updated — it still removes FINISH_REASON (which is never set anymore) and does NOT remove RESPONSE_MODEL (which IS now set).
This means when a Message object is reused across multiple calls, RESPONSE_MODEL can carry a stale value from the previous call.
Fix: In removeResultHeaders, replace:
message.removeHeader(LangChain4jEmbeddingsHeaders.FINISH_REASON);with:
message.removeHeader(LangChain4jEmbeddingsHeaders.RESPONSE_MODEL);This review was generated by an AI agent, Hermès on behalf of @gnodet.
oscerd
left a comment
There was a problem hiding this comment.
Correct fix for the real bug: embed(TextSegment) / embedAll(List) return a Response that does not carry the model name, so the documented CamelLangChain4jEmbeddingsResponseModel header was never set, and moving to embed(EmbeddingRequest) is the way to get an EmbeddingResponse that has modelName(). Setting the header only when modelName() is non-null is the right guard.
What makes this a good PR rather than just a fix is that the upgrade guide is honest about the two consequences, both of which matter:
FINISH_REASONis no longer set becauseEmbeddingResponsehas no finish reason. For embeddings that header was effectively always null anyway (the genericResponse.finishReason()is not populated for an embedding call), so in practice nobody was receiving it, and the guide says so. The one loose end is thatLangChain4jEmbeddingsHeaders.FINISH_REASONis still declared with@Metadata, so it now advertises a header the producer never sets; worth either dropping that declaration for consistency or leaving a line, though keeping the constant does avoid breaking anyone who references it.- The sharper one, and good that it is called out, is that a custom
EmbeddingModelthat only overridesembed(TextSegment)is no longer invoked and the LangChain4j default methods then recurse into aStackOverflowError. That is a genuine breaking change for downstream custom models, and spelling out that they must implementembedAll(List)ordoEmbed(EmbeddingRequest)is exactly the guidance those users need.
Tests updated for the metadata and observability paths. CI does not appear to have run on the fork, so this is a source review; the behaviour change is real but documented. LGTM.
Claude Code on behalf of oscerd
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
davsclaus
left a comment
There was a problem hiding this comment.
Thanks @urunni88, this follows the option recommended on CAMEL-26514, and the upgrade guide entry explains both side effects (no finish reason, a model that only overrides embed(TextSegment) now fails). I checked the StackOverflowError claim against LangChain4j 1.23.0: embed(EmbeddingRequest) → doEmbed → embedAll → embed(EmbeddingRequest), so a model has to override embedAll or doEmbed. Note that the batch path already went through embedAll before this PR, so only the single-body path changes for such a model.
Findings:
-
(medium) The GenAI span still has no response model.
GenAiUsage.of(input, output, finishReason, responseModel): the 4th argument is the response model, and both calls passnull(LangChain4jEmbeddingsProducer.java:95and:129). The JIRA mentions this too ("and the GenAI usage has no response model"). Passresult.modelName()there, the wayLangChain4jChatProducerdoes, sogen_ai.response.modelis set (GenAiObservabilityImplonly sets the tag when it is not blank).shouldEmitGenAiSpanFromEmbeddingsProducercan then also asserttags.get(GenAiAttributes.RESPONSE_MODEL). -
(medium)
removeResultHeaders(LangChain4jEmbeddingsProducer.java:186) clears the headers of an earlier call, but it does not removeRESPONSE_MODEL, which this PR now sets. When a message goes through two embeddings endpoints, and the second model returns no model name, the first model's name stays on the message. Addmessage.removeHeader(LangChain4jEmbeddingsHeaders.RESPONSE_MODEL). gnodet-bot proposed replacing theFINISH_REASONremoval; I would keep that line too, because the header name is still public and a stale value should still be cleared. -
(low)
FINISH_REASONis still declared with@Metadata(LangChain4jEmbeddingsHeaders.java:23-24), so the catalog and the component doc still list a header that is never set. Either mark it deprecated in this PR (constant@Deprecatedand a deprecation note in@Metadata, then commit the regeneratedlangchain4j-embeddings.json, catalog JSON and endpoint DSL), or open a follow-up JIRA. I am fine either way; say which one in the PR. -
(low)
result.embeddings().get(0)(LangChain4jEmbeddingsProducer.java:215) throws a bareIndexOutOfBoundsExceptionif a model returns no embedding. LangChain4j's ownembed(TextSegment)checks the size withensureEq. A check with a clear message would be nicer, but it is optional. -
(nit) The upgrade-guide section is added after the last "Jackson 3 - ..." subsection, so it reads as part of that group. Moving it before
=== Jackson 3, next to the other component sections, would be better.
Tests: shouldSetResponseModelHeader covers the single and the List body, uses AssertJ and assertIsSatisfied with a timeout. Good. CI is running on the head.
Claude Code on behalf of Claus Ibsen. This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.
|
🧪 CI tested the following changed modules:
🔬 Scalpel shadow comparison — Scalpel: 10 of 722 tested, 26 compile-only — current: 10 all testedMaveniverse Scalpel detected 10 affected modules (current approach: 10). Skip-tests mode would test 10 modules (2 direct + 9 downstream), skip tests for 26 (generated code, meta-modules) Modules Scalpel would test (10)
Modules with tests skipped (26)
All tested modules (37 modules, 5m 16s total)Total reactor time: 5m 16s
Top 20 slowest modules:
|
Pass the response model to the GenAI span, clear a stale CamelLangChain4jEmbeddingsResponseModel header, fail clearly when a model returns no embedding, and move the upgrade guide section before the Jackson 3 sections. Co-authored-by: Claude <noreply@anthropic.com>
|
Thanks for the review, @davsclaus! I pushed a commit for the findings:
All 21 tests of the module pass locally. |
gnodet-bot
left a comment
There was a problem hiding this comment.
Following up on my earlier review — all 4 findings have been addressed in the new commit.
✅ Stale RESPONSE_MODEL header — removeResultHeaders now clears RESPONSE_MODEL before repopulating headers.
✅ GenAI span missing response model — both processSingle and processBatch now pass result.modelName() as the 4th argument to GenAiUsage.of().
✅ Empty embeddings guard — populateHeaders now checks result.embeddings() for null/empty and throws IllegalStateException with a clear message instead of a bare IndexOutOfBoundsException.
✅ Upgrade guide position — the langchain4j-embeddings section now appears before the Jackson 3 sections.
The FINISH_REASON @Metadata constant remaining declared (davsclaus finding #3) is pre-existing and the deferral to a follow-up JIRA is acceptable.
Test stubs correctly override embedAll(List<TextSegment>), which is where LangChain4j's default embed(EmbeddingRequest) dispatches. LGTM.
This review was generated by an AI agent, Hermès on behalf of @gnodet.
davsclaus
left a comment
There was a problem hiding this comment.
Thanks @urunni88 for the quick turnaround. I went through 02af57f against my earlier review:
- GenAI span response model: fixed. Both
GenAiUsage.of(...)calls now passresult.modelName()(LangChain4jEmbeddingsProducer.java:95and:129).shouldEmitGenAiSpanFromEmbeddingsProducerassertsgen_ai.response.model(LangChain4jEmbeddingsObservabilityTest.java:92). - Stale
RESPONSE_MODEL: fixed.removeResultHeadersnow also clearsRESPONSE_MODEL(:189), and theFINISH_REASONremoval stays. FINISH_REASONstill declared with@Metadata: you chose a follow-up JIRA, which is fine with me. I could not find it in JIRA yet; when you open it, please link it here so the loose end stays visible.- Empty embeddings: fixed. An empty or null list now fails with
IllegalStateException("The embedding model returned no embedding")(:216-218). It is thrown inside thetry, so the error is still recorded on the span. - Upgrade-guide position: fixed. The section now sits before
=== Jackson 3.
I found no new problems in the delta, and the PR still merges cleanly with main. CI on the head is still running; approving on the condition that it goes green.
Claude Code on behalf of davsclaus. This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.
|
Thanks @davsclaus! I opened the follow-up for FINISH_REASON: https://issues.apache.org/jira/browse/CAMEL-26873. I'll send a PR for it once this one is merged. |
|
The follow-up PR for CAMEL-26873 is here: #29794 |
Description
CAMEL-26514: the producer called
EmbeddingModel.embed(TextSegment)andembedAll(List), whoseResponsedoes not carry the model name, so the documentedCamelLangChain4jEmbeddingsResponseModelheader was never set.Following the recommendation on the JIRA, the producer now calls
embed(EmbeddingRequest)for both a single body and a List body, and sets the header fromEmbeddingResponse.modelName()when it is not null.As noted on the JIRA, this has two side effects, both described in the 4.24 upgrade guide:
EmbeddingResponsehas no finish reason, so theCamelLangChain4jEmbeddingsFinishReasonheader is no longer set. Happy to deprecate that header here or in a follow-up if you prefer.embed(TextSegment)is bypassed: LangChain4j's default methods then call each other until aStackOverflowError. The stub models ofLangChain4jEmbeddingsObservabilityTestandLangChain4jEmbeddingsErrorMetadataTesthit exactly this, so they now implementembedAll(List<TextSegment>), as the LangChain4j models do.Test:
LangChain4jEmbeddingsObservabilityTest.shouldSetResponseModelHeadersends a single body and a List body and checks the header on each message. It fails without the fix (the header isnull) and passes with it. All 21 tests of the module pass.Target
mainbranch)Tracking
Apache Camel coding standards and style
I checked that each commit in the pull request has a meaningful subject line and body.
I have run
mvn clean install -DskipTestslocally from root folder and I have committed all auto-generated changes.AI-assisted contributions
Co-authored-bytrailers) and the PR description identifies the AI tool used.This PR was prepared with the help of Claude (Anthropic), credited with a
Co-authored-bytrailer in the commit.