langchain - 💡(How to fix) Fix bug(langchain): glob_search returns files in arbitrary order despite docstring promising mtime-desc sort

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…

The glob_search tool exposed by FilesystemFileSearchMiddleware documents that it "returns matching file paths sorted by modification time (most recently modified first)".

Internally it does call match.stat() for every match and builds a list of (virtual_path, modified_at_iso) tuples — but it never sorts that list before joining. The result order is whatever Path.glob() yields, which is OS- and filesystem-dependent.

Two consequences:

  1. Callers (and the LLM driving the tool) rely on the documented ordering to decide which files to inspect first. The current behavior silently breaks that contract.
  2. The stat() calls and datetime.fromtimestamp(...) conversions are pure overhead — their output is computed and immediately discarded.

Fix: sort matching by the ISO timestamp (descending) before extracting the paths.

Error Message

Error Message and Stack Trace (if applicable)

Root Cause

The glob_search tool exposed by FilesystemFileSearchMiddleware documents that it "returns matching file paths sorted by modification time (most recently modified first)".

Internally it does call match.stat() for every match and builds a list of (virtual_path, modified_at_iso) tuples — but it never sorts that list before joining. The result order is whatever Path.glob() yields, which is OS- and filesystem-dependent.

Two consequences:

  1. Callers (and the LLM driving the tool) rely on the documented ordering to decide which files to inspect first. The current behavior silently breaks that contract.
  2. The stat() calls and datetime.fromtimestamp(...) conversions are pure overhead — their output is computed and immediately discarded.

Fix: sort matching by the ISO timestamp (descending) before extracting the paths.

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

httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.2.0 orjson: 3.11.9 packaging: 26.2 pydantic: 2.13.4 pyyaml: 6.0.3 requests: 2.34.0 requests-toolbelt: 1.0.0 tenacity: 9.1.4 typing-extensions: 4.15.0 uuid-utils: 0.15.0 xxhash: 3.7.0 zstandard: 0.25.0

Code Example

import os
import time
import tempfile
from pathlib import Path
from langchain.agents.middleware.file_search import FilesystemFileSearchMiddleware

with tempfile.TemporaryDirectory() as root:
    root_path = Path(root)
    # Create three files with monotonically increasing mtimes.
    for name in ["oldest.txt", "middle.txt", "newest.txt"]:
        p = root_path / name
        p.write_text("x")
        time.sleep(0.05)

    mw = FilesystemFileSearchMiddleware(root_path=root_path, allow_absolute_paths=True)
    glob_search = mw.tools[0]  # `glob_search` tool

    result = glob_search.invoke({"pattern": "*.txt"})
    print(result)
    # Docstring promises: most recently modified first ->
    #   newest.txt
    #   middle.txt
    #   oldest.txt
    # Actual order is whatever Path.glob() returns -> filesystem-dependent.

---
RAW_BUFFERClick to expand / collapse

Submission checklist

  • 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-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

import os
import time
import tempfile
from pathlib import Path
from langchain.agents.middleware.file_search import FilesystemFileSearchMiddleware

with tempfile.TemporaryDirectory() as root:
    root_path = Path(root)
    # Create three files with monotonically increasing mtimes.
    for name in ["oldest.txt", "middle.txt", "newest.txt"]:
        p = root_path / name
        p.write_text("x")
        time.sleep(0.05)

    mw = FilesystemFileSearchMiddleware(root_path=root_path, allow_absolute_paths=True)
    glob_search = mw.tools[0]  # `glob_search` tool

    result = glob_search.invoke({"pattern": "*.txt"})
    print(result)
    # Docstring promises: most recently modified first ->
    #   newest.txt
    #   middle.txt
    #   oldest.txt
    # Actual order is whatever Path.glob() returns -> filesystem-dependent.

Error Message and Stack Trace (if applicable)

Description

The glob_search tool exposed by FilesystemFileSearchMiddleware documents that it "returns matching file paths sorted by modification time (most recently modified first)".

Internally it does call match.stat() for every match and builds a list of (virtual_path, modified_at_iso) tuples — but it never sorts that list before joining. The result order is whatever Path.glob() yields, which is OS- and filesystem-dependent.

Two consequences:

  1. Callers (and the LLM driving the tool) rely on the documented ordering to decide which files to inspect first. The current behavior silently breaks that contract.
  2. The stat() calls and datetime.fromtimestamp(...) conversions are pure overhead — their output is computed and immediately discarded.

Fix: sort matching by the ISO timestamp (descending) before extracting the paths.

System Info

System Information

OS: Linux OS Version: #29~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jun 26 14:16:59 UTC 2 Python Version: 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0]

Package Information

langchain_core: 1.4.0 langchain: 1.3.0 langsmith: 0.8.3 langchain_protocol: 0.0.15 langgraph_sdk: 0.3.14

Optional packages not installed

deepagents deepagents-cli

Other Dependencies

httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.2.0 orjson: 3.11.9 packaging: 26.2 pydantic: 2.13.4 pyyaml: 6.0.3 requests: 2.34.0 requests-toolbelt: 1.0.0 tenacity: 9.1.4 typing-extensions: 4.15.0 uuid-utils: 0.15.0 xxhash: 3.7.0 zstandard: 0.25.0

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 - 💡(How to fix) Fix bug(langchain): glob_search returns files in arbitrary order despite docstring promising mtime-desc sort