Skip to content

fix: strip markdown JSON code blocks in OpenAI adapter responseFormat#55

Merged
best merged 1 commit into
masterfrom
fix/strip-json-code-blocks
Apr 3, 2026
Merged

fix: strip markdown JSON code blocks in OpenAI adapter responseFormat#55
best merged 1 commit into
masterfrom
fix/strip-json-code-blocks

Conversation

@best
Copy link
Copy Markdown
Contributor

@best best commented Apr 3, 2026

Closes #54

Problem

When using responseFormat: { type: 'json' }, many LLMs (especially OpenAI-compatible endpoints like MiniMax) return JSON wrapped in markdown code fences:

```json
{"name": "Alice", "age": 30}

This causes `JSON.parse()` failures in downstream consumers.

## Solution

Added `stripJsonCodeBlock()` utility to `openai-adapter.ts` that detects and removes markdown code fences from responses when `responseFormat.type === 'json'`.

### Changes
- `packages/ai/src/adapters/openai-adapter.ts` — Add `stripJsonCodeBlock()`, call it in `doGenerate()` after `stripThinkTags()`
- `packages/ai/src/index.ts` — Export `stripJsonCodeBlock` from `@openlinkos/ai`
- `packages/ai/__tests__/strip-json-code-blocks.test.ts` — 8 unit tests (new file)

### Verification
- All 1033 tests pass (including 8 new stripJsonCodeBlock tests)
- No existing tests broken
- Only affects responses when `responseFormat.type === 'json'`

Closes #54

When responseFormat.type is 'json', many LLMs return JSON wrapped
in markdown code fences (\\\). This causes
JSON.parse failures in consumers.

Added stripJsonCodeBlock() utility that detects and removes the
fencing, called in doGenerate() when responseFormat is set to json.

Changes:
- Add stripJsonCodeBlock() to openai-adapter.ts
- Call it in doGenerate() when responseFormat.type === 'json'
- Export stripJsonCodeBlock from @openlinkos/ai index
- Add 8 unit tests for stripJsonCodeBlock

All 1033 tests pass.
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Code Review

Automated review for PR #55informational only, does not block merge.

Checklist

Item Notes
Type Safety No issues detected
Error Handling No issues detected
Test Coverage 1 test file(s) updated
API Consistency 1 new export(s) added
Breaking Changes No breaking changes detected

Stats

  • 📁 Files changed: 3 (3 TypeScript)
  • ➕ Lines added: 79
  • 🧪 Test files updated: 1

All checks passed — looks good! 🎉

Powered by openlinkos/agent · AI PR Review workflow

@best best merged commit e55f600 into master Apr 3, 2026
5 checks passed
@best
Copy link
Copy Markdown
Contributor Author

best commented Apr 3, 2026

🔍 代码审查 — PR #55

关联 Issue: #54 — responseFormat JSON 返回 markdown 包裹导致解析失败

方案: stripJsonCodeBlock() 自动剥离 markdown code fence


✅ 审查通过

  • 方案精准:仅在 responseFormat.type === "json" 时触发
  • 执行顺序正确:stripThinkTags() 之后调用
  • 正则健壮:lazy match + 首尾锚定,仅整段包裹才剥离
  • 测试覆盖全:8 个用例,含边界和负面测试
  • 改动最小化:3 文件,核心逻辑约 20 行

注意事项(不阻塞):

  • 当前仅覆盖非流式 doGenerate() 路径,流式场景如需支持可后续扩展

CI 全绿: Build ✅ | Test ✅ | AI Review ✅ | Release check ✅

已执行 squash merge。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] OpenAI 兼容性测试失败 - JSON 解析错误

1 participant