[Bug] _validate rejects LLM replies wrapped in a markdown code fence; json_object/prompt_only unusable with fence-emitting models

未关闭
#627 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@2030XiaoGe 已经在做这个了。

开始于 2026年11月24日。

  • #628 来自 @2030XiaoGe —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
22/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
ai

调研方向

Start in src/everos/component/llm/openai_provider.py, at _validate and the existing _strip_trailing_commas repair step it calls. Fence stripping (a leading json or and a trailing ```) would be added as an extra repair before validation. Work is already under way in linked PR #628, so check that PR before starting. Done means a fenced JSON reply validates against _EpisodeReply in json_object and prompt_only modes.

由索引模型根据 Issue 内容生成。

描述

Summary

OpenAIProvider._validate rejects LLM replies wrapped in a markdown code fence (json ... ), so non-OpenAI-native models (glm, deepseek, etc.) that habitually wrap JSON in a fence fail validation in json_object / prompt_only mode, returning EXTERNAL_SERVICE_UNAVAILABLE on every /add and /flush.

Reproduce

Model: glm-5.2 (and any model that wraps JSON in ```json fences), via an OpenAI-compatible endpoint.

everos.toml:

[llm]
model = "glm-5.2"
api_key = "..."
base_url = "..."
response_format_mode = "prompt_only"   # or "json_object"
curl -X POST http://localhost:8000/api/v2/memory/add \
  -H "Content-Type: application/json" \
  -d '{"session_id":"s1","messages":[{"sender_id":"A","role":"user","timestamp":1737900000000,"content":"C likes basketball"},{"sender_id":"C","role":"user","timestamp":1737900006000,"content":"yes I do"}]}'
curl -X POST http://localhost:8000/api/v2/memory/flush \
  -H "Content-Type: application/json" -d '{"session_id":"s1"}'

Response:

{"error":{"code":"EXTERNAL_SERVICE_UNAVAILABLE","message":"no usable reply in 2 attempts (model='glm-5.2', response_format_mode='prompt_only'): reply failed validation against _EpisodeReply"}}

Actual LLM reply (raw)

The model returns valid JSON wrapped in a fence:
```json {"title": "...", "content": "..."} ```

Root cause

src/everos/component/llm/openai_provider.py _validate runs response_format.model_validate_json(text) on the full reply text. A reply starting with ```json is not valid JSON, so validation fails. The only repair attempted is _strip_trailing_commas, which does not strip a markdown fence:

def _validate(text: str, response_format: type[BaseModel]) -> str:
    try:
        response_format.model_validate_json(text)
    except ValidationError as exc:
        repaired = _strip_trailing_commas(text)
        if repaired != text and _validates(repaired, response_format):
            return repaired
        raise _AttemptFailedError(
            f"reply failed validation against {response_format.__name__}", ...
        ) from exc
    return text

Why this is a gap (not already covered)

  • #564 / PR #580 fixed trailing commas but did not add fence stripping.
  • #515 / PR #538 removed the old brace-balanced _extract_json_object (which happened to skip fences by starting at the first {) in favor of strict model_validate_json, dropping the implicit fence tolerance.
  • The config docs (default.toml:87) explicitly offer json_object / prompt_only for endpoints that "do not accept a strict schema". In those modes the endpoint is not enforcing a schema, so a model wrapping JSON in a fence is a common, expected behavior — yet _validate cannot parse it.

So a user who follows the documented fallback (prompt_only) for a non-strict endpoint still gets every extraction rejected.

Expected

_validate strips a leading (with optional language tag) and a trailing before validating, the same way the old _extract_json_object implicitly skipped fences by starting at the first {. Or: strip the fence as an additional repair step alongside _strip_trailing_commas.

Environment

  • EverOS 1.7.1 (main, commit e5778ed)
  • everalgo-user-memory 0.10.1
  • LLM: glm-5.2 via OpenAI-compatible endpoint, response_format_mode = "prompt_only"
  • Python 3.12, Linux
主要语言
Python
星标
13.3k
派生
926
平均合并
1 天 10 小时
30 天内合并 PR
57

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

EverMind-AI/EverOS 的其他 Issue

查看 EverMind-AI/EverOS 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。