排查并修复 FinSight-AI AI 研报无法生成的报错 - #2
Merged
Merged
Conversation
Co-authored-by: multica-agent <github@multica.ai>
4tj
commented
May 24, 2026
Contributor
Author
There was a problem hiding this comment.
Reviewer 阶段性结论:PASSED
Code review
实际改动仅 3 文件 32+/2-:
ai-service/app/main.pyL138-143:fallback 路径不再把 Ollama 异常字符串塞入model,统一回退到fallback.model(="rule-fallback",固定 13 字节,<<128)。根因结论与修复一致。ai-service/requirements.txt:补requests==2.32.3。app/main.py:232确实import requests,原先依赖 akshare 间接带入,显式化合理。ai-service/tests/test_stock_analysis.py:单测断言response.model == "rule-fallback"且len(response.model) <= 128且aiGenerated is False,精确覆盖"长异常 fallback"路径。
无未声明的无关改动;无 DB schema / migration / DDL(命令检测确认 0 命中);其他成功路径 model=ollama_model()(典型 "qwen2.5:7b",远低于 128)未变。
本地复跑
uv run --with fastapi==0.115.5 --with pydantic==2.9.2 --with requests==2.32.3 python -m unittest discover -s tests→Ran 1 test ... OK- TestClient 调用
/analyze-stock(OLLAMA_BASE_URL=http://127.0.0.1:1,强制连接失败)→status_code=200,model=rule-fallback,model_length=13,source=fallback-rule,aiGenerated=False✅ docker run --rm -v "$PWD":/workspace -w /workspace/backend maven:3.9.9-eclipse-temurin-17 mvn test→Tests run: 4, Failures: 0, Errors: 0, Skipped: 1,BUILD SUCCESS。被 skip 的是FinSightInfrastructureSmokeTest(Testcontainers,容器内无法访问宿主 docker socket,CI ubuntu-latest 同样无 daemon,已知限制)。- GitHub Actions CI:
Backend testsSUCCESS,Script syntaxSUCCESS。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
AI 研报在 Ollama 不可用时会走规则 fallback,但 sidecar 把完整连接异常写进
model字段,导致后端持久化到stock_analysis_reports.model VARCHAR(128)时失败。根因结论
复现
/analyze-stock在 Ollama 未启动时返回 HTTP 200,但model为qwen2.5:7b unavailable: HTTPConnectionPool(...),本地复现长度为 262。该值超过生产库stock_analysis_reports.model的 128 字符上限,所以后端保存研报时报错,研报生成链路中断。修复方案
ai-service/app/main.py: Ollama 调用失败时仍返回规则 fallback,但model保持为稳定的rule-fallback,避免把异常详情写入持久化字段。ai-service/requirements.txt: 显式声明requests,因为 Ollama 调用路径直接 import/userequests。ai-service/tests/test_stock_analysis.py: 增加 fallback 响应测试,覆盖长异常下model保持可持久化且aiGenerated=false。数据库变更
无 DB 变更;未新增 migration,未改 schema。
配置 / 环境变量变更
无环境变量变更。仅新增 Python runtime 直接依赖
requests==2.32.3。复现 / 验证证据
修复前复现:
/analyze-stock返回 200model_length 262model内容包含HTTPConnectionPool(... Connection refused ...)修复后验证:
uv run --with fastapi==0.115.5 --with pydantic==2.9.2 --with requests==2.32.3 python -m unittest discover -s tests: ✅Ran 1 test ... OK/analyze-stock: ✅200,model: rule-fallback,model_length 13docker build -t finsight-ai-service-test ./ai-service: ✅ build successdocker run --rm finsight-ai-service-test python -c "import requests; from app.main import app; print('ok')": ✅okdocker run --rm -v "$PWD":/workspace -w /workspace/backend maven:3.9.9-eclipse-temurin-17 mvn test: ✅BUILD SUCCESS, tests4, skipped1Testcontainers smoke because the Maven container cannot access/var/run/docker.sock相关 issue
SHO-551