Skip to content

Conversation

@DanielDerefaka
Copy link

Summary

This PR introduces a centralized json_utils module to standardize JSON output across all btcli commands, addressing the inconsistencies noted in issue #635.

Problem

  • JSON output exists in 20+ command files but is inconsistent
  • 3 different output methods used (json.dumps(), print_json(), hardcoded strings)
  • Field names vary (error vs err_msg vs message)
  • No centralized schema definitions
  • Some success responses include "error": "" instead of omitting the field

Solution

Add bittensor_cli/src/bittensor/json_utils.py with standardized helpers:

  • json_response() - Base response formatter
  • json_success() - Success response helper
  • json_error() - Error response helper
  • json_transaction() - Transaction response helper
  • serialize_balance() - Consistent Balance serialization

Standard Response Format

{
    "success": true,
    "data": { ... }
}
{
    "success": false,
    "error": "Error message"
}

Changes

  • New: bittensor_cli/src/bittensor/json_utils.py - Standardized JSON utilities
  • Updated: bittensor_cli/src/commands/wallets.py - Sample migration (3 functions)
  • New: tests/unit_tests/test_json_utils.py - 21 comprehensive unit tests

Test Plan

  • All 21 new unit tests pass
  • Existing unit tests unaffected (4 pre-existing failures in swap_hotkey tests)
  • Syntax validation passes

Migration Strategy

This PR establishes the foundation. Additional commands can be migrated incrementally in follow-up PRs to minimize review burden and risk.

Closes #635

Copy link
Contributor

@thewhaleking thewhaleking left a comment

Choose a reason for hiding this comment

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

I like the idea, but you're neglecting a few key features: json_console is used widely, so this should be applied to everything, and all transaction commands include an extrinsic identifier.

@DanielDerefaka
Copy link
Author

Thanks for the feedback! I'll expand this to cover all json_console usages and add extrinsic_identifier to all transaction commands. Will push updates shortly.

DanielDerefaka added a commit to DanielDerefaka/btcli that referenced this pull request Dec 22, 2025
…identifier

- Add TransactionResult and MultiTransactionResult classes for consistent
  transaction responses across all commands
- Add print_transaction_response() helper function
- Update schema to use {success, message, extrinsic_identifier} format
- Migrate wallets.py: transfer, swap_hotkey, set_id
- Migrate sudo.py: trim command
- Migrate stake/add.py and stake/remove.py
- Migrate liquidity.py: add_liquidity, remove_liquidity, modify_liquidity
- Update tests for new transaction response utilities (25 tests passing)

This addresses feedback from @thewhaleking on PR opentensor#781 to apply standardized
JSON output to all json_console usages with extrinsic_identifier support.

Closes opentensor#635
DanielDerefaka added a commit to DanielDerefaka/btcli that referenced this pull request Dec 22, 2025
…identifier

- Add TransactionResult and MultiTransactionResult classes for consistent
  transaction responses across all commands
- Add print_transaction_response() helper function
- Update schema to use {success, message, extrinsic_identifier} format
- Migrate wallets.py: transfer, swap_hotkey, set_id
- Migrate sudo.py: trim command
- Migrate stake/add.py and stake/remove.py
- Migrate liquidity.py: add_liquidity, remove_liquidity, modify_liquidity
- Update tests for new transaction response utilities (25 tests passing)

This addresses feedback from @thewhaleking on PR opentensor#781 to apply standardized
JSON output to all json_console usages with extrinsic_identifier support.

Closes opentensor#635
@DanielDerefaka DanielDerefaka force-pushed the feat/improve-json-output branch from 92b056d to 0cbe07e Compare December 22, 2025 15:35
@thewhaleking
Copy link
Contributor

Also this is opened against main. It should be opened against staging

@DanielDerefaka DanielDerefaka changed the base branch from main to staging December 22, 2025 16:10
@DanielDerefaka
Copy link
Author

Done, changed the base branch to staging.

This PR introduces a centralized json_utils module to standardize JSON output
across all btcli commands, addressing the inconsistencies noted in issue opentensor#635.

Changes:
- Add `bittensor_cli/src/bittensor/json_utils.py` with standardized helpers:
  - `json_response()` - Base response formatter
  - `json_success()` - Success response helper
  - `json_error()` - Error response helper
  - `json_transaction()` - Transaction response helper
  - `serialize_balance()` - Consistent Balance serialization

- Update `wallets.py` with sample usage (3 functions updated as POC)

- Add comprehensive unit tests (21 tests) for schema compliance

Standard Response Format:
```json
{
    "success": bool,
    "data": {...},     // Optional: command-specific data
    "error": str       // Optional: error message
}
```

This establishes the foundation for full migration of all commands to use
consistent JSON schemas. Additional commands can be migrated incrementally.

Closes opentensor#635
…identifier

- Add TransactionResult and MultiTransactionResult classes for consistent
  transaction responses across all commands
- Add print_transaction_response() helper function
- Update schema to use {success, message, extrinsic_identifier} format
- Migrate wallets.py: transfer, swap_hotkey, set_id
- Migrate sudo.py: trim command
- Migrate stake/add.py and stake/remove.py
- Migrate liquidity.py: add_liquidity, remove_liquidity, modify_liquidity
- Update tests for new transaction response utilities (25 tests passing)

This addresses feedback from @thewhaleking on PR opentensor#781 to apply standardized
JSON output to all json_console usages with extrinsic_identifier support.

Closes opentensor#635
@DanielDerefaka
Copy link
Author

@thewhaleking I hope my update will be helpful for you, could you please review my PR?

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.

Improve JSON output

2 participants