langchain - ✅(Solved) Fix TypeError in merge_lists when streaming Mistral responses with inline citations [9 pull requests, 5 comments, 5 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35259Fetched 2026-04-08 00:26:54
View on GitHub
Comments
5
Participants
5
Timeline
31
Reactions
0
Assignees
Timeline (top)
cross-referenced ×9referenced ×7commented ×5labeled ×4

Model: mistral-medium-2505 (but likely affects any Mistral model returning citations)

The error occurs when:

  • Using LangGraph's create_react_agent with Mistral
  • Streaming mode enabled
  • Tools like Tavily search that return results the model cites

Error Message

File ".../langchain_core/utils/_merge.py", line 114, in <listcomp> if "index" in e_left and e_left["index"] == e["index"] ~~~~~~~^^^^^^^^^ TypeError: string indices must be integers, not 'str'

Root Cause

Model: mistral-medium-2505 (but likely affects any Mistral model returning citations)

The error occurs when:

  • Using LangGraph's create_react_agent with Mistral
  • Streaming mode enabled
  • Tools like Tavily search that return results the model cites

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Other Dependencies

aiohttp: 3.11.14 dataclasses-json: 0.6.7 elasticsearch: 8.19.3 httpx: 0.28.1 httpx-sse: 0.4.1 jsonpatch: 1.33 langgraph: 1.0.5 mcp: 1.17.0 numpy: 2.2.6 openai: 2.14.0 opentelemetry-api: 1.34.1 opentelemetry-exporter-otlp-proto-http: 1.34.1 opentelemetry-sdk: 1.34.1 orjson: 3.10.18 packaging: 24.2 pydantic: 2.11.7 pydantic-settings: 2.10.1 pytest: 7.4.0 pyyaml: 6.0.3 PyYAML: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.0.0 SQLAlchemy: 2.0.41 sqlalchemy: 2.0.41 tenacity: 8.2.3 tiktoken: 0.9.0 tokenizers: 0.21.2 typing-extensions: 4.14.0 unstructured: 0.18.21 unstructured-client: 0.39.1 uuid-utils: 0.12.0 zstandard: 0.23.0

PR fix notes

PR #35265: fix(core): handle non-dict elements in merge_lists index lookup

Description (problem / solution / changelog)

Description

Fixes #35259

When streaming Mistral responses with inline citations, the content list can contain a mix of str and dict elements. For example:

[
    "start answer...",
    {"type": "reference", "index": 0, "reference_ids": ["iKcb2CAQ7"]},
    "other answer..."
]

The merge_lists function in _merge.py assumed all elements in the merged list would be dicts when searching for a matching index. When it encountered a str element, the expression "index" in e_left would check for substring containment (valid but wrong), and then e_left["index"] would fail with TypeError: string indices must be integers, not 'str'.

Fix

Added an isinstance(e_left, dict) guard in the list comprehension (line 114) that finds merge targets by index. This ensures only dict elements are checked for matching index keys, skipping strings and other non-dict types.

Tests

Added 2 new parametrized test cases to test_merge_lists:

  1. Mixed str and dict with matching index - verifies dicts with the same index are correctly merged while strings are preserved
  2. Mixed str and dict with non-matching index - verifies new dict elements are appended when no matching index exists

Issue

This affects any Mistral model returning inline citations (e.g., mistral-medium-2505) when used with streaming and tools like Tavily search.

Dependencies

None

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +3/-1)
  • libs/core/tests/unit_tests/utils/test_utils.py (modified, +35/-0)

PR #35271: fix: TypeError in merge_lists with mixed content types & pretty_repr HTML for non-string content

Description (problem / solution / changelog)

Fixes

