Skip to content

feat: add debug mode - #27

Merged
dariyaanis merged 18 commits into
mainfrom
feat/debug-mode
Nov 19, 2026
Merged

dariyaanis merged 18 commits into
mainfrom
feat/debug-mode

Conversation

@dariyaanis

Copy link
Copy Markdown
Collaborator

Summary

  • add opt-in debug logging to the synchronous and asynchronous clients
  • log sanitized request/response pairs, durations, compact polling progress, failures, and solve summaries
  • mask credentials and proxy fields, abbreviate base64 and long strings, and isolate debug failures from SDK behavior
  • document debug mode and add a runnable Image to Text example using the bundled asset
  • cover success, API/HTTP/network/timeout failures, masking, truncation, summaries, and logger isolation

Validation

  • python -m pytest -q — 133 passed
  • python -m ruff check captcha_solver_api tests examples
  • python -m mypy --no-site-packages --ignore-missing-imports captcha_solver_api
  • python -m build --no-isolation
  • python -m twine check --strict dist/*
  • python -m compileall -q captcha_solver_api examples
  • git diff --check

No integration test that creates a paid CAPTCHA task was run.

@dzmitry-duboyski

Copy link
Copy Markdown
Contributor

Thanks for the PR! The debug mode itself is well thought out - credentials, cookies, proxy data and base64 payloads are masked, solve() gets a summary, and logging failures never affect SDK behavior. pytest, ruff and mypy pass locally. A few things to address before merging.

How to handle this review: each item has an ID. Please reply to every item with the commit that fixes it (or explain why it's not needed). Request re-review once all required (R) items are addressed.

Required

R1. Debug mode only in dedicated debug_mode.py examples (sync and async)

The original request was a dedicated example that demonstrates debug mode, with a name that makes this obvious. The --debug flag in every other example is not needed:

  • revert the changes to the regular examples in examples/sync/ and examples/async/ back to main, except commit a27b9ee (API key placeholder) - keep that one;
  • remove examples/_cli.py;
  • in examples/sync/debug_mode.py, enable debug mode directly with debug=True instead of debug_enabled(). Right now debug output is off when the example is run without flags, so it doesn't demonstrate anything;
  • add examples/async/debug_mode.py - the same example for AsyncCaptchaClient with debug=True;
  • in README.md and examples/README.md, drop the statements that every example accepts --debug and link to both debug_mode.py examples instead.

The same commits also rewrite docstrings and STEP comments across all examples (docs: add workflow guides to examples, docs: align step comments across examples, etc.). That is unrelated to debug mode - please drop it from this PR. If you think those changes are worth keeping, open a separate PR for them.

R2. Debug mode takes over the logger - captcha_solver_api/_debug.py:81-83, :86

With debug=True the SDK attaches its own StreamHandler to captcha_solver_api.<Client> and sets propagate = False. As a result:

  • if the application has configured logging, SDK logs never reach it and only go to stderr;
  • propagate = False stays set for the rest of the process, including clients created later with debug=False;
  • logs can't be enabled via standard logging (logging.getLogger("captcha_solver_api").setLevel(logging.DEBUG)), because emit() only checks the flag.

Suggest leaving propagate alone and attaching the default handler only when the application has no handlers configured (e.g. not logging.getLogger().hasHandlers()). Please add a test that SDK logs reach a configured root handler.

Optional (nit)

N1. Hardcoded httpTimeout=30s - captcha_solver_api/_debug.py:96

This duplicates timeout=30 in client.py:141 and async_client.py:151. Consider a shared constant passed to initialized().

N2. Redundant __cause__ assignment - client.py:160, 185, async_client.py:170, 195

raise ... from exc already sets __cause__. If it's needed so that _error_details() can see the cause before the raise, add a short comment; otherwise drop it.

N3. debug vs _debug naming - client.py:108, async_client.py:118

self.debug is a bool and self._debug is the logger - easy to mix up. Consider renaming the logger to something like self._debug_log.

Note

This PR conflicts with #26 (Kaleidoscope) in client.py and async_client.py. I suggest merging #26 first and then rebasing this one on top. When resolving the conflict, keep the isinstance(solution, (dict, str)) check from #26 inside the new try block in solve(), otherwise Kaleidoscope tasks will fail with NetworkError.

@dariyaanis

Copy link
Copy Markdown
Collaborator Author

Addressed the review in commit f63dc2c:\n\n- R1: restored regular sync/async examples to main while preserving the API-key placeholder cleanup, removed examples/_cli.py, enabled debug=True directly in the sync example, added examples/async/debug_mode.py, and updated both READMEs to link to the dedicated examples.\n- R2: stopped changing propagate, only installs the fallback handler when root logging has no handlers, allows standard logging to enable SDK events, and added coverage for configured root handlers and flag-free logging.\n- N1: introduced shared HTTP_TIMEOUT_SECONDS and use it for requests and initialization logs.\n- N2: kept the pre-raise cause assignments and documented why they are needed before the debug event is emitted.\n- N3: renamed the logger attribute to _debug_log.\n\nValidation: python -m ruff check ., python -m pytest -q -p no:cacheprovider (137 passed), python -m build, and python -m twine check --strict dist/* all pass.\n\n@dzmitry-duboyski Please re-review when you have a chance.

@dariyaanis

Copy link
Copy Markdown
Collaborator Author

Follow-up audit: commit 5093e18 updates the R2 coverage to use the exact package logger from the review (logging.getLogger(captcha_solver_api).setLevel(logging.DEBUG)) rather than the client-specific child logger. Ruff and all 137 tests still pass.

@dariyaanis

Copy link
Copy Markdown
Collaborator Author

Rebased PR #27 onto main after #26 was merged. The conflict resolution keeps Kaleidoscope support in both clients: solution accepts dict or a non-empty str, while an empty string still raises NetworkError. Validation after rebase: Ruff passed, 145 tests passed, package build passed, and Twine strict checks passed. Current head: 252a6e1.

@dariyaanis
dariyaanis merged commit 63d710a into main Nov 19, 2026
11 checks passed
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