Skip to content

LangChain4j - Make modelName optional if providing ChatModel or StreamingChatModel#1126

Open
marttp wants to merge 1 commit intogoogle:mainfrom
marttp:issue_1124
Open

LangChain4j - Make modelName optional if providing ChatModel or StreamingChatModel#1126
marttp wants to merge 1 commit intogoogle:mainfrom
marttp:issue_1124

Conversation

@marttp
Copy link
Copy Markdown

@marttp marttp commented Apr 9, 2026

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):

2. Or, if no issue exists, describe the change:

N/A

Testing Plan

Unit Tests:

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

Command

mvn test -pl contrib/langchain4j -am
[INFO] Results:
[INFO] 
[WARNING] Tests run: 27, Failures: 0, Errors: 0, Skipped: 6
[INFO] 
[INFO] 
[INFO] --- jacoco:0.8.12:report (report) @ google-adk-langchain4j ---
[INFO] Loading execution data file /Users/tpcoder/Documents/Working/OSS Contribution/adk-java/contrib/langchain4j/target/jacoco.exec
[INFO] Analyzed bundle 'Agent Development Kit - LangChain4j' with 6 classes
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Google Agent Development Kit Maven Parent POM 1.0.1-SNAPSHOT:
[INFO] 
[INFO] Google Agent Development Kit Maven Parent POM ...... SUCCESS [  0.064 s]
[INFO] Agent Development Kit .............................. SUCCESS [ 22.594 s]
[INFO] Agent Development Kit - LangChain4j ................ SUCCESS [  2.265 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  25.493 s
[INFO] Finished at: 2026-04-09T08:02:22+07:00
[INFO] ------------------------------------------------------------------------

Manual End-to-End (E2E) Tests:

I created a sample project to verify session interaction

public class MultiProviderAgent {

  /**
   * Creates a science teacher agent powered by Anthropic Claude.
   */
  public static LlmAgent createClaudeAgent() {
    AnthropicChatModel claudeModel =
        AnthropicChatModel.builder()
            .apiKey(System.getenv("ANTHROPIC_API_KEY"))
            .modelName("claude-sonnet-4-6")
            .build();

    LangChain4j adkModel = LangChain4j.builder()
        .chatModel(claudeModel)
        .build();
    
    return LlmAgent.builder()
        .name("claude-science-agent")
        .description("A science teacher powered by Claude")
        .model(adkModel)
        .instruction(
            """
                You are a science teacher.
                Explain concepts clearly and concisely in 2-3 sentences.
                """)
        .build();
  }

  /**
   * Creates a history teacher agent powered by Google Gemini.
   */
  public static LlmAgent createGeminiAgent() {
    GoogleAiGeminiChatModel geminiModel =
        GoogleAiGeminiChatModel.builder()
            .apiKey(System.getenv("GOOGLE_API_KEY"))
            .modelName("gemini-2.5-flash")
            .build();

    LangChain4j adkModel = LangChain4j.builder()
        .chatModel(geminiModel)
        .build();

    return LlmAgent.builder()
        .name("gemini-history-agent")
        .description("A history teacher powered by Gemini")
        .model(adkModel)
        .instruction(
            """
                You are a history teacher.
                Provide accurate and engaging historical facts in 2-3 sentences.
                """)
        .build();
  }

  private MultiProviderAgent() {
  }
}

And triggered by this

  public static void main(String[] args) {
    LlmAgent claudeAgent = MultiProviderAgent.createClaudeAgent();
    LlmAgent geminiAgent = MultiProviderAgent.createGeminiAgent();

    askAgent(claudeAgent, "What is quantum entanglement?");
    askAgent(geminiAgent, "What caused the fall of the Roman Empire?");
  }

E2E Result

--- claude-science-agent ---
Q: What is quantum entanglement?
A: Quantum entanglement is a phenomenon where two or more particles become linked in such a way that the quantum state of one particle instantly influences the state of the other, no matter how far apart they are. When you measure a property of one entangled particle, you immediately know the corresponding property of its partner. Einstein famously called this "spooky action at a distance," and while it seems mysterious, it has been repeatedly confirmed by experiments and is even being used in emerging technologies like quantum computing and cryptography.

--- gemini-history-agent ---
Q: What caused the fall of the Roman Empire?
A: The fall of the Western Roman Empire in 476 CE was not a single event but a complex process driven by multiple factors. Relentless barbarian invasions, like those by the Goths and Vandals, severely strained Rome's vast borders and military resources. Simultaneously, internal issues such as political instability, economic decline, and social upheaval eroded the empire's foundations from within.

Process finished with exit code 0

Checklist

  • I have read the CONTRIBUTING.md document.
  • My pull request contains a single commit.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The LangChain4j builder now auto-detects modelName from the underlying ChatModel or StreamingChatModel via
defaultRequestParameters().modelName(), making it optional when the model already exposes it. Explicit modelName() still takes precedence for backward compatibility. If model name cannot be resolved from any source, the builder fails fast with a clear IllegalStateException.

@marttp marttp changed the title Make modelName optional if providing ChatModel or StreamingChatModel Langchain4j - Make modelName optional if providing ChatModel or StreamingChatModel Apr 9, 2026
@marttp marttp changed the title Langchain4j - Make modelName optional if providing ChatModel or StreamingChatModel LangChain4j - Make modelName optional if providing ChatModel or StreamingChatModel Apr 9, 2026
@marttp marttp marked this pull request as ready for review April 9, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove mandatory model name in LangChain4j integration

1 participant