hsliuping/TradingAgents-CN · warning · ValueError

REDDIT FETCHING ERROR: max limit is less than the number of

Error message

REDDIT FETCHING ERROR: max limit is less than the number of files in the category. Will not be able to fetch any posts

What it means

Raised by fetch_top_from_category when max_limit is smaller than the number of files in the local reddit category directory. The function divides max_limit evenly across subreddit files, so fewer posts than files would yield zero posts per subreddit. NOTE: the comparison is inverted relative to the message wording — it fires when max_limit < file count.

Source

Thrown at tradingagents/dataflows/news/reddit.py:69

def fetch_top_from_category(
    category: Annotated[
        str, "Category to fetch top post from. Collection of subreddits."
    ],
    date: Annotated[str, "Date to fetch top posts from."],
    max_limit: Annotated[int, "Maximum number of posts to fetch."],
    query: Annotated[str, "Optional query to search for in the subreddit."] = None,
    data_path: Annotated[
        str,
        "Path to the data folder. Default is 'reddit_data'.",
    ] = "reddit_data",
):
    base_path = data_path

    all_content = []

    if max_limit < len(os.listdir(os.path.join(base_path, category))):
        raise ValueError(
            "REDDIT FETCHING ERROR: max limit is less than the number of files in the category. Will not be able to fetch any posts"
        )

    limit_per_subreddit = max_limit // len(
        os.listdir(os.path.join(base_path, category))
    )

    for data_file in os.listdir(os.path.join(base_path, category)):
        # check if data_file is a .jsonl file
        if not data_file.endswith(".jsonl"):
            continue

        all_content_curr_subreddit = []

        with open(os.path.join(base_path, category, data_file), "rb") as f:
            for i, line in enumerate(f):
                # skip empty lines
                if not line.strip():

View on GitHub (pinned to 74783e8817)

Solutions

  1. Increase max_limit to at least the number of files in the category directory (e.g. 50)
  2. Count files first: len(os.listdir(os.path.join(data_path, category))) and pass max_limit >= that
  3. Remove unused subreddit files from the directory if you truly need few posts

Example fix

# before
posts = get_reddit_global_news('2025-01-01', max_limit=5)
# after
posts = get_reddit_global_news('2025-01-01', max_limit=100)
Defensive patterns

Strategy: validation

Validate before calling

import os
n_files = len(os.listdir(os.path.join(data_path, category)))
max_limit = max(max_limit, n_files)
posts = fetch_top_from_category(data_path, category, max_limit)

Try / catch

try:
    posts = get_reddit_global_news(curr_date, max_limit)
except ValueError as e:
    if 'max limit' in str(e):
        posts = get_reddit_global_news(curr_date, 100)
    else:
        raise

Prevention

When it happens

Trigger: Calling get_reddit_global_news(..., max_limit=5) when the category dir (e.g. data_path/reddit/global) contains more than 5 files; small max_limit values with many cached subreddit JSON files.

Common situations: Lowering the post limit for faster tests, adding more subreddit files to the data directory, or a changed default data_path pointing at a larger directory.

Related errors


AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28). Data as JSON: /api/errors/a2e6cad19c8b59f6. Report an issue: GitHub.