Skip to content

fix: Resolve IllegalArgumentException for text MIME types in LangChain4j adapter#1185

Open
hemasekhar-p wants to merge 1 commit intogoogle:mainfrom
hemasekhar-p:fix-langchain4j-mimetype
Open

fix: Resolve IllegalArgumentException for text MIME types in LangChain4j adapter#1185
hemasekhar-p wants to merge 1 commit intogoogle:mainfrom
hemasekhar-p:fix-langchain4j-mimetype

Conversation

@hemasekhar-p
Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
When LangChain4j.toUserOrToolResultMessage() processes inlineData parts with text-based MIME types, it instantiates a TextContent object but adds it directly to the master list rather than assigning it to the local lc4jContent reference. this is causing the false validation for this condition if (lc4jContent == null) and throwing an unexpected IllegalArgumentException. Additionally, the existing string matching was too strict .equals(), causing valid payloads to fail if a charset parameter was appended like this application/json; charset=utf-8

Solution:
Modified the text MIME type matching logic to use .startsWith() and .contains() to gracefully tolerate appended parameters like boundaries or charsets.

Implemented a private extractCharset() helper method to dynamically read the provided character set and safely falling back to UTF-8 if no charset provided or malformed.

Updated the control flow to assign the generated TextContent directly to the lc4jContent variable,

Testing Plan

Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added the following methods to LangChain4jTest.java which all execute and pass successfully:

testGenerateContentWithTextPlainInlineData - Verifies standard text parsing.

testGenerateContentWithApplicationJsonInlineData - Verifies JSON payload parsing.

testGenerateContentWithUnsupportedMimeType - Verifies the unsupported types still correctly throws IllegalArgumentException for truly unhandled types.

testGenerateContentWithExplicitCharset - Verifies the dynamic extraction of custom charsets.

testGenerateContentWithMalformedCharsetFallback - Verifies safe fallback to UTF-8 when provided with unsupported charset parameters.

@hemasekhar-p hemasekhar-p self-assigned this May 6, 2026
@hemasekhar-p hemasekhar-p force-pushed the fix-langchain4j-mimetype branch from 25b3468 to 698d5fe Compare May 6, 2026 07:57
@hemasekhar-p hemasekhar-p changed the title Fix langchain4j mimetype fix: Resolve IllegalArgumentException for text MIME types in LangChain4j adapter May 6, 2026
@hemasekhar-p hemasekhar-p force-pushed the fix-langchain4j-mimetype branch from 698d5fe to 4ad4cb6 Compare May 6, 2026 08:15
@hemasekhar-p
Copy link
Copy Markdown
Author

@krwc Please take a look into it.

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.

IllegalArgumentException: "Unknown or unhandled mime type" incorrectly thrown for valid text/JSON/XML inputs in LangChain4j adapter

1 participant