Repository navigation
Add a batch polling example to the async search guide - #55
Open
Toraastrta wants to merge 1 commit into
Open
Toraastrta wants to merge 1 commit into
Toraastrta wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
async: True, then polls only the unfinished ones each round;failedwithout stopping the others. In the Status and Error Codes examples, a search with theErrorstatus comes back with HTTP 503, whichClientraises asserpapi.HTTPError. So the example catchesserpapi.HTTPErrorwith status 500 or 503 (the server errors that page lists) aroundsearch_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 astatus == "Error"check in case a 200 response ever carries that status;timeout=min(30, remaining), capped sleep).The text also covers two things I hit while testing: a search can finish with
Successand still carry anerror("Google hasn't returned any results for this query."), and passing the originalqtosearch_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
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
serpapipackage version (if known): 1.3.0 (editable install ofmasterat 5c991b6)Client.searchwithasync: True,Client.search_archive(docs only)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.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.ProcessingtoSuccessin 2 to 14 seconds;Cachednever appeared.search_archive(search_id=..., q="coffee")raisedserpapi.HTTPErrorwith status 400 and the error "You are using queryqparameter on our Search Archive API...". An asyncgoogle_mapssearch with invaliddatafinished asSuccesswitherror"Google hasn't returned any results for this query."serpapi.Clientand mocked HTTP responses: duplicate queries kept both results; a 503 withstatus: "Error"moved that search tofailedwith its message while the others finished; aSuccesswitherrorprinted the reason; a search stillProcessingon the first rounds finished later. A 500 was handled like the 503; 401, 429, 502 and 504 stopped the loop withserpapi.HTTPError. Against a fake client, a search that never finishes raisedTimeoutErrorat the deadline.Errorstatus, 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 withserpapi.HTTPErrorinstead of itsRuntimeError.Two notes for the docs tests: the runner fails a
/searches/response that carries anerror, 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
Evidence
Before: the guide goes from the single-search poll straight to "When to use async searches".
After: the new section and its notes.
🤖 Generated with Claude Code