# yandex-search-cli

> A command-line integration for repeatable research through Yandex web search, cited answers, image search, reverse image search, and query-frequency statistics.

## Contribution and scope

Built with AI coding agents as part of Nolan Vale's independent product and workflow-automation work. Nolan defines requirements, directs implementation, checks results, and iterates. Yandex provides the underlying search, generation, and statistics; this repository connects those services to scripts and agents.

## Install

```bash
uv tool install yandex-search-cli
```

Alternatively, use pip install yandex-search-cli in a suitable Python environment.

## Setup

For Search API access:

```bash
export YANDEX_API_KEY=your-key
export YANDEX_FOLDER_ID=your-folder-id
```

A configuration file at ~/.search-api/config.json with apiKey and folderId is also supported. Keep credentials out of source control. Consult docs/USAGE.md for service-specific setup and credentials, including Wordstat.

## Commands

- `yandex-search <query>` — structured Yandex web search from terminal. Flags: --json, --num-results, --type (ru|com|tr|kk|be|uz), --region, --page, --pages, --site, --period (all|day|2weeks|month), --sort (relevance|time), --order (asc|desc; requires --sort time)
- `yandex-gen [query]` — YandexGPT answer generation with search context and optional dialogue. Flags: --json, --site, --format (pdf|xls|ods|rtf|ppt|odp|swf|odt|odg|doc), --lang (two-letter ISO 639-1), --date, --messages-file FILE (use - for stdin)
- `yandex-image-search <query>` — image search by text query. Flags: --json, --num-results, --type, --region, --page, --site
- `yandex-image-search-by-image (--url URL | --cbir-id ID)` — reverse image search, find pages containing a given image. Flags: --site, --page, --family-mode (none|moderate|strict), --json
- `yandex-wordstat <top|dynamics|regions|regions-tree>` — Yandex Wordstat query-frequency statistics. `top <phrase>`: most popular queries; `dynamics <phrase> --from DATE`: frequency over time; `regions <phrase>`: geographic distribution; `regions-tree`: supported region IDs

## Output reference

- Web search: [{title, url, domain, date, passages[]}].
- Generative search: {message{content, role}, sources[{used, title, url}], isAnswerRejected, fixedMisspellQuery}.
- Image search: [{url, domain, title, thumbnail_url, width, height, page_url, format}].
- Reverse image search: {images[{url, format, width, height, passage, host, pageTitle, pageUrl}], page, id}.
- Wordstat top: {totalCount, results[{phrase, count}], associations[{phrase, count}]}.
- Wordstat dynamics: {results[{date, count, share}]}.
- Wordstat regions: {results[{region, count, share, affinityIndex}]}.
- Wordstat regions-tree: {regions[{id, label, children[]}]}.

## Examples

```bash
yandex-search "document review workflow" --json
yandex-search "query" --site habr.com --json
yandex-search "AI news" --period month --sort time --pages 2 --json
yandex-gen "smart city research" --format pdf --lang en --date ">=20250101" --json
yandex-gen "How do I enable HTTPS?" --messages-file conversation.json --json
yandex-gen "document processing approaches" --json
yandex-search "query" --json | jq -r '.[].url'
yandex-image-search "python logo" --json
yandex-image-search-by-image --url "https://example.com/photo.jpg" --json
yandex-wordstat top "python framework" -n 20 --json
```

## Data and review

Queries and other request inputs are sent to external services. Protect credentials, submit only material you are authorized to share, and verify sources and generated answers before using them in decisions. Provider availability and source quality are not guaranteed by this integration.

## Key files

- README.md: purpose, contribution, setup, and limitations.
- README.ru.md: Russian documentation.
- AGENTS.md: agent integration guidance.
- docs/USAGE.md: usage and service-specific setup.
- src/yandex_cli/client.py: request/response layer.
- src/yandex_cli/parsers.py: response parsing.
- pyproject.toml: dependencies and entry points.

## Links

- Repository: https://github.com/nolan-vale/yandex-search-cli
- Product-builder portfolio: https://github.com/nolan-vale
- PyPI: https://pypi.org/project/yandex-search-cli/
- Search API documentation: https://yandex.cloud/en/docs/search-api/

## Research workflow (0.2.3)

- `yandex-search QUERY --pages 3 -n 10 --json`: collect consecutive pages from --page, deduplicate exact URLs; stop on an empty page. A later-page failure exits nonzero without partial success JSON.
- -n accepts 1–100 per page; total search window is 250 results. Query plus --site must be non-empty and <=400 characters.
- All commands: --timeout SECONDS (default 30, per HTTP request), --version. For Wordstat, put these before the subcommand.
- Web --region is supported for ru/tr and sent at the request root. Image search does not support --region.
- Retrieval retries transient network failures and HTTP 429/500/502/503/504, up to 3 attempts, with bounded backoff/Retry-After. Generative requests are not replayed.
- XML error 15 is no matches; other XML errors exit nonzero. Diagnostics use stderr and redact keys/folder IDs. Invalid arguments exit 2.
- JSON schema unchanged. date is page modification time from modtime, not verified article publication date.
- Verify DOI/authors/year at the primary source. Search discovers links; PDFs are downloaded separately.

## Freshness and dialogue

- Web `--period` maps to the API's root `period`: all=PERIOD_ALL_TIME, day=PERIOD_DAY, 2weeks=PERIOD_2_WEEKS, month=PERIOD_MONTH. Without the flag the field is omitted. The filter is kept on every requested page; the output JSON schema is unchanged.
- `yandex-gen --messages-file FILE` reads a UTF-8 JSON array of `{role, content}` messages; `-` reads stdin. Roles user/assistant and ROLE_USER/ROLE_ASSISTANT are accepted and normalized to API role names.
- With a positional query, history is followed by that new ROLE_USER message. Without a query, the array is the complete request. Keep the previous assistant's `message` in the history to continue the conversation.
- The full request must contain 1–100 messages, with non-empty text content of at most 16384 characters each. Invalid input exits 2 before credentials/network and does not echo message contents.
- History files are input only: the CLI does not create, change or persist them. Response JSON remains the ordinary API response, including message and sources.

## Source filters and sorting

- Generative --format limits source document format and maps to a DOC_FORMAT_* enum; --lang normalizes a two-letter language code to lowercase; --date passes a validated date expression. Each becomes a separate object in the root searchFilters array. With no filters, searchFilters is omitted.
- Date syntax: YYYYMMDD, <YYYYMMDD, <=YYYYMMDD, >YYYYMMDD, >=YYYYMMDD, YYYYMMDD..YYYYMMDD (inclusive), YYYYMM* or YYYY*. Dates must be valid calendar dates and ranges must not be reversed. Quote expressions containing <, > or *. These are Yandex page-addition dates, not verified publication dates.
- Web --sort time sets sortSpec.sortMode=SORT_MODE_BY_TIME. The API defaults to newest page updates first; --order asc/desc explicitly sets SORT_ORDER_ASC/DESC. --order requires --sort time. --sort relevance uses SORT_MODE_BY_RELEVANCE. With neither sorting flag the sortSpec field is omitted.
- Sorting and freshness are kept on every requested page. Source filters work with dialogue and --site. JSON output schemas are unchanged; invalid CLI arguments exit 2 before credentials/network.
