Skip to content

fix(search): normalize non-array search responses to prevent results.map crash#1

Open
fkysly wants to merge 1 commit intonumman-ali:mainfrom
fkysly:main
Open

fix(search): normalize non-array search responses to prevent results.map crash#1
fkysly wants to merge 1 commit intonumman-ali:mainfrom
fkysly:main

Conversation

@fkysly
Copy link
Copy Markdown

@fkysly fkysly commented Feb 7, 2026

Summary

Fixes a crash in zai-cli search when the MCP search response is not an array.

Error observed:

results.map is not a function

What changed

In packages/zai-cli/src/commands/search.ts:

  • Add normalizeSearchResults(input) helper to accept multiple response shapes:
    • []
    • { results: [] }
    • { data: [] }
    • { items: [] }
    • single result object fallback
  • Map over normalized results instead of assuming results is always an array
  • Keep output schema unchanged (rank, title, url, summary, source, date)

Why

Some queries (notably some non-English queries) can return a payload shape that differs from the expected array, which previously caused the command to crash. This patch makes parsing resilient and degrades gracefully (empty list instead of process failure).

Validation

  • npm run build passes
  • Reproduced failing query before patch
  • After patch, same query no longer crashes and returns structured output (or empty array when no results)

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.

1 participant