fix: Resolve IllegalArgumentException for text MIME types in LangChain4j adapter#1185
Open
hemasekhar-p wants to merge 1 commit intogoogle:mainfrom
Open
fix: Resolve IllegalArgumentException for text MIME types in LangChain4j adapter#1185hemasekhar-p wants to merge 1 commit intogoogle:mainfrom
hemasekhar-p wants to merge 1 commit intogoogle:mainfrom
Conversation
25b3468 to
698d5fe
Compare
698d5fe to
4ad4cb6
Compare
4ad4cb6 to
d64286f
Compare
Author
|
@krwc Please take a look into it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
IllegalArgumentException: "Unknown or unhandled mime type"incorrectly thrown for valid text/JSON/XML inputs inLangChain4jadapter #1184Problem:
When
LangChain4j.toUserOrToolResultMessage()processesinlineDataparts with text-based MIME types, it instantiates aTextContentobject but adds it directly to the master list rather than assigning it to the locallc4jContentreference. this is causing the false validation for this conditionif (lc4jContent == null)and throwing an unexpectedIllegalArgumentException. Additionally, the existing string matching was too strict.equals(), causing valid payloads to fail if a charset parameter was appended like thisapplication/json; charset=utf-8Solution:
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 toUTF-8if no charset provided or malformed.Updated the control flow to assign the generated
TextContentdirectly to thelc4jContentvariable,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:
Added the following methods to
LangChain4jTest.javawhich 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.