cuery.search#
Async helpers for grounded / live web search via OpenAI, Google Gemini and xAI Grok.
Provides a convenience search function that executes multiple web searches
concurrently (rate limited via a semaphore) and preserves input order. Logic
mirrors cuery.call.gather_calls() but is simplified for plain string prompts.
Currently supported providers (client argument):
"openai"– Uses the OpenAI Responses API with theweb_search_previewtool."gemini"– Uses the Google Gemini (google-genai) API with Google Search grounding(
google_searchfor Gemini 2.x,google_search_retrievalfor 1.5 models).
Both return a SearchResult with extracted plain text plus a list of
Reference objects (title + URL) when available. If parsing fails a ValueError
is raised (no silent fallbacks here – upstream caller can decide how to handle).
Attributes#
Classes#
Single search reference with title and URL. |
|
Search result with extracted text and references. |
Functions#
|
|
|
Convert Markdown text to plain text, stripping all formatting. |
|
Resolve a URL redirect to its final destination URL. |
|
Convert a raw web search response into a |
|
Convert a Gemini grounded search raw response into |
|
Convert an xAI Grok Live Search chat completion into |
|
Call OpenAI Responses API with Web Search enabled (async). |
|
|
|
Call Gemini with Google Search grounding (supports Gemini > 2.0 only). |
|
Call xAI Grok with Live Search (async). |
|
DeepSeek doesn't support search yet, so we simply execute the prompt as-is. |
|
Validate if the given client and model are supported. |
|
Perform a web search using the specified model and return structured response. |
|
Simplified gather mirroring |
Module Contents#
- cuery.search.OAIResponse#
- cuery.search.VALID_MODELS#
- cuery.search.unmark_element(element, stream=None)#
- cuery.search.__md#
- cuery.search.unmark(text)#
Convert Markdown text to plain text, stripping all formatting.
- class cuery.search.Source(/, **data)#
Bases:
cuery.response.ResponseSingle search reference with title and URL.
- Parameters:
data (Any)
- title: str#
- url: str#
- property domain: str | None#
Extract domain from URL.
- Return type:
str | None
- class cuery.search.SearchResult(/, **data)#
Bases:
cuery.response.ResponseSearch result with extracted text and references.
- Parameters:
data (Any)
- answer: str#
- cuery.search.resolve_redirect(redirect_url, timeout=2)#
Resolve a URL redirect to its final destination URL.
Gemini grounding chunk URIs are things like: https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQFXCboQq0LRg6hUOP…
- Parameters:
redirect_url (str)
timeout (int)
- Return type:
str
- cuery.search.validate_openai(response, plain=False)#
Convert a raw web search response into a
SearchResultinstance.- Parameters:
plain (bool)
- Return type:
- cuery.search.validate_gemini(response, plain=False)#
Convert a Gemini grounded search raw response into
SearchResult.- Parameters:
response (pydantic.BaseModel)
plain (bool)
- Return type:
- cuery.search.validate_xai(response, plain=False)#
Convert an xAI Grok Live Search chat completion into
SearchResult.Citations (if
return_citationsenabled – default true) are exposed as a list of URL strings onresponse.citations. We map each into aReferenceusing the URL as both title and URL (title data not currently provided).- Parameters:
plain (bool)
- Return type:
- async cuery.search.query_openai(prompt, country=None, city=None, context_size='medium', reasoning_effort='low', model='gpt-4.1-mini', use_search=True, validate=True, response_format=None)#
Call OpenAI Responses API with Web Search enabled (async).
API Docs: - https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses
- Parameters:
prompt (str)
country (str | None)
city (str | None)
context_size (Literal['low', 'medium', 'high'] | str)
reasoning_effort (Literal['low', 'medium', 'high'] | str)
model (str)
use_search (bool)
validate (bool)
response_format (cuery.response.Response | None)
- Return type:
SearchResult | OAIResponse
- cuery.search.coords(country)#
- Parameters:
country (str)
- Return type:
google.genai.types.LatLng | None
- async cuery.search.query_gemini(prompt, model='gemini-2.0-flash', country=None, language=None, thinking_budget=-1, use_search=True, validate=True)#
Call Gemini with Google Search grounding (supports Gemini > 2.0 only).
API Docs: - https://ai.google.dev/gemini-api/docs/google-search - https://ai.google.dev/gemini-api/docs/thinking
- Parameters:
prompt (str)
model (str)
country (str | None)
language (str | None)
thinking_budget (int)
use_search (bool)
validate (bool)
- Return type:
SearchResult | google.genai.types.GenerateContentResponse
- async cuery.search.query_xai(prompt, model='grok-4', timeout=None, mode='on', max_search_results=15, sources=None, country=None, use_search=True, validate=True)#
Call xAI Grok with Live Search (async).
API Docs: - https://docs.x.ai/docs/guides/live-search
- Parameters:
prompt (str)
model (str)
timeout (int | None)
mode (Literal['auto', 'on', 'off'])
max_search_results (int)
sources (list[Literal['web', 'news', 'x']] | None)
country (str | None)
use_search (bool)
validate (bool)
- Return type:
SearchResult | xai_sdk.chat.Response
- async cuery.search.query_deepseek(prompt, model='deepseek-chat', validate=True, use_search=True, **kwds)#
DeepSeek doesn’t support search yet, so we simply execute the prompt as-is.
- Parameters:
prompt (str)
model (str)
validate (bool)
use_search (bool)
- Return type:
Any
- cuery.search.LLMS#
- cuery.search.validate_model(client, model)#
Validate if the given client and model are supported.
- Parameters:
client (str)
model (str)
- Return type:
None
- cuery.search.FORMATTED_SEARCH_PROMPT = ''#
- async cuery.search.search_with_format(prompt, model, response_format, **kwds)#
Perform a web search using the specified model and return structured response.
- Parameters:
prompt (str)
model (str)
response_format (cuery.response.Response)
- Return type:
- cuery.search.SUPPORT_COUNTRY = ['openai', 'xai', 'google']#
- async cuery.search.gather(prompts, model='openai/gpt-4.1-mini', use_search=True, country=None, validate=True, policies=None, execute=True, **kwds)#
Simplified gather mirroring
hasdata.gatherusingall_with_policies.Creates one coroutine per prompt for the selected model/provider with optional policies (timeout, retries, semaphore, fallback, progress). When
executeis False the wrapped coroutine objects are returned for the caller to schedule.- Parameters:
prompts (str | list[str])
model (str)
use_search (bool)
country (str | None)
validate (bool)
policies (dict[str, Any] | None)
execute (bool)
- Return type:
list[collections.abc.Coroutine] | collections.abc.Coroutine | cuery.response.ResponseSet | SearchResult