ZhuLinsen/daily_stock_analysis · error · ValueError

文本超过 {MAX_TEXT_BYTES // 1024}KB 限制

Error message

文本超过 {MAX_TEXT_BYTES // 1024}KB 限制

What it means

Guard in parse_import_from_text: the UTF-8 byte length of the pasted text exceeds MAX_TEXT_BYTES (100KB). Measured on encoded bytes, not characters, so CJK-heavy text hits it sooner per character.

Source

Thrown at src/services/import_parser.py:248

            rows.append(parts)
    if not rows:
        return []
    df = pd.DataFrame(rows)
    return _parse_dataframe(df)


def parse_import_from_text(text: str) -> List[Tuple[Optional[str], Optional[str], str]]:
    """
    Parse clipboard/text into items.

    Args:
        text: Raw text (e.g. from clipboard).

    Returns:
        List of (code, name, confidence).
    """
    if len(text.encode("utf-8")) > MAX_TEXT_BYTES:
        raise ValueError(f"文本超过 {MAX_TEXT_BYTES // 1024}KB 限制")

    logger.debug(f"[ImportParser] 开始解析粘贴文本: bytes={len(text.encode('utf-8'))}")
    data = text.encode("utf-8")
    return parse_import_from_bytes(data, filename="paste.txt")

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Paste only the code (and optionally name) column
  2. Split the paste into batches under 100KB each
  3. If the content is really a file, use the file upload path (2MB limit) instead

Example fix

# before
items = parse_import_from_text(huge_clipboard_text)
# after
lines = [ln for ln in huge_clipboard_text.splitlines() if ln.strip()]
items = []
for i in range(0, len(lines), 500):
    items += parse_import_from_text('\n'.join(lines[i:i+500]))
Defensive patterns

Strategy: validation

Validate before calling

from src.services.import_parser import MAX_TEXT_BYTES
if len(text.encode('utf-8')) > MAX_TEXT_BYTES:
    text = '\n'.join(ln for ln in text.splitlines() if ln.strip() and looks_like_code_or_name(ln))

Type guard

def within_text_limit(t: str) -> bool:
    return len(t.encode('utf-8')) <= 100 * 1024

Prevention

When it happens

Trigger: Pasting a huge clipboard (entire watchlist page, thousands of rows with names) into the text-import UI.

Common situations: Select-all paste from a large spreadsheet; pasting an entire webpage rather than the code column; clipboard containing a whole CSV file.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/99f5784c674a72bf. Report an issue: GitHub.