Skip to content

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

@omricarmi4

Description

@omricarmi4

Description

There is a logic error in com.google.adk.models.langchain4j.LangChain4j within the toUserOrToolResultMessage(Content content) method. When parsing text/*, application/json, or +xml mime types, the code correctly creates a TextContent object but adds it directly to the lc4jContents list while leaving the local variable lc4jContent as null.

Immediately after this if/else if block, the code checks if lc4jContent == null and throws an IllegalArgumentException, making it impossible to process these standard text and JSON/XML content types.

Environment

  • ADK Version: 1.2.0
  • LangChain4J Version: 1.14.0
  • Class: com.google.adk.models.langchain4j.LangChain4j

Steps to Reproduce

Pass any Content object containing an inlineData Part with a text-based mime type (e.g., text/plain, application/json) through the LangChain4J model adapter.

Root Cause Analysis

In toUserOrToolResultMessage, the if/else block handles different mime types:

dev.langchain4j.data.message.Content lc4jContent = null;

// ... [audio/video/image/pdf logic successfully assigns lc4jContent] ...

} else if (mimeType.startsWith("text/") || "application/json".equals(mimeType) || mimeType.endsWith("+json") || mimeType.endsWith("+xml")) {
    // ❌ BUG: Adds directly to the list, leaving lc4jContent as null
    lc4jContents.add(TextContent.from(new String(bytes, StandardCharsets.UTF_8)));
}

// ❌ Throws exception because lc4jContent is still null for text/json/xml
if (lc4jContent == null) {
    throw new IllegalArgumentException("Unknown or unhandled mime type: " + mimeType);
}

// (For other types, it gets added here)
lc4jContents.add(lc4jContent);

Expected Behavior

Text-based content should be parsed into a TextContent object and successfully added to the message list without throwing an IllegalArgumentException.

Suggested Fix

Update the else if branch for text/JSON types to assign the value to lc4jContent instead of adding it directly to the list, maintaining consistency with the other mime type branches:

} else if (mimeType.startsWith("text/") || "application/json".equals(mimeType) || mimeType.endsWith("+json") || mimeType.endsWith("+xml")) {
    // ✅ FIX: Assign to the local variable instead
    lc4jContent = TextContent.from(new String(bytes, StandardCharsets.UTF_8));
}

if (lc4jContent == null) {
    throw new IllegalArgumentException("Unknown or unhandled mime type: " + mimeType);
}

lc4jContents.add(lc4jContent);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions