Skip to content

Add option to return raw JSON from query in ClaudeCode SDK#1

Draft
JokerRun wants to merge 1 commit intomainfrom
terragon/feature-return-raw-json
Draft

Add option to return raw JSON from query in ClaudeCode SDK#1
JokerRun wants to merge 1 commit intomainfrom
terragon/feature-return-raw-json

Conversation

@JokerRun
Copy link
Owner

Summary

  • Introduces a new return_raw_json option in ClaudeCodeOptions to allow returning raw JSON data from queries instead of parsed messages.
  • Adds a new method process_query_raw in InternalClient to handle raw JSON streaming.
  • Updates the query function to conditionally yield either parsed messages or raw JSON based on the new option.
  • Includes comprehensive tests and validation scripts to verify the new functionality and maintain code quality.

Changes

Core Functionality

  • New Option: Added return_raw_json: bool = False to ClaudeCodeOptions.
  • Client Update: Added process_query_raw async generator method in InternalClient to stream raw JSON data.
  • Query Function: Modified return type to AsyncIterator[Union[Message, dict[str, Any]]] and added logic to yield raw JSON if return_raw_json is enabled.

Testing & Validation

  • Test Script: Added test_raw_json.py to test both raw JSON and normal parsed message modes.
  • Validation Scripts: Added validate_code.py and validate_implementation.py to verify imports, types, syntax, and implementation correctness.

Environment

  • Added Python virtual environment files under test_env for consistent testing environment.

Test plan

  • Run test_raw_json.py to verify that queries return raw JSON data when enabled and parsed messages otherwise.
  • Run validate_code.py and validate_implementation.py to ensure code correctness and adherence to new feature requirements.
  • Manual inspection and testing of the query function with both modes.

This enhancement provides users with flexibility to access raw JSON responses directly, facilitating advanced use cases and debugging.

🌿 Generated by Terry


ℹ️ Tag @terragon-labs to ask questions and address PR feedback

📎 Task: https://www.terragonlabs.com/task/e150d50e-edb1-4c1c-bd5a-f9c50c4dea04

- Added `return_raw_json` boolean option to ClaudeCodeOptions to control output format.
- Modified `query` function to yield raw JSON dicts when `return_raw_json` is true.
- Added `process_query_raw` method in InternalClient to stream raw JSON data.
- Updated type hints to reflect possible return of raw JSON or parsed messages.
- Added tests and validation scripts for the new raw JSON feature.

This feature enables users to receive unparsed JSON data directly from the query, providing more flexibility for downstream processing.

Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
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