ZhuLinsen/daily_stock_analysis · warning · HTTPException

share_image_too_large

share_image_too_large

Error message

报告内容超过分享图片上限 {max_chars} 字符

What it means

413 share_image_too_large from GET /history/{record_id}/share-image-html. After loading the record and its markdown, the handler compares len(markdown_content) against config.markdown_to_image_max_chars (default 15000, overridable via configuration). Reports longer than the limit cannot be rendered into the deterministic share-image HTML, so the request is rejected before generation. This is a size guard, not a server fault.

Source

Thrown at api/v1/endpoints/history.py:835

    response_class=HTMLResponse,
    responses={
        200: {"description": "供桌面端内置 Chromium 渲染的分享图 HTML"},
        404: {"description": "报告不存在", "model": ErrorResponse},
        413: {"description": "报告内容超过分享图长度上限", "model": ErrorResponse},
        500: {"description": "报告生成失败", "model": ErrorResponse},
    },
    summary="获取历史报告分享图 HTML",
    description="根据历史报告与持久化结构化数据生成只供桌面端本地截图的确定性 HTML",
)
def get_history_share_image_html(
    record_id: str,
    db_manager: DatabaseManager = Depends(get_database_manager),
) -> HTMLResponse:
    result, markdown_content = _history_share_image_input(record_id, db_manager)
    config = get_config()
    max_chars = getattr(config, "markdown_to_image_max_chars", 15000)
    if len(markdown_content) > max_chars:
        raise HTTPException(
            status_code=413,
            detail={
                "error": "share_image_too_large",
                "message": f"报告内容超过分享图片上限 {max_chars} 字符",
            },
        )

    try:
        html = build_share_image_html(
            markdown_content,
            structured_payload=_history_share_image_payload(result),
            branding=_history_share_image_branding(config),
        )
    except Exception as exc:
        logger.error("Share image HTML generation failed for %s: %s", record_id, exc)
        raise HTTPException(
            status_code=500,
            detail={

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Raise markdown_to_image_max_chars in the service configuration (and restart/redeploy) if larger images are acceptable.
  2. Generate the report as a shorter 'standard' report type and share that instead.
  3. Fetch /history/{record_id}/markdown first, check its length, and only request the share image when under the limit.
  4. If the limit is intentional, surface a friendly 'report too large to share as image' message in the client.

Example fix

# before
max_chars = 15000  # default; detailed reports exceed it

# after (config)
markdown_to_image_max_chars = 30000
Defensive patterns

Strategy: validation

Validate before calling

const md = await fetchMarkdown(id); // GET /history/{id}/markdown
const maxChars = 15000; // must mirror config markdown_to_image_max_chars
if (md.content.length > maxChars) {
  showTooLargeNotice(md.content.length, maxChars); // skip the share-image call
}

Try / catch

try { await getShareHtml(id); }
catch (e) { if (isHttp413(e)) showShareTooLarge(e.maxChars); }

Prevention

When it happens

Trigger: Requesting a share image for a long-form 'detailed' report whose markdown exceeds the configured cap; lowering markdown_to_image_max_chars in config while retaining large old reports; reports with very long news/strategy sections.

Common situations: Detailed analysis reports naturally exceeding 15k characters; environments that tuned the cap down for performance; desktop share flow hitting the limit only for specific stocks with dense content

Related errors


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