Skip to content

Camel 22510 avoid leaking internal data types#21704

Merged
gnodet merged 11 commits intoapache:mainfrom
chandru9:CAMEL-22510-avoid-leaking-internal-data-types
Mar 11, 2026
Merged

Camel 22510 avoid leaking internal data types#21704
gnodet merged 11 commits intoapache:mainfrom
chandru9:CAMEL-22510-avoid-leaking-internal-data-types

Conversation

@chandru9
Copy link
Copy Markdown
Contributor

@chandru9 chandru9 commented Mar 4, 2026

Description

Target

  • I checked that the commit is targeting the correct branch (Camel 4 uses the main branch)

Tracking

  • If this is a large change, bug fix, or code improvement, I checked there is a JIRA issue filed for the change (usually before you start working on it).

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 -DskipTests locally from root folder and I have committed all auto-generated changes.

@Croway
Copy link
Copy Markdown
Contributor

Croway commented Mar 4, 2026

please rebase this branch

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 4, 2026

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using build-all, build-dependents, skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

@davsclaus
Copy link
Copy Markdown
Contributor

use junit6 instead of 5

@jamesnetherton
Copy link
Copy Markdown
Contributor

Do we expect users will want to convert from BufferedImage? Just wondering if we can avoid introducing AWT and just check the mime / content-type and use byte[] or InputStream if image processing is required?

@davsclaus
Copy link
Copy Markdown
Contributor

Do we expect users will want to convert from BufferedImage? Just wondering if we can avoid introducing AWT and just check the mime / content-type and use byte[] or InputStream if image processing is required?

No we dont use that anywhere else. And indeed its not a type anyone really use.

Also this PR has an unit that doesn't really test the newly introduced converter

@davsclaus davsclaus marked this pull request as draft March 5, 2026 08:48
@davsclaus
Copy link
Copy Markdown
Contributor

@chandru9 can you eplain your reason for this PR

@chandru9
Copy link
Copy Markdown
Contributor Author

chandru9 commented Mar 5, 2026

@jamesnetherton : Good point. The BufferedImage converter was added to support direct image processing inputs, but since Camel routes typically handle images as byte[], InputStream, or WrappedFile, and we already detect the MIME type to create the appropriate ImageContent, introducing BufferedImage may not be necessary.

To avoid introducing the AWT dependency, I can remove the BufferedImage converter and rely on byte[] / InputStream based conversion using the detected content-type. This should cover the intended use cases for image processing in routes.

@chandru9
Copy link
Copy Markdown
Contributor Author

chandru9 commented Mar 5, 2026

@davsclaus : PR intended to improve the usability of the langchain4j-agent component by enabling automatic body conversion to AiAgentBody for common input types such as files, streams,text.image etc.
final advantage is to avoid the usage of convertBodyTo().
Also I will add tests for converter.

@davsclaus
Copy link
Copy Markdown
Contributor

Thanks, removing that AWT BufferedImage is welcome.

Copy link
Copy Markdown
Contributor

@orpiske orpiske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @chandru9! I like the general idea of the PR, but, please I'd like some clarifications regarding the fixed usage of PNG format (maybe I'm not fully understanding it ... but I tried pointing out the parts that are unclear to me).

@davsclaus davsclaus marked this pull request as ready for review March 9, 2026 14:53
@davsclaus
Copy link
Copy Markdown
Contributor

There are uncommitted changes
HEAD detached at pull/21704/merge
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java

@chandru9
Copy link
Copy Markdown
Contributor Author

@Croway , @davsclaus , @jamesnetherton and @orpiske
Thank you for the support.

Copy link
Copy Markdown
Contributor

@gnodet gnodet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work on this @chandru9! The direction of using Camel's type converter system instead of Agent.processBody() is architecturally sound. A few items still need to be addressed before this can be merged:

Blocking

  1. Uncommitted changes after build — as @davsclaus pointed out, the generated files don't match after a clean build. Please run:

    mvn install -pl components/camel-ai/camel-langchain4j-agent -DskipTests
    mvn formatter:format impsort:sort -pl components/camel-ai/camel-langchain4j-agent

    and commit the resulting changes.

  2. Star importdev.langchain4j.data.message.* needs to be expanded to individual imports. This was already flagged by @davsclaus. Please expand it to the specific classes used: AudioContent, Content, ImageContent, PdfFileContent, TextContent, VideoContent.

  3. Agent.processBody() is now dead code — the PR removes the call from LangChain4jAgentProducer but leaves the method on the Agent interface. Users who have custom Agent implementations overriding processBody() will find their overrides silently ignored. This is a breaking change. Please either:

    • Add @Deprecated to the method with a clear message pointing to the type converter approach, or
    • Remove the method entirely and document the change in the 4.19 upgrade guide.
  4. Behavioral change for String payloads — previously, when the body was a plain String, Agent.processBody() set the user message but did not create a Content object on the AiAgentBody. Now, textToAiAgentBody always wraps the String in a TextContent. If any custom Agent.chat() implementations check whether content is null to differentiate text-only vs multimodal messages, this will break them. Please verify this is intentional and compatible with AgentWithMemory and AgentWithoutMemory.

Moderate

  1. Inconsistent exchange.getIn() vs exchange.getMessage() — the PR correctly switches detectMimeTypeFromHeaders() and buildAiAgentBody() to exchange.getMessage(), but methods not touched by this PR (toAiAgentBody(), byteArrayToAiAgentBody(), detectMimeType()) still use exchange.getIn(). Since you're already in this file, it would be good to be consistent — but this can also be a follow-up.

  2. Test class named *IT but doesn't need external infrastructureLangChain4jAgentAutoConversionIT uses a mock agent and no testcontainers. Consider renaming to *Test so it runs with mvn test instead of requiring mvn verify.

  3. shouldAutoConvertInputStream test — this sends a ByteArrayInputStream without setting a MIME type header. detectMimeTypeFromHeaders() will throw IllegalArgumentException when no MIME type is found. Is this test expected to pass?

  4. Spurious blank line — there's an unnecessary blank line added at the beginning of createContent() (around line 193 in the diff).

Nit

  1. Removed inline comments on cloud header constants — the comments like // AWS S3, // Azure Blob Storage etc. were helpful for readability. Consider keeping them.

@chandru9
Copy link
Copy Markdown
Contributor Author

chandru9 commented Mar 10, 2026

Hello @gnodet, thank you for the detailed review,
I have addressed the review comments in the latest update:
2 - I have removed the star imports in my previous commits also after running format command somehow star imports are back in my IDE, I observed lately, but I preserved this part. :)
3 - Agent.processBody() is deprecated.
4- You are correct that AgentWithMemory and AgentWithoutMemory differentiate text-only vs multimodal messages based on content == null.
With the current implementation creating a TextContent, String payloads would always follow the multimodal path.
To preserve the previous behavior,
I have updated the textToAiAgentBody converter so that it only sets the userMessage and leaves content as null, ensuring text payloads continue to use the existing text-only flow.
5 - Adjusted to use exchange.getMessage().
6 - Test renamed.
7 - test cases adjusted
8 - removed the blank line.
9 - also retained inline comments,.

@davsclaus
Copy link
Copy Markdown
Contributor

org.apache.camel.component.langchain4j.agent.integration.LangChain4jAgentAutoConversionTest.shouldFailForUnsupportedMimeType 0.025s FAILURE
org.apache.camel.component.langchain4j.agent.integration.LangChain4jAgentAutoConversionTest.shouldAutoConvertInputStream 0.035s FAILURE

@gnodet gnodet merged commit 1cf2c48 into apache:main Mar 11, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants