tinyfish 0.2.3__tar.gz → 0.2.4__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 (37) hide show
  1. tinyfish-0.2.4/.pre-commit-config.yaml +23 -0
  2. tinyfish-0.2.4/CLAUDE.md +60 -0
  3. {tinyfish-0.2.3 → tinyfish-0.2.4}/PKG-INFO +1 -1
  4. tinyfish-0.2.4/docs/internal/api-integration-header.md +58 -0
  5. {tinyfish-0.2.3 → tinyfish-0.2.4}/pyproject.toml +5 -1
  6. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/client/_base.py +12 -1
  7. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/client/async_.py +2 -0
  8. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/client/sync.py +2 -0
  9. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/agent/__init__.py +50 -12
  10. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/agent/types.py +21 -8
  11. {tinyfish-0.2.3 → tinyfish-0.2.4}/tests/test_client.py +43 -0
  12. {tinyfish-0.2.3 → tinyfish-0.2.4}/uv.lock +1 -1
  13. {tinyfish-0.2.3 → tinyfish-0.2.4}/.gitignore +0 -0
  14. {tinyfish-0.2.3 → tinyfish-0.2.4}/README.md +0 -0
  15. {tinyfish-0.2.3 → tinyfish-0.2.4}/RELEASE.md +0 -0
  16. {tinyfish-0.2.3 → tinyfish-0.2.4}/docs/exceptions-and-errors-guide.md +0 -0
  17. {tinyfish-0.2.3 → tinyfish-0.2.4}/docs/internal/exceptions-and-errors-guide.md +0 -0
  18. {tinyfish-0.2.3 → tinyfish-0.2.4}/docs/internal/publishing-private-guide.md +0 -0
  19. {tinyfish-0.2.3 → tinyfish-0.2.4}/docs/pagination-guide.md +0 -0
  20. {tinyfish-0.2.3 → tinyfish-0.2.4}/docs/proxy-and-browser-profiles.md +0 -0
  21. {tinyfish-0.2.3 → tinyfish-0.2.4}/docs/streaming-guide.md +0 -0
  22. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/__init__.py +0 -0
  23. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/__init__.py +0 -0
  24. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/client/__init__.py +0 -0
  25. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/exceptions.py +0 -0
  26. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/resource.py +0 -0
  27. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/_utils/sse_parser.py +0 -0
  28. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/client.py +0 -0
  29. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/py.typed +0 -0
  30. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/runs/__init__.py +0 -0
  31. {tinyfish-0.2.3 → tinyfish-0.2.4}/src/tinyfish/runs/types.py +0 -0
  32. {tinyfish-0.2.3 → tinyfish-0.2.4}/tests/__init__.py +0 -0
  33. {tinyfish-0.2.3 → tinyfish-0.2.4}/tests/conftest.py +0 -0
  34. {tinyfish-0.2.3 → tinyfish-0.2.4}/tests/test_agent.py +0 -0
  35. {tinyfish-0.2.3 → tinyfish-0.2.4}/tests/test_errors.py +0 -0
  36. {tinyfish-0.2.3 → tinyfish-0.2.4}/tests/test_runs.py +0 -0
  37. {tinyfish-0.2.3 → tinyfish-0.2.4}/tests/testing_guide.md +0 -0
@@ -0,0 +1,23 @@
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v5.0.0
4
+ hooks:
5
+ - id: trailing-whitespace
6
+ - id: end-of-file-fixer
7
+ - id: check-yaml
8
+ - id: check-added-large-files
9
+ - id: check-toml
10
+ - id: debug-statements
11
+
12
+ - repo: https://github.com/astral-sh/ruff-pre-commit
13
+ rev: v0.15.5
14
+ hooks:
15
+ - id: ruff-check
16
+ - id: ruff-format
17
+
18
+ - repo: https://github.com/astral-sh/uv-pre-commit
19
+ rev: 0.10.9
20
+ hooks:
21
+ - id: uv-lock
22
+ name: uv-lock
23
+ files: ^sdk/sdk-python/(uv\.lock|pyproject\.toml)$
@@ -0,0 +1,60 @@
1
+ # TinyFish Python SDK (`tinyfish`)
2
+
3
+ Official Python SDK for TinyFish. Provides sync (`TinyFish`) and async (`AsyncTinyFish`) clients for running
4
+ automations, streaming events, and managing runs.
5
+
6
+ For full method reference and examples: see `README.md`.
7
+
8
+ ---
9
+
10
+ ## Package Manager
11
+
12
+ `uv` — not pip, not poetry.
13
+
14
+ ## Key Entry Points
15
+
16
+ ```
17
+ src/tinyfish/__init__.py → exports TinyFish, AsyncTinyFish, and all public types
18
+ src/tinyfish/client.py → TinyFish client class (sync)
19
+ src/tinyfish/agent/ → agent.run(), agent.queue(), agent.stream()
20
+ src/tinyfish/runs/ → runs.get(), runs.list()
21
+ src/tinyfish/_utils/ → HTTP client base (sync + async), retry, error handling
22
+ ```
23
+
24
+ ## Quick Usage
25
+
26
+ ```python
27
+ from tinyfish import TinyFish
28
+
29
+ client = TinyFish(api_key="your-api-key") # or set TINYFISH_API_KEY env var
30
+ response = client.agent.run(goal="...", url="https://...")
31
+ print(response.result)
32
+ ```
33
+
34
+ ## Tests
35
+
36
+ ```bash
37
+ cd sdk/sdk-python && uv run pytest
38
+ ```
39
+
40
+ ## Pre-commit Hooks
41
+
42
+ ```bash
43
+ cd sdk/sdk-python && pre-commit install
44
+ ```
45
+
46
+ Hooks: `ruff format`, `ruff check`, `uv-lock` sync. Run before first commit and after any dep change.
47
+
48
+ ## Lint / Format
49
+
50
+ ```bash
51
+ cd sdk/sdk-python && uv run ruff check . && uv run ruff format .
52
+ ```
53
+
54
+ ## Package name
55
+
56
+ `tinyfish` (PyPI). The npm org uses a hyphen: `@tiny-fish/` (not `@tinyfish/`).
57
+
58
+ ## Further Reading
59
+
60
+ See `README.md` for full usage examples, authentication patterns, and SDK changelog.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinyfish
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: Official Python SDK for the TinyFish API
5
5
  Requires-Python: >=3.11
6
6
  Requires-Dist: httpx>=0.27.0
@@ -0,0 +1,58 @@
1
+ # API Integration Tagging (`TF_API_INTEGRATION`)
2
+
3
+ Internal-only mechanism for identifying which integration platform is making API requests through our SDK.
4
+
5
+ ## How it works
6
+
7
+ The SDK reads the `TF_API_INTEGRATION` environment variable when building request bodies. When set, the SDK injects `api_integration` into every request body sent to the automation endpoints.
8
+
9
+ ```text
10
+ TF_API_INTEGRATION=n8n --> { "goal": "...", "url": "...", "api_integration": "n8n" }
11
+ ```
12
+
13
+ This reuses the existing `api_integration` body field that the server already parses, stores in the DB, and emits to PostHog — no backend changes needed.
14
+
15
+ This is intentionally **not** exposed as a constructor parameter or public API. It does not appear in editor autocomplete, type definitions, or documentation. Only we control which integrations set this value.
16
+
17
+ ## Environment variable
18
+
19
+ | Variable | Example values | Required |
20
+ | --------------------- | ---------------------- | -------- |
21
+ | `TF_API_INTEGRATION` | `n8n`, `dify`, `zapier` | No |
22
+
23
+ When unset or empty, `api_integration` is omitted from the body. Existing behavior is unchanged.
24
+
25
+ ## Where it's read
26
+
27
+ `src/tinyfish/_utils/client/_base.py` — `_inject_integration()`, called from `_request()` in both sync and async clients:
28
+
29
+ ```python
30
+ integration = os.environ.get("TF_API_INTEGRATION", "").strip()
31
+ if integration:
32
+ json["api_integration"] = integration
33
+ ```
34
+
35
+ This runs on every API request automatically — no per-resource setup needed.
36
+
37
+ ## Server-side flow
38
+
39
+ 1. Server validates `api_integration` via Zod schema (`frontend/app/v1/schemas.ts`)
40
+ 2. `buildRunOptions()` extracts it from the request body
41
+ 3. Stored in `runs.api_integration` DB column
42
+ 4. Emitted on `run_completed` PostHog event as `api_integration` property
43
+
44
+ ## Setting up a new integration
45
+
46
+ To tag requests from a new integration partner:
47
+
48
+ 1. Set `TF_API_INTEGRATION=<name>` in the environment where the SDK runs (e.g., the n8n node, Dify plugin, Zapier action)
49
+ 2. No SDK code changes needed — the env var is picked up automatically
50
+ 3. The value will appear in PostHog under `run_completed.api_integration`
51
+
52
+ ## Tests
53
+
54
+ `tests/test_client.py`:
55
+
56
+ - `test_integration_in_body_via_env` — `api_integration` is in the request body when env var is set
57
+ - `test_no_integration_in_body_by_default` — `api_integration` is absent when env var is not set
58
+ - `test_async_integration_in_body_via_env` — async client variant
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "tinyfish"
3
- version = "0.2.3"
3
+ version = "0.2.4"
4
4
  description = "Official Python SDK for the TinyFish API"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -33,6 +33,10 @@ asyncio_default_fixture_loop_scope = "session"
33
33
  [tool.ruff]
34
34
  src = ["src"]
35
35
  line-length = 120
36
+ [tool.ruff.format]
37
+ quote-style = "double"
38
+ docstring-code-format = true
39
+ docstring-code-line-length = 20
36
40
 
37
41
  [tool.ruff.lint]
38
42
  select = ["E", "F", "I", "UP", "PYI", "TID"]
@@ -4,7 +4,7 @@ from __future__ import annotations
4
4
 
5
5
  import os
6
6
  from importlib.metadata import version as _pkg_version
7
- from typing import TypeVar
7
+ from typing import Any, TypeVar
8
8
 
9
9
  import httpx
10
10
  from pydantic import BaseModel
@@ -79,8 +79,19 @@ class _BaseClient:
79
79
  "Accept": "application/json",
80
80
  "X-API-Key": self._api_key,
81
81
  "User-Agent": f"tinyfish-python/{_pkg_version('tinyfish')}",
82
+ "X-TF-Request-Origin": "tinyfish-python",
82
83
  }
83
84
 
85
+ @staticmethod
86
+ def _inject_integration(json: dict[str, Any] | None) -> dict[str, Any] | None:
87
+ """Inject api_integration from TF_API_INTEGRATION env var into JSON body."""
88
+ if json is None:
89
+ return None
90
+ integration = os.environ.get("TF_API_INTEGRATION", "").strip()
91
+ if integration:
92
+ json["api_integration"] = integration
93
+ return json
94
+
84
95
  def _make_status_error(self, response: httpx.Response) -> APIStatusError:
85
96
  """
86
97
  Translate an error HTTP response into the appropriate SDK exception.
@@ -72,6 +72,7 @@ class BaseAsyncAPIClient(_BaseClient):
72
72
  APIConnectionError: Network/connection error
73
73
  APIStatusError: HTTP error status (4xx, 5xx)
74
74
  """
75
+ json = self._inject_integration(json)
75
76
  max_attempts = self._max_retries + 1
76
77
 
77
78
  # Retryable: 408, 429, 5xx, and network errors (TimeoutException ⊂ RequestError).
@@ -142,6 +143,7 @@ class BaseAsyncAPIClient(_BaseClient):
142
143
  matching the retry behaviour of ``_request()``. Once a 200 response
143
144
  is received and streaming begins, no further retries are attempted.