1. TypeError in merge_lists when streaming Mistral responses with inline citations (#35259)

Problem: When merge_lists processes a list containing both strings and dicts with "index" keys, the list comprehension tries to access e_left["index"] on string elements, causing TypeError: string indices must be integers, not 'str'.

Fix: Added isinstance(e_left, dict) guard before checking for "index" key in the list comprehension.

2. pretty_repr(html=True) does not return HTML for non-string content (#34875)

Problem: When BaseMessage.content is a list (valid type), pretty_repr(html=True) returns the raw Python repr with no HTML formatting, despite the docstring promising HTML output.

Fix:

  • Parse list content blocks (text, image_url, plain strings) into readable text
  • Apply HTML escaping and line break conversion when html=True
  • Resolves the existing TODO comment in the code

Changed files

  • libs/core/langchain_core/messages/base.py (modified, +29/-2)
  • libs/core/langchain_core/utils/_merge.py (modified, +3/-1)

PR #35290: fix(core): fix TypeError in merge_lists when merged list contains non-dict elements

Description (problem / solution / changelog)

Description

Fixes #35259.

When streaming Mistral responses with inline citations, merge_lists can encounter a mixed list of strings and dicts. The list comprehension that searches for matching index values assumed all elements in merged were dicts, causing:

TypeError: string indices must be integers, not 'str'

Changes

Added isinstance(e_left, dict) check in the list comprehension before accessing dict keys, so string elements are safely skipped.

Testing

Reproduces with the exact example from the issue:

from langchain_core.utils._merge import merge_lists
merged = ["start answer...", {"type": "reference", "index": 0, "reference_ids": ["iKcb2CAQ7"]}, "other answer..."]
other = [{"type": "reference", "index": 0, "reference_ids": ["DGqzzmqc3"]}]
result = merge_lists(merged, other)  # no longer raises TypeError

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +3/-1)

PR #35349: fix(core): handle non-dict elements in merge_lists index lookup

Description (problem / solution / changelog)

Adds a type guard in merge_lists to skip non-dict elements when looking up the merge index, preventing AttributeError when streaming Mistral citation objects.

Fixes #35259

Generated with the help of AI tools.

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +2/-1)
  • libs/core/tests/unit_tests/utils/test_utils.py (modified, +35/-0)

PR #35481: fix(core): guard merge_lists against non-dict elements when matching by index

Description (problem / solution / changelog)

Problem

merge_lists crashes with TypeError: string indices must be integers, not 'str' when the merged list contains a mix of strings and dicts with "index" keys.

This happens with Mistral streaming responses that include inline citations — the content array alternates between text strings and reference dicts:

merged = [
    "start answer...",
    {"type": "reference", "index": 0, "reference_ids": ["iKcb2CAQ7"]},
    "other answer..."
]
other = [{"type": "reference", "index": 0, "reference_ids": ["DGqzzmqc3"]}]

merge_lists(merged, other)  # TypeError!

Root Cause

The index-matching list comprehension iterates over all elements in merged and checks "index" in e_left. For string elements, Python's in operator checks for substring containment (which works), but then e_left["index"] tries to index a string with a string key, which raises TypeError.

Fix

Add isinstance(e_left, dict) guard before accessing dict-specific attributes.

Tests

Added regression test with mixed strings and dicts (the exact pattern from the Mistral streaming scenario). 81 tests pass.

Fixes #35259

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +2/-1)
  • libs/core/tests/unit_tests/utils/test_utils.py (modified, +21/-0)

PR #35623: fix(core): prevent TypeError in merge_lists when list contains mixed strings and dicts

Description (problem / solution / changelog)

Bug

merge_lists raises TypeError: string indices must be integers, not 'str' when the merged list contains a mix of plain strings and dicts with an "index" key. This happens during Mistral streaming with inline citations.

Reported in: #35259

Root cause

The guard "index" in e_left at line 121 of _merge.py performs substring containment when e_left is a string, not dict key lookup. If any string in the list happens to contain the substring "index", the check passes and e_left["index"] fails because you cannot index a string with a string key.

Fix

Add isinstance(e_left, dict) before the "index" in e_left check:

