Skip to content

feat: add --fields flag to filter JSON output#53

Open
qtzx06 wants to merge 4 commits intoPolymarket:mainfrom
qtzx06:feat/json-fields-filter
Open

feat: add --fields flag to filter JSON output#53
qtzx06 wants to merge 4 commits intoPolymarket:mainfrom
qtzx06:feat/json-fields-filter

Conversation

@qtzx06
Copy link

@qtzx06 qtzx06 commented Mar 20, 2026

Problem

The CLI's JSON output includes every field from the API response. A single market object has 80+ fields. When agents or scripts consume this output, they're processing (and paying for) data they don't need.

This matters because LLM-based agents pay per token. Feeding an agent 80 fields per market when it only needs question and volumeNum wastes context window and increases cost for every call.

Solution

Adds a global --fields flag that filters JSON output to only the requested keys:

# before: returns 80+ fields per market
polymarket markets list -o json

# after: returns only what you need
polymarket markets list -o json --fields question,volumeNum,slug

The filtering happens at the print_json layer, so it works with every command that produces JSON output — no per-command changes needed.

How it works

  • --fields accepts a comma-separated list of JSON field names
  • Filters top-level keys from objects
  • For arrays, filters each element individually
  • When --fields is not set, output is unchanged (fully backwards compatible)
  • Has no effect on table output

Implementation

Two files changed, 74 lines added:

  • src/main.rs — adds --fields global arg, stores the field list at startup via OnceLock
  • src/output/mod.rsprint_json reads the stored fields and filters before printing; includes filter_fields helper with 4 unit tests

Test plan

  • cargo test — 135 tests pass (82 unit + 49 integration + 4 new)
  • cargo clippy -- -D warnings — clean
  • cargo fmt --check — clean
  • Verified --fields appears in --help output

Note

Medium Risk
Medium risk because it changes CLI argument parsing and request-building for markets list/events list, which can alter query results and JSON output shape for downstream scripts.

Overview
Adds a global --fields flag that filters JSON output only to a specified subset of keys, implemented centrally in output::print_json via a per-invocation field list set from main.

Extends markets list and events list with additional filtering flags (volume/liquidity min/max and start/end date ranges; plus market tag id filtering) and wires them into the respective *Request::builder() calls.

Written by Cursor Bugbot for commit 8ecbdcf. This will update automatically on new commits. Configure here.

qtzx06 added 2 commits March 20, 2026 11:57
…ts list

the SDK supports these query params but the CLI wasn't wiring them
through, forcing users to over-fetch and filter client-side.

closes Polymarket#38
agents and scripts consuming CLI output pay per token. a market
object has 80+ fields but a typical query only needs 2-3 of them.

--fields lets callers specify exactly which keys to keep:

  polymarket markets list -o json --fields question,volumeNum,slug

this filters at the output layer so it works with every command
that produces JSON, without touching any individual command logic.
qtzx06 added 2 commits March 20, 2026 13:59
- swap OnceLock for RwLock so --fields can change between
  interactive shell commands instead of being stuck on first value
- move field extraction into run() so the shell path also picks
  it up (previously only main() extracted fields)
- only route through serde_json::to_value when --fields is active,
  preserving original key order for unfiltered output
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

if let Ok(mut guard) = JSON_FIELDS.write() {
*guard = fields;
}
}
Copy link

Choose a reason for hiding this comment

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

Silent failure in set_json_fields hides broken --fields

Low Severity

set_json_fields silently swallows a RwLock write failure with if let Ok(...), meaning the --fields flag is quietly ignored and the previous filter value persists. Meanwhile, print_json calls .unwrap() on the read lock, which would panic on the same poisoned-lock condition. The writer silently no-ops while the reader crashes — these two call sites need consistent error handling. If the write silently fails, users get unfiltered output with no indication that --fields was ignored.

Additional Locations (1)
Fix in Cursor Fix in Web

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