Skip to content

Add a batch polling example to the async search guide - #55

Open
Toraastrta wants to merge 1 commit into
serpapi:masterfrom
Toraastrta:docs/async-batch-polling
Open

Toraastrta wants to merge 1 commit into
serpapi:masterfrom
Toraastrta:docs/async-batch-polling

Conversation

@Toraastrta

Copy link
Copy Markdown

Summary

Adds a "Poll a batch of searches" section to the async Search Archive guide, and links to it from the README.

Today the guide shows how to submit one async search and poll it. Running several means writing the submit, poll and re-check loop yourself, which is what #53 asks to make easier. The issue offers two options: an SDK helper, or a documented example of the loop. This PR does the second. I left the helper out because its shape (return value, deadline, error policy) is a design call for maintainers; happy to follow up if you want one.

The example:

  • submits every search first with async: True, then polls only the unfinished ones each round;
  • keys results by search ID, so a query listed twice keeps both results;
  • moves searches that fail on SerpApi's side to failed without stopping the others. In the Status and Error Codes examples, a search with the Error status comes back with HTTP 503, which Client raises as serpapi.HTTPError. So the example catches serpapi.HTTPError with status 500 or 503 (the server errors that page lists) around search_archive(), and re-raises everything else (401, 429, other 5xx, connection errors). That page's example is for the Search API, so whether the Search Archive API answers the same way is my reading, not something I could observe. It also keeps a status == "Error" check in case a 200 response ever carries that status;
  • uses the same deadline handling as the single-search example above it (deadline checked before each request, timeout=min(30, remaining), capped sleep).

The text also covers two things I hit while testing: a search can finish with Success and still carry an error ("Google hasn't returned any results for this query."), and passing the original q to search_archive() returns a 400 (the warning from the original thread).

No code changes. The new block runs in the live docs tests and uses three searches per run.

Type

  • Bug fix
  • Feature
  • Refactor / performance
  • Documentation / examples
  • Tests
  • Maintenance / packaging / CI

Related issue

Refs #53. It covers the documented-example option. I didn't use "Closes" because the helper option is still open.

Affected SDK functionality

  • serpapi package version (if known): 1.3.0 (editable install of master at 5c991b6)
  • Python version and operating system: Python 3.13.18, macOS 26.6.2
  • SDK methods or helpers affected: Client.search with async: True, Client.search_archive (docs only)
  • SerpApi engine or endpoint involved (if applicable): Google Search API, Search Archive API
  • Other affected Python versions or environments (confirmed or suspected): the new block compiles on Python 3.8.20; not run there

Testing

  • Human testing: I ran the live checks below with my own SerpApi key.
  • AI-executed testing:
    • python -m pytest tests/test_docs_examples.py tests/test_docs_example_runner.py -k 'not live' -q: 57 passed.
    • python -m sphinx -M html docs docs/_build -W --keep-going: no warnings. I checked the rendered page; no code block scrolls sideways.
    • Live docs test for this page, python -m pytest 'tests/test_docs_examples.py::test_documentation_examples_live[docs/user_guide/async-search-archive.md]' --require-docs-key -q: 1 passed, run again after the final edits.
    • Live probe with a separate script (4 async searches): each went Processing to Success in 2 to 14 seconds; Cached never appeared. search_archive(search_id=..., q="coffee") raised serpapi.HTTPError with status 400 and the error "You are using query q parameter on our Search Archive API...". An async google_maps search with invalid data finished as Success with error "Google hasn't returned any results for this query."
    • Offline run of the new block with the real serpapi.Client and mocked HTTP responses: duplicate queries kept both results; a 503 with status: "Error" moved that search to failed with its message while the others finished; a Success with error printed the reason; a search still Processing on the first rounds finished later. A 500 was handled like the 503; 401, 429, 502 and 504 stopped the loop with serpapi.HTTPError. Against a fake client, a search that never finishes raised TimeoutError at the deadline.
  • CI results, failures, or tests skipped (with reasons): CI not run yet. I couldn't make a live search end with the Error status, since the docs describe it as a proxy timeout or internal error. So the 503 handling is based on the Status and Error Codes page and covered only by the mocked run.

Side note, not changed here: the single-search polling example above has the same status == "Error" check. With a 503 the client raises before that check runs, so the example still stops, just with serpapi.HTTPError instead of its RuntimeError.

Two notes for the docs tests: the runner fails a /searches/ response that carries an error, so the three queries are ones that return results. And in the worst case (every request taking its full timeout), this page's two 120-second deadlines plus three submits can exceed the five-minute page limit. In my four live runs the page took between 1 and 8 seconds. I can shorten the batch deadline if you'd rather keep a hard margin.

AI disclosure

  • Model(s): Claude Opus 5.5
  • Harness/app(s) (e.g., Codex, Claude Code, Cursor): Claude Code
  • Used for (e.g., coding, tests, review, documentation): drafting the example and text, the offline and live check scripts, and a review pass. I reviewed the change and ran the live checks myself.

Evidence

Before: the guide goes from the single-search poll straight to "When to use async searches".

53-docs-before

After: the new section and its notes.

53-docs-after-1-example 53-docs-after-2-notes

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
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