144
145
  """
146
+ json = self._inject_integration(json)
145
147
  max_attempts = self._max_retries + 1
146
148
 
147
149
  @retry(
@@ -72,6 +72,7 @@ class BaseSyncAPIClient(_BaseClient):
72
72
  APIConnectionError: Network/connection error
73
73
  APIStatusError: HTTP error status (4xx, 5xx)
74
74
  """
75
+ json = self._inject_integration(json)
75
76
  max_attempts = self._max_retries + 1
76
77
 
77
78
  # Retryable: 408, 429, 5xx, and network errors (TimeoutException ⊂ RequestError).
@@ -142,6 +143,7 @@ class BaseSyncAPIClient(_BaseClient):
142
143
  matching the retry behaviour of ``_request()``. Once a 200 response
143
144
  is received and streaming begins, no further retries are attempted.
144
145
  """
146
+ json = self._inject_integration(json)
145
147
  max_attempts = self._max_retries + 1
146
148
 
147
149
  @retry(
@@ -41,8 +41,15 @@ class AgentStream:
41
41
 
42
42
  Use as::
43
43
 
44
- with client.agent.stream(goal=..., url=...) as stream:
45
- for event in stream:
44
+ with (
45
+ client.agent.stream(
46
+ goal=...,
47
+ url=...,
48
+ ) as stream
49
+ ):
50
+ for (
51
+ event
52
+ ) in stream:
46
53
  ...
47
54
  """
48
55
 
@@ -64,8 +71,15 @@ class AsyncAgentStream:
64
71
 
65
72
  Use as::
66
73
 
67
- async with client.agent.stream(goal=..., url=...) as stream:
68
- async for event in stream:
74
+ async with (
75
+ client.agent.stream(
76
+ goal=...,
77
+ url=...,
78
+ ) as stream
79
+ ):
80
+ async for (
81
+ event
82
+ ) in stream:
69
83
  ...
70
84
  """
71
85
 
@@ -166,10 +180,22 @@ class AgentResource(BaseSyncAPIResource):
166
180
  Use the on_* callbacks for a reactive style, or iterate over
167
181
  the stream for a sequential style::
168
182
 
169
- with client.agent.stream(goal=..., url=...) as stream:
170
- for event in stream:
171
- if isinstance(event, ProgressEvent):
172
- print(event.purpose)
183
+ with (
184
+ client.agent.stream(
185
+ goal=...,
186
+ url=...,
187
+ ) as stream
188
+ ):
189
+ for (
190
+ event
191
+ ) in stream:
192
+ if isinstance(
193
+ event,
194
+ ProgressEvent,
195
+ ):
196
+ print(
197
+ event.purpose
198
+ )
173
199
 
174
200
  Args:
175
201
  goal: Natural language description of what to do on the page.
@@ -309,10 +335,22 @@ class AsyncAgentResource(BaseAsyncAPIResource):
309
335
  Use the on_* callbacks for a reactive style, or iterate over
310
336
  the stream for a sequential style::
311
337
 
312
- async with client.agent.stream(goal=..., url=...) as stream:
313
- async for event in stream:
314
- if isinstance(event, ProgressEvent):
315
- print(event.purpose)
338
+ async with (
339
+ client.agent.stream(
340
+ goal=...,
341
+ url=...,
342
+ ) as stream
343
+ ):
344
+ async for (
345
+ event
346
+ ) in stream:
347
+ if isinstance(
348
+ event,
349
+ ProgressEvent,
350
+ ):
351
+ print(
352
+ event.purpose
353
+ )
316
354
 
317
355
  Args:
318
356
  goal: Natural language description of what to do on the page.
@@ -67,12 +67,19 @@ class AgentRunResponse(BaseModel):
67
67
  ```python
68
68
  response = client.agent.run(
69
69
  goal="Find the price of iPhone 15",
70
- url="https://www.apple.com"
70
+ url="https://www.apple.com",
71
71
  )
72
- if response.status == "COMPLETED":
73
- print(response.result)
72
+ if (
73
+ response.status
74
+ == "COMPLETED"
75
+ ):
76
+ print(
77
+ response.result
78
+ )
74
79
  else:
75
- print(f"Failed: {response.error.message}")
80
+ print(
81
+ f"Failed: {response.error.message}"
82
+ )
76
83
  ```
77
84
  """
78
85
 
@@ -103,13 +110,19 @@ class AgentRunAsyncResponse(BaseModel):
103
110
  ```python
104
111
  response = client.agent.queue(
105
112
  goal="Extract product details",
106
- url="https://example.com"
113
+ url="https://example.com",
114
+ )
115
+ print(
116
+ f"Run started: {response.run_id}"
107
117
  )
108
- print(f"Run started: {response.run_id}")
109
118
 
110
119
  # Check status later
111
- run = client.runs.get(response.run_id)
112
- print(f"Status: {run.status}")
120
+ run = client.runs.get(
121
+ response.run_id
122
+ )
123
+ print(
124
+ f"Status: {run.status}"
125
+ )
113
126
  ```
114
127
  """
115
128
 
@@ -184,3 +184,46 @@ def test_repr_short_key():
184
184
  r = repr(c)
185
185
  assert "****" in r
186
186
  assert "short" not in r
187
+
188
+
189
+ # ─── TF_API_INTEGRATION env var ───────────────────────────────────────────
190
+
191
+
192
+ # TF_API_INTEGRATION env var injects api_integration into request body
193
+ @pytest.mark.respx(base_url=BASE_URL)
194
+ def test_integration_in_body_via_env(respx_mock: MockRouter):
195
+ with mock.patch.dict(os.environ, {"TF_API_INTEGRATION": "n8n"}):
196
+ c = TinyFish(api_key=API_KEY, base_url=BASE_URL, max_retries=0)
197
+ route = respx_mock.post(_PATH).mock(return_value=httpx.Response(200, json=_RUN_OK))
198
+ c.agent.run(**_BODY)
199
+ import json
200
+
201
+ body = json.loads(route.calls[0].request.content)
202
+ assert body["api_integration"] == "n8n"
203
+
204
+
205
+ # Without TF_API_INTEGRATION, api_integration is not in request body
206
+ @pytest.mark.respx(base_url=BASE_URL)
207
+ def test_no_integration_in_body_by_default(respx_mock: MockRouter):
208
+ with mock.patch.dict(os.environ, {}, clear=False):
209
+ os.environ.pop("TF_API_INTEGRATION", None)
210
+ c = TinyFish(api_key=API_KEY, base_url=BASE_URL, max_retries=0)
211
+ route = respx_mock.post(_PATH).mock(return_value=httpx.Response(200, json=_RUN_OK))
212
+ c.agent.run(**_BODY)
213
+ import json
214
+
215
+ body = json.loads(route.calls[0].request.content)
216
+ assert "api_integration" not in body
217
+
218
+
219
+ # Async client injects api_integration into request body when env var is set
220
+ @pytest.mark.respx(base_url=BASE_URL)
221
+ async def test_async_integration_in_body_via_env(respx_mock: MockRouter):
222
+ with mock.patch.dict(os.environ, {"TF_API_INTEGRATION": "dify"}):
223
+ route = respx_mock.post(_PATH).mock(return_value=httpx.Response(200, json=_RUN_OK))
224
+ async with AsyncTinyFish(api_key=API_KEY, base_url=BASE_URL, max_retries=0) as c:
225
+ await c.agent.run(**_BODY)
226
+ import json
227
+
228
+ body = json.loads(route.calls[0].request.content)
229
+ assert body["api_integration"] == "dify"
@@ -322,7 +322,7 @@ wheels = [
322
322
 
323
323
  [[package]]
324
324
  name = "tinyfish"
325
- version = "0.2.3"
325
+ version = "0.2.4"
326
326
  source = { editable = "." }
327
327
  dependencies = [
328
328
  { name = "httpx" },
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes