[GH-ISSUE #64] instanceof AIMessageChunk Fails When Using Ollama Provider #18

Closed
opened 2026-02-16 06:16:49 -05:00 by yindo · 2 comments
Owner

Originally created by @sugengsuprayogi on GitHub (Nov 21, 2025).
Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/64

Summary

When streaming responses using the Ollama provider, the returned chunks look like AIMessageChunk objects, but instanceof AIMessageChunk returns false.
The exact same code works as expected when using OpenAI models.

This suggests that the Ollama integration is constructing message chunks using a different module instance of AIMessageChunk, causing the prototype chain to mismatch.


Reproduction

OpenAI (works)

Raw chunk: AIMessageChunk {
  id: "chatcmpl-xxx",
  content: "kan",
  response_metadata: { model_provider: "openai", usage: {} },
  ...
}

is AIMessageChunk: true
constructor: AIMessageChunk
type: ai

Ollama (fails)

Raw chunk: AIMessageChunk {
  id: "run-xxx",
  content: "al",
  response_metadata: {},
  ...
}

is AIMessageChunk: false
constructor: AIMessageChunk
type: ai

Notice both objects expose the same fields and the same constructor name, but instanceof returns false.


Diagnosis

This typically happens when a class is constructed from a different copy of the module.
Most likely:

  • The Ollama DeepAgent integration imports AIMessageChunk from a bundled/isolated instance of @langchain/core
  • The application imports AIMessageChunk from a different instance
    → Therefore the prototype comparison inside instanceof fails.

This does not happen with OpenAI provider, indicating consistent import paths on that side.


Expected Behavior

instanceof AIMessageChunk should return true for all providers that generate AIMessageChunk objects, including Ollama.


Why This Causes Problems

Libraries and downstream apps frequently use:

if (chunk instanceof AIMessageChunk) {
    // handle AI message chunks
}

Because Ollama chunks fail this check, they require special-case logic that breaks the uniformity of message streaming.


Possible Fixes

  1. Ensure the Ollama provider constructs chunks using the same import path:

    import { AIMessageChunk } from "@langchain/core/messages";
    
  2. Avoid bundling or including a second copy of @langchain/core.

  3. Re-export and use a single instance from deepagentjs to guarantee consistency.


Additional Notes

Despite failing instanceof, the objects are valid message chunks and .type === "ai" works correctly.
Normalizing manually also fixes the issue:

new AIMessageChunk({ ...chunk });

But ideally, this should not be required.

Originally created by @sugengsuprayogi on GitHub (Nov 21, 2025). Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/64 ### **Summary** When streaming responses using the Ollama provider, the returned chunks **look like** `AIMessageChunk` objects, but `instanceof AIMessageChunk` returns `false`. The exact same code **works as expected** when using OpenAI models. This suggests that the Ollama integration is constructing message chunks using a *different module instance* of `AIMessageChunk`, causing the prototype chain to mismatch. --- ## **Reproduction** ### **OpenAI (works)** ``` Raw chunk: AIMessageChunk { id: "chatcmpl-xxx", content: "kan", response_metadata: { model_provider: "openai", usage: {} }, ... } is AIMessageChunk: true constructor: AIMessageChunk type: ai ``` ### **Ollama (fails)** ``` Raw chunk: AIMessageChunk { id: "run-xxx", content: "al", response_metadata: {}, ... } is AIMessageChunk: false constructor: AIMessageChunk type: ai ``` Notice both objects expose the same fields and the same constructor name, but `instanceof` returns `false`. --- ## **Diagnosis** This typically happens when a class is constructed from **a different copy of the module**. Most likely: * The Ollama DeepAgent integration imports `AIMessageChunk` from a bundled/isolated instance of `@langchain/core` * The application imports `AIMessageChunk` from a different instance → Therefore the prototype comparison inside `instanceof` fails. This does **not** happen with OpenAI provider, indicating consistent import paths on that side. --- ## **Expected Behavior** `instanceof AIMessageChunk` should return `true` for all providers that generate `AIMessageChunk` objects, including Ollama. --- ## **Why This Causes Problems** Libraries and downstream apps frequently use: ```ts if (chunk instanceof AIMessageChunk) { // handle AI message chunks } ``` Because Ollama chunks fail this check, they require special-case logic that breaks the uniformity of message streaming. --- ## **Possible Fixes** 1. Ensure the Ollama provider constructs chunks using the same import path: ```ts import { AIMessageChunk } from "@langchain/core/messages"; ``` 2. Avoid bundling or including a second copy of `@langchain/core`. 3. Re-export and use a single instance from deepagentjs to guarantee consistency. --- ## **Additional Notes** Despite failing `instanceof`, the objects are valid message chunks and `.type === "ai"` works correctly. Normalizing manually also fixes the issue: ```ts new AIMessageChunk({ ...chunk }); ``` But ideally, this should not be required.
yindo added the bughelp wanted labels 2026-02-16 06:16:49 -05:00
yindo closed this issue 2026-02-16 06:16:49 -05:00
Author
Owner

@christian-bromann commented on GitHub (Jan 3, 2026):

if (chunk instanceof AIMessageChunk) {

We generally don't recommend to do instanceof checks for LangChain message objects. Instead do AIMessageChunk.isInstance(chunk). I don't think this is something we can fix within deepagentjs. It is interesting to see that it fails particularly with Ollama but not with OpenAI. Not sure when we get a chance to look into this but any investigations and contributions would be much appreciated.

@christian-bromann commented on GitHub (Jan 3, 2026): > if (chunk instanceof AIMessageChunk) { We generally don't recommend to do `instanceof` checks for LangChain message objects. Instead do `AIMessageChunk.isInstance(chunk)`. I don't think this is something we can fix within deepagentjs. It is interesting to see that it fails particularly with Ollama but not with OpenAI. Not sure when we get a chance to look into this but any investigations and contributions would be much appreciated.
Author
Owner

@VedantMadane commented on GitHub (Jan 10, 2026):

@sugengsuprayogi @christian-bromann Taking #64. Will replace instanceof with AIMessageChunk.isInstance() per recommendation. Working repro + PR.

@VedantMadane commented on GitHub (Jan 10, 2026): @sugengsuprayogi @christian-bromann Taking #64. Will replace instanceof with AIMessageChunk.isInstance() per recommendation. Working repro + PR.
yindo changed title from instanceof AIMessageChunk Fails When Using Ollama Provider to [GH-ISSUE #64] instanceof AIMessageChunk Fails When Using Ollama Provider 2026-06-05 17:20:58 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#18