Skip to content

fix: emit valid JSON when --output json falls through to raw#128

Merged
steipete merged 1 commit intosteipete:mainfrom
armanddp:fix/json-output-null-fallback
Mar 28, 2026
Merged

fix: emit valid JSON when --output json falls through to raw#128
steipete merged 1 commit intosteipete:mainfrom
armanddp:fix/json-output-null-fallback

Conversation

@armanddp
Copy link
Copy Markdown

Problem

When --output json is requested and wrapped.json() returns null (e.g. an MCP tool returns content without a json-typed entry), printRaw() is called as the last-resort fallback. This uses util.inspect(depth: 8), which produces output like:

{ content: [ { type: 'text', text: '...' } ] }

This looks like JS object notation but isn't valid JSON — single quotes, no quoting rules, truncation markers. Downstream JSON parsers (e.g. OpenClaw's mcporter bridge) then fail with:

Expected property name or '}' in JSON at position 2

Fix

When the requested format is json, printRaw() now uses JSON.stringify() instead of util.inspect(), guaranteeing the output is always valid JSON. Falls back to JSON.stringify(String(raw)) for non-serializable values (circular references, etc.).

Changes

  • printRaw() accepts an optional requestedFormat parameter
  • When requestedFormat === 'json', uses JSON.stringify instead of inspect
  • Updated existing test expectation to match (string raw now emits "raw-only-string" instead of unquoted)
  • Added new test: object raw fallback emits valid JSON, not util.inspect

Testing

All 419 tests pass (103 test files).

Companion to openclaw/openclaw#54728 (QMD 1.1+ mcporter compatibility).

AI-assisted: Built with Claude (Opus 4.6) via OpenClaw. Fully tested.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2ec8beb463

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/cli/output-utils.ts Outdated
// which breaks downstream parsers (e.g. OpenClaw's mcporter bridge).
if (requestedFormat === 'json') {
try {
console.log(JSON.stringify(raw, null, 2));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle undefined stringify result in JSON raw fallback

When requestedFormat === 'json', printRaw assumes JSON.stringify(raw, null, 2) either returns JSON text or throws, but for top-level undefined, function, or symbol it returns undefined without throwing. This then logs the literal undefined, which is not valid JSON and still breaks downstream parsers in the exact fallback path this patch is trying to harden.

Useful? React with 👍 / 👎.

@armanddp
Copy link
Copy Markdown
Author

Addressed Codex P2 in 730d93c:

undefined stringify result: JSON.stringify(undefined) returns undefined (not a string) without throwing — for top-level undefined, function, or symbol values. The previous code would console.log(undefined), emitting the literal word undefined which isn't valid JSON.

Fix: check typeof serialized === 'string' and fall back to 'null' when stringify returns undefined. Added test case for the undefined raw input.

420/420 tests pass.

@steipete steipete force-pushed the fix/json-output-null-fallback branch from 730d93c to b744c44 Compare March 28, 2026 19:41
@steipete steipete merged commit 914e03f into steipete:main Mar 28, 2026
1 check passed
@steipete
Copy link
Copy Markdown
Owner

Landed via temp rebase onto main.

  • Gate: ./runner pnpm check && ./runner pnpm build && ./runner pnpm test
  • Land commit: b744c44
  • Merge commit: 914e03f

Thanks @armanddp!

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.

2 participants