specstream-client 1.3.1__tar.gz → 1.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {specstream_client-1.3.1 → specstream_client-1.3.3}/PKG-INFO +62 -2
  2. {specstream_client-1.3.1 → specstream_client-1.3.3}/README.md +61 -1
  3. {specstream_client-1.3.1 → specstream_client-1.3.3}/pyproject.toml +1 -1
  4. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/__init__.py +1 -1
  5. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/client.py +45 -7
  6. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/lifecycle_commands.py +5 -5
  7. {specstream_client-1.3.1 → specstream_client-1.3.3}/tests/test_http_boundary.py +9 -1
  8. {specstream_client-1.3.1 → specstream_client-1.3.3}/.gitignore +0 -0
  9. {specstream_client-1.3.1 → specstream_client-1.3.3}/LICENSE +0 -0
  10. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/__main__.py +0 -0
  11. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/authorization.py +0 -0
  12. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/cli.py +0 -0
  13. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/families.py +0 -0
  14. {specstream_client-1.3.1 → specstream_client-1.3.3}/specstream_client/py.typed +0 -0
  15. {specstream_client-1.3.1 → specstream_client-1.3.3}/tests/__init__.py +0 -0
  16. {specstream_client-1.3.1 → specstream_client-1.3.3}/tests/test_cli.py +0 -0
  17. {specstream_client-1.3.1 → specstream_client-1.3.3}/tests/test_client.py +0 -0
  18. {specstream_client-1.3.1 → specstream_client-1.3.3}/tests/test_convergence.py +0 -0
  19. {specstream_client-1.3.1 → specstream_client-1.3.3}/tests/test_m16_exports.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: specstream-client
3
- Version: 1.3.1
3
+ Version: 1.3.3
4
4
  Summary: Python client for building lead lists and company lists from the live web. Every match comes with its source, priced before it runs.
5
5
  Project-URL: Homepage, https://specstream.ai
6
6
  Project-URL: Documentation, https://specstream.ai/docs/sdk
@@ -50,6 +50,66 @@ Create an API key under **Agents & API** in your SpecStream workspace and keep i
50
50
  export SPECSTREAM_API_KEY="ss_live_..."
51
51
  ```
52
52
 
53
+ ## Search the web
54
+
55
+ `web_search` calls the [Search API](https://specstream.ai/docs/search-api), `GET /v1/web-search`, at $5 per 1,000 searches of up to 10 results.
56
+
57
+ ```python
58
+ import os
59
+ from specstream_client import Client
60
+
61
+ client = Client(os.environ["SPECSTREAM_API_KEY"])
62
+
63
+ search = client.web_search(q="What is the population of Japan?")
64
+ for source in search["sources"][:1]:
65
+ print(source["title"], source["url"], source["snippet"])
66
+ ```
67
+
68
+ ## Extract pages
69
+
70
+ `extract_pages` calls the [Extract API](https://specstream.ai/docs/extract-api), `POST /v1/extract`, at $0.80 per 1,000 pages, and a page with no text is not charged.
71
+
72
+ ```python
73
+ import os
74
+ from specstream_client import Client
75
+
76
+ client = Client(os.environ["SPECSTREAM_API_KEY"])
77
+
78
+ extract = client.extract_pages({"urls": ["https://example.com"]})
79
+ for page in extract["results"][:1]:
80
+ print(page["title"], page["url"], page["text"])
81
+ ```
82
+
83
+ ## Drop-in for the API you already call
84
+
85
+ SpecStream answers calls written for these search APIs at its own address, in each API's own reply shape. To switch, change the base address and the key. Each search call is one Search API web search on your SpecStream key: $0.005 for up to 10 results ($5 per 1,000 searches), and each 10 results more in the same call add $0.006.
86
+
87
+ | API you call | Base address you have | SpecStream address | Where the SpecStream key goes |
88
+ | --- | --- | --- | --- |
89
+ | Tavily | `https://api.tavily.com` | `https://api.specstream.ai/tavily` | Bearer token or `api_key` |
90
+ | Exa | `https://api.exa.ai` | `https://api.specstream.ai/exa` | `x-api-key` or Bearer token |
91
+ | Brave Search API | `https://api.search.brave.com` | `https://api.specstream.ai` | `X-Subscription-Token` |
92
+ | Serper | `https://google.serper.dev` | `https://api.specstream.ai/serper` | `X-API-KEY` |
93
+ | Bing Web Search API v7 | `https://api.bing.microsoft.com/v7.0/search` | `https://api.specstream.ai/v7.0/search` | `Ocp-Apim-Subscription-Key` |
94
+ | Google Custom Search JSON API | `https://www.googleapis.com` or `https://customsearch.googleapis.com` | `https://api.specstream.ai` | `key=` (`cx` is accepted and not used) |
95
+ | SearXNG (JSON) | your instance address | `https://api.specstream.ai/searxng/<key>` | in the address, `key=`, `X-API-Key` or Bearer token |
96
+ | Firecrawl v2 | `https://api.firecrawl.dev` | `https://api.specstream.ai/firecrawl` | Bearer token |
97
+
98
+ The official Tavily and Exa Python SDKs take the address as an argument:
99
+
100
+ ```python
101
+ import os
102
+ from tavily import TavilyClient
103
+ from exa_py import Exa
104
+
105
+ tavily = TavilyClient(api_key=os.environ["SPECSTREAM_API_KEY"], api_base_url="https://api.specstream.ai/tavily")
106
+ exa = Exa(api_key=os.environ["SPECSTREAM_API_KEY"], base_url="https://api.specstream.ai/exa")
107
+ ```
108
+
109
+ - A Brave call that sends no `count` is priced as 20 results ($0.011). Send `count=10` for $0.005.
110
+ - A Firecrawl `/v2/scrape` call is one Extract API page at $0.0008. The Firecrawl JavaScript SDK works with this address; the Firecrawl Python SDK does not yet.
111
+ - Each route's request and reply fields: [specstream.ai/agents.md](https://specstream.ai/agents.md). Create a key under **Agents & API** at [specstream.ai/workspace](https://specstream.ai/workspace). New accounts start with $10 of credit, no card.
112
+
53
113
  ## Read a request, get the price
54
114
 
55
115
  ```python
@@ -113,7 +173,7 @@ specstream rows --find-id FIND_ID
113
173
 
114
174
  ## Pricing
115
175
 
116
- New accounts start with $10 of credit for 30 days, no card. A delivered result costs 5 cents, 15 cents or $1 depending on how many facts it carries. No result, no charge. Rechecks cost half. People and agents share one balance. See [pricing](https://specstream.ai/pricing).
176
+ New accounts start with $10 of credit, no card. A delivered result costs 5 cents, 15 cents or $1 depending on how many facts it carries. No result, no charge. Rechecks cost half. People and agents share one balance. See [pricing](https://specstream.ai/pricing).
117
177
 
118
178
  ## Links
119
179
 
@@ -23,6 +23,66 @@ Create an API key under **Agents & API** in your SpecStream workspace and keep i
23
23
  export SPECSTREAM_API_KEY="ss_live_..."
24
24
  ```
25
25
 
26
+ ## Search the web
27
+
28
+ `web_search` calls the [Search API](https://specstream.ai/docs/search-api), `GET /v1/web-search`, at $5 per 1,000 searches of up to 10 results.
29
+
30
+ ```python
31
+ import os
32
+ from specstream_client import Client
33
+
34
+ client = Client(os.environ["SPECSTREAM_API_KEY"])
35
+
36
+ search = client.web_search(q="What is the population of Japan?")
37
+ for source in search["sources"][:1]:
38
+ print(source["title"], source["url"], source["snippet"])
39
+ ```
40
+
41
+ ## Extract pages
42
+
43
+ `extract_pages` calls the [Extract API](https://specstream.ai/docs/extract-api), `POST /v1/extract`, at $0.80 per 1,000 pages, and a page with no text is not charged.
44
+
45
+ ```python
46
+ import os
47
+ from specstream_client import Client
48
+
49
+ client = Client(os.environ["SPECSTREAM_API_KEY"])
50
+
51
+ extract = client.extract_pages({"urls": ["https://example.com"]})
52
+ for page in extract["results"][:1]:
53
+ print(page["title"], page["url"], page["text"])
54
+ ```
55
+
56
+ ## Drop-in for the API you already call
57
+
58
+ SpecStream answers calls written for these search APIs at its own address, in each API's own reply shape. To switch, change the base address and the key. Each search call is one Search API web search on your SpecStream key: $0.005 for up to 10 results ($5 per 1,000 searches), and each 10 results more in the same call add $0.006.
59
+
60
+ | API you call | Base address you have | SpecStream address | Where the SpecStream key goes |
61
+ | --- | --- | --- | --- |
62
+ | Tavily | `https://api.tavily.com` | `https://api.specstream.ai/tavily` | Bearer token or `api_key` |
63
+ | Exa | `https://api.exa.ai` | `https://api.specstream.ai/exa` | `x-api-key` or Bearer token |
64
+ | Brave Search API | `https://api.search.brave.com` | `https://api.specstream.ai` | `X-Subscription-Token` |
65
+ | Serper | `https://google.serper.dev` | `https://api.specstream.ai/serper` | `X-API-KEY` |
66
+ | Bing Web Search API v7 | `https://api.bing.microsoft.com/v7.0/search` | `https://api.specstream.ai/v7.0/search` | `Ocp-Apim-Subscription-Key` |
67
+ | Google Custom Search JSON API | `https://www.googleapis.com` or `https://customsearch.googleapis.com` | `https://api.specstream.ai` | `key=` (`cx` is accepted and not used) |
68
+ | SearXNG (JSON) | your instance address | `https://api.specstream.ai/searxng/<key>` | in the address, `key=`, `X-API-Key` or Bearer token |
69
+ | Firecrawl v2 | `https://api.firecrawl.dev` | `https://api.specstream.ai/firecrawl` | Bearer token |
70
+
71
+ The official Tavily and Exa Python SDKs take the address as an argument:
72
+
73
+ ```python
74
+ import os
75
+ from tavily import TavilyClient
76
+ from exa_py import Exa
77
+
78
+ tavily = TavilyClient(api_key=os.environ["SPECSTREAM_API_KEY"], api_base_url="https://api.specstream.ai/tavily")
79
+ exa = Exa(api_key=os.environ["SPECSTREAM_API_KEY"], base_url="https://api.specstream.ai/exa")
80
+ ```
81
+
82
+ - A Brave call that sends no `count` is priced as 20 results ($0.011). Send `count=10` for $0.005.
83
+ - A Firecrawl `/v2/scrape` call is one Extract API page at $0.0008. The Firecrawl JavaScript SDK works with this address; the Firecrawl Python SDK does not yet.
84
+ - Each route's request and reply fields: [specstream.ai/agents.md](https://specstream.ai/agents.md). Create a key under **Agents & API** at [specstream.ai/workspace](https://specstream.ai/workspace). New accounts start with $10 of credit, no card.
85
+
26
86
  ## Read a request, get the price
27
87
 
28
88
  ```python
@@ -86,7 +146,7 @@ specstream rows --find-id FIND_ID
86
146
 
87
147
  ## Pricing
88
148
 
89
- New accounts start with $10 of credit for 30 days, no card. A delivered result costs 5 cents, 15 cents or $1 depending on how many facts it carries. No result, no charge. Rechecks cost half. People and agents share one balance. See [pricing](https://specstream.ai/pricing).
149
+ New accounts start with $10 of credit, no card. A delivered result costs 5 cents, 15 cents or $1 depending on how many facts it carries. No result, no charge. Rechecks cost half. People and agents share one balance. See [pricing](https://specstream.ai/pricing).
90
150
 
91
151
  ## Links
92
152
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "specstream-client"
3
- version = "1.3.1"
3
+ version = "1.3.3"
4
4
  description = "Python client for building lead lists and company lists from the live web. Every match comes with its source, priced before it runs."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -11,4 +11,4 @@ __all__ = [
11
11
  "SpecstreamError",
12
12
  "assert_family",
13
13
  ]
14
- __version__ = "1.3.0"
14
+ __version__ = "1.3.3"
@@ -4,9 +4,12 @@ from __future__ import annotations
4
4
 
5
5
  import json
6
6
  import re
7
+ import time
7
8
  import urllib.error
8
9
  import urllib.request
9
10
  from collections.abc import Callable
11
+ from datetime import UTC, datetime
12
+ from email.utils import parsedate_to_datetime
10
13
  from typing import Any, cast
11
14
  from urllib.parse import quote as quote_path
12
15
  from urllib.parse import urlencode, urljoin, urlsplit
@@ -15,7 +18,7 @@ from specstream_client.authorization import require_work_authorization
15
18
  from specstream_client.families import FAMILIES
16
19
 
17
20
  DEFAULT_BASE_URL = "https://api.specstream.ai"
18
- USER_AGENT = "specstream-python/1.3.0"
21
+ USER_AGENT = "specstream-python/1.3.3"
19
22
 
20
23
  OpenFn = Callable[[urllib.request.Request, float], Any]
21
24
 
@@ -27,6 +30,29 @@ class _NoRedirect(urllib.request.HTTPRedirectHandler):
27
30
  return None
28
31
 
29
32
 
33
+ RETRY_AFTER_MAX_S = 10.0
34
+
35
+
36
+ def _retry_after(exc: urllib.error.HTTPError) -> float | None:
37
+ """Seconds to wait before the one retry of a 503 that names its Retry-After, or None."""
38
+ if exc.code != 503 or exc.headers is None:
39
+ return None
40
+ value = (exc.headers.get("Retry-After") or "").strip()
41
+ try:
42
+ seconds = float(value)
43
+ except ValueError:
44
+ try:
45
+ when = parsedate_to_datetime(value)
46
+ except (TypeError, ValueError):
47
+ return None
48
+ if when.tzinfo is None:
49
+ return None
50
+ seconds = (when - datetime.now(UTC)).total_seconds()
51
+ if not 0 <= seconds <= RETRY_AFTER_MAX_S:
52
+ return None
53
+ return seconds
54
+
55
+
30
56
  class SpecstreamError(RuntimeError):
31
57
  def __init__(self, message: str, status: int | None = None, body: Any = None) -> None:
32
58
  super().__init__(message)
@@ -270,7 +296,7 @@ class Client:
270
296
 
271
297
  def web_search(self, *, q: str, passages: bool | None = None) -> dict[str, Any]:
272
298
  """
273
- Fast search: answer a question from the live web in seconds, each value with its source.
299
+ Search API: answer a question from the live web in seconds, each value with its source.
274
300
  """
275
301
  path = "/v1/web-search"
276
302
  query: dict[str, Any] = {
@@ -290,8 +316,9 @@ class Client:
290
316
 
291
317
  def extract_pages(self, request: dict[str, Any]) -> dict[str, Any]:
292
318
  """
293
- Read up to 20 pages live: each page's main text as Markdown or plain text, its title and
294
- its published date. Only a page read is charged.
319
+ Extract API: read up to 20 pages, live from their URLs or from HTML you send, each
320
+ page's main text as Markdown or plain text, its title and its published date. Only a
321
+ page read is charged.
295
322
  """
296
323
  path = "/v1/extract"
297
324
  return self._request("POST", path, request)
@@ -1595,9 +1622,20 @@ class Client:
1595
1622
  payload = None if body is None else json.dumps(body, separators=(",", ":")).encode("utf-8")
1596
1623
  req = urllib.request.Request(url, data=payload, method=method, headers=self._headers())
1597
1624
  try:
1598
- with self._opener(req, self.timeout) as resp:
1599
- status = getattr(resp, "status", 200)
1600
- text = resp.read().decode("utf-8", "replace")
1625
+ try:
1626
+ with self._opener(req, self.timeout) as resp:
1627
+ status = getattr(resp, "status", 200)
1628
+ text = resp.read().decode("utf-8", "replace")
1629
+ except urllib.error.HTTPError as busy:
1630
+ wait = _retry_after(busy)
1631
+ if wait is None:
1632
+ raise
1633
+ # The server did nothing and names when to ask again: ask once more.
1634
+ busy.close()
1635
+ time.sleep(wait)
1636
+ with self._opener(req, self.timeout) as resp:
1637
+ status = getattr(resp, "status", 200)
1638
+ text = resp.read().decode("utf-8", "replace")
1601
1639
  except urllib.error.HTTPError as exc:
1602
1640
  text = exc.read().decode("utf-8", "replace") if exc.fp else ""
1603
1641
  try:
@@ -33,16 +33,16 @@ COMMANDS = {'get-vocabulary': {'method': 'get_vocabulary',
33
33
  'body': False,
34
34
  'body_required': False,
35
35
  'query': {'q': 'string', 'passages': 'boolean'},
36
- 'summary': 'Fast search: answer a question from the live web in seconds, each '
37
- 'value with its source'},
36
+ 'summary': 'Search API: answer a question from the live web in seconds, each value '
37
+ 'with its source'},
38
38
  'extract-pages': {'method': 'extract_pages',
39
39
  'arguments': [],
40
40
  'body': True,
41
41
  'body_required': True,
42
42
  'query': {},
43
- 'summary': "Read up to 20 pages live: each page's main text as Markdown or "
44
- 'plain text, its title and its published date. Only a page read is '
45
- 'charged'},
43
+ 'summary': 'Extract API: read up to 20 pages, live from their URLs or from HTML '
44
+ "you send, each page's main text as Markdown or plain text, its "
45
+ 'title and its published date. Only a page read is charged'},
46
46
  'web-search-answers': {'method': 'web_search_answers',
47
47
  'arguments': [],
48
48
  'body': False,
@@ -29,7 +29,12 @@ def endpoint():
29
29
  status, body = cases.get(
30
30
  self.path.rsplit("/", 1)[-1], (200, '{"data":{"status":"completed"}}')
31
31
  )
32
+ busy = self.path.endswith("/busy") and seen.count(self.path) == 1
33
+ if busy:
34
+ status, body = 503, '{"error":{"code":"capacity"}}'
32
35
  self.send_response(status)
36
+ if busy:
37
+ self.send_header("Retry-After", "0")
33
38
  self.end_headers()
34
39
  self.wfile.write(body.encode())
35
40
 
@@ -48,7 +53,7 @@ def endpoint():
48
53
 
49
54
 
50
55
  def test_error_envelopes_and_malformed_successes_are_not_deliveries(endpoint):
51
- origin, _ = endpoint
56
+ origin, seen = endpoint
52
57
  client = Client("offline", base_url=origin)
53
58
  for case in ("error", "null", "array", "empty"):
54
59
  with pytest.raises(SpecstreamError) as failed:
@@ -59,6 +64,9 @@ def test_error_envelopes_and_malformed_successes_are_not_deliveries(endpoint):
59
64
  assert unavailable.value.status == 503
60
65
  assert unavailable.value.body == "<html>unavailable</html>"
61
66
  assert client.get_find("valid")["status"] == "completed"
67
+ assert client.get_find("busy")["status"] == "completed"
68
+ assert seen.count("/v1/finds/html") == 1
69
+ assert seen.count("/v1/finds/busy") == 2
62
70
 
63
71
 
64
72
  def test_redirect_does_not_forward_account_authority(endpoint):