# Before
"index" in e_left

# After
isinstance(e_left, dict) and "index" in e_left

Tests

  • Added test_merge_lists_mixed_str_and_dict_with_index covering the exact scenario from the issue
  • All 54 merge tests pass with no regressions

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +2/-1)
  • libs/core/tests/unit_tests/utils/test_utils.py (modified, +25/-0)

PR #35625: Fix TypeError in merge_lists when handling mixed type lists

Description (problem / solution / changelog)

Fixes #35259. Add isinstance check before accessing dict keys to prevent TypeError when merged list contains mixed types (strings and dicts).

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +2/-1)

PR #35655: core: fix merge_lists crash when merged list contains non-dict elements

Description (problem / solution / changelog)

Description

Fixes a bug in merge_lists() where iterating over merged to find elements with a matching "index" key assumes all elements are dicts. However, merged can contain non-dict elements (strings, ints, etc.):

  • Using "index" in e_left on a string checks for substring membership (unexpected behavior)
  • Using "index" in e_left on an int raises TypeError: argument of type 'int' is not iterable

This adds an isinstance(e_left, dict) guard before the "index" in e_left check to prevent both cases.

Related: #35259

Issue

Prevents TypeError and unexpected substring matching when non-dict items exist in merged lists during tool call chunk aggregation.

Dependencies

None.

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +2/-1)

PR #35876: fix: add type check in merge_lists for non-dict elements

Description (problem / solution / changelog)

Summary

Fixes #35259

  • merge_lists in langchain_core/utils/_merge.py crashes with TypeError: string indices must be integers when merging streaming chunks that contain a mix of strings and dicts (e.g., Mistral responses with inline citations).
  • Root cause: The list comprehension checks "index" in e_left without first verifying e_left is a dict. When e_left is a string, Python's in operator performs substring matching instead of dict key lookup. If the check passes, e_left["index"] fails because string indexing requires integers, not strings.
  • Fix: Add isinstance(e_left, dict) guard before the "index" in e_left check to skip non-dict elements during the index-matching scan.

Reproduction

from langchain_core.utils._merge import merge_lists

merged = [
    "start answer...",
    {"type": "reference", "index": 0, "reference_ids": ["iKcb2CAQ7"]},
    "other answer..."
]
other = [{"type": "reference", "index": 0, "reference_ids": ["DGqzzmqc3"]}]

# Before fix: TypeError: string indices must be integers
# After fix: works correctly
result = merge_lists(merged, other)

Test plan

  • Verify the reproduction case from the issue no longer raises TypeError
  • Existing merge_lists tests continue to pass
  • Test with Mistral streaming citations end-to-end (requires Mistral API key)

Changed files

  • libs/core/langchain_core/utils/_merge.py (modified, +2/-1)

Code Example

from langchain_core.utils._merge import merge_lists

merged = [
    "start answer...",
    {"type": "reference", "index": 0, "reference_ids": ["iKcb2CAQ7"]},
    "other answer..."
]

# Another reference to merge (from streaming chunk)
other = [{"type": "reference", "index": 0, "reference_ids": ["DGqzzmqc3"]}]

# This raises TypeError
result = merge_lists(merged, other)

---

File ".../langchain_core/utils/_merge.py", line 114, in <listcomp>
      if "index" in e_left and e_left["index"] == e["index"]
                               ~~~~~~~^^^^^^^^^
  TypeError: string indices must be integers, not 'str'
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

from langchain_core.utils._merge import merge_lists

merged = [
    "start answer...",
    {"type": "reference", "index": 0, "reference_ids": ["iKcb2CAQ7"]},
    "other answer..."
]

# Another reference to merge (from streaming chunk)
other = [{"type": "reference", "index": 0, "reference_ids": ["DGqzzmqc3"]}]

# This raises TypeError
result = merge_lists(merged, other)

Error Message and Stack Trace (if applicable)

File ".../langchain_core/utils/_merge.py", line 114, in <listcomp>
      if "index" in e_left and e_left["index"] == e["index"]
                               ~~~~~~~^^^^^^^^^
  TypeError: string indices must be integers, not 'str'

Description

Model: mistral-medium-2505 (but likely affects any Mistral model returning citations)

The error occurs when:

  • Using LangGraph's create_react_agent with Mistral
  • Streaming mode enabled
  • Tools like Tavily search that return results the model cites

System Info

System Information

OS: Linux OS Version: #1 SMP Thu Oct 5 21:02:42 UTC 2023 Python Version: 3.11.13 (main, Jun 20 2025, 10:35:36) [GCC 11.4.0]

Package Information

langchain_core: 1.2.5 langchain: 1.2.0 langchain_community: 0.4.1 langsmith: 0.3.45 langchain_classic: 1.0.1 langchain_elasticsearch: 1.0.0 langchain_experimental: 0.4.1 langchain_mcp_adapters: 0.2.1 langchain_mistralai: 1.1.1 langchain_openai: 1.1.6 langchain_tavily: 0.2.16 langchain_text_splitters: 1.1.0 langchain_unstructured: 1.0.1 langgraph_sdk: 0.3.1 langgraph_supervisor: 0.0.31

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.14 dataclasses-json: 0.6.7 elasticsearch: 8.19.3 httpx: 0.28.1 httpx-sse: 0.4.1 jsonpatch: 1.33 langgraph: 1.0.5 mcp: 1.17.0 numpy: 2.2.6 openai: 2.14.0 opentelemetry-api: 1.34.1 opentelemetry-exporter-otlp-proto-http: 1.34.1 opentelemetry-sdk: 1.34.1 orjson: 3.10.18 packaging: 24.2 pydantic: 2.11.7 pydantic-settings: 2.10.1 pytest: 7.4.0 pyyaml: 6.0.3 PyYAML: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.0.0 SQLAlchemy: 2.0.41 sqlalchemy: 2.0.41 tenacity: 8.2.3 tiktoken: 0.9.0 tokenizers: 0.21.2 typing-extensions: 4.14.0 unstructured: 0.18.21 unstructured-client: 0.39.1 uuid-utils: 0.12.0 zstandard: 0.23.0

extent analysis

Problem Summary

The issue is a TypeError when merging lists in LangChain's merge_lists function, specifically when dealing with a string index.

Root Cause Analysis

The root cause is likely due to the fact that the merge_lists function is trying to access a string index as if it were an integer.

Fix Plan

Step 1: Update merge_lists function to handle string indices

def merge_lists(left, right):
    if isinstance(left, str):
        return [left] + right
    elif isinstance(right, str):
        return left + [right]
    else:
        # existing logic
        pass

Step 2: Update the merge_lists function to handle nested lists

def merge_lists(left, right):
    if isinstance(left, str):
        return [left] + right
    elif isinstance(right, str):
        return left + [right]
    elif isinstance(left, list) and isinstance(right, list):
        merged = []
        for e_left, e_right in zip(left, right):
            if isinstance(e_left, dict) and isinstance(e_right, dict):
                merged.append(merge_dicts(e_left, e_right))
            elif isinstance(e_left, list) and isinstance(e_right, list):
                merged.append(merge_lists(e_left, e_right))
            else:
                merged.append(e_left)
                merged.append(e_right)
        return merged
    else:
        # existing logic
        pass

Step 3: Update the merge_dicts function to handle string keys

def merge_dicts(left, right):
    merged = {}
    for key in set(list(left.keys()) + list(right.keys())):
        if key in left and key in right:
            if isinstance(left[key], str) and isinstance(right[key], str):
                merged[key] = left[key] + right[key]
            elif isinstance(left[key], list)

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

langchain - ✅(Solved) Fix TypeError in merge_lists when streaming Mistral responses with inline citations [9 pull requests, 5 comments, 5 participants]