omnimod-sdk 0.1.0__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.
@@ -0,0 +1,48 @@
1
+ # OS/editor files
2
+ .DS_Store
3
+ Thumbs.db
4
+ .idea/
5
+ .vscode/
6
+ *.swp
7
+ *.swo
8
+
9
+ # Secrets and local environment
10
+ .env
11
+ .env.*
12
+ !.env.example
13
+ *.pem
14
+ *.key
15
+ *.crt
16
+ *.p12
17
+ *.pfx
18
+
19
+ # Python
20
+ __pycache__/
21
+ *.py[cod]
22
+ .pytest_cache/
23
+ .ruff_cache/
24
+ .mypy_cache/
25
+ .coverage
26
+ htmlcov/
27
+ .venv/
28
+ venv/
29
+ dist/
30
+ build/
31
+ *.egg-info/
32
+
33
+ # Node / frontend
34
+ node_modules/
35
+ .next/
36
+ out/
37
+ coverage/
38
+ npm-debug.log*
39
+ yarn-debug.log*
40
+ yarn-error.log*
41
+ pnpm-debug.log*
42
+
43
+ # Logs and local artifacts
44
+ *.log
45
+ tmp/
46
+ temp/
47
+ .cache/
48
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The Kavach LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,52 @@
1
+ Metadata-Version: 2.4
2
+ Name: omnimod-sdk
3
+ Version: 0.1.0
4
+ Summary: Python SDK for the OmniMod moderation API
5
+ Project-URL: Homepage, https://omnimod.net
6
+ Project-URL: Documentation, https://omnimod.net/docs
7
+ Project-URL: Source, https://github.com/DivyaHemantCareer/OmniModApp
8
+ Project-URL: Support, https://omnimod.net/support
9
+ Author-email: OmniMod <info@omnimod.net>
10
+ Maintainer-email: OmniMod <info@omnimod.net>
11
+ License-Expression: MIT
12
+ License-File: LICENSE
13
+ Keywords: content-moderation,moderation,safety,trust-and-safety
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Internet :: WWW/HTTP
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Classifier: Typing :: Typed
24
+ Requires-Python: >=3.11
25
+ Description-Content-Type: text/markdown
26
+
27
+ # OmniMod Python SDK
28
+
29
+ Small dependency-free Python client for the OmniMod API.
30
+
31
+ ## Install
32
+
33
+ ```bash
34
+ python -m pip install omnimod-sdk
35
+ ```
36
+
37
+ The package is published as `omnimod-sdk` and imported as `omnimod_sdk`.
38
+ It requires Python 3.11 or newer and an approved OmniMod API key.
39
+
40
+ ## Usage
41
+
42
+ ```python
43
+ from omnimod_sdk import OmniModClient
44
+
45
+ client = OmniModClient(api_key="omad_live_...")
46
+ result = client.moderate_text("hello community", policy="community-default")
47
+
48
+ if result.is_blocked:
49
+ print("blocked", result.severity)
50
+ ```
51
+
52
+ The SDK starts with text moderation. OpenAPI, TypeScript SDK, image helpers, async helpers, and richer typed policy/event APIs are planned after the core API contract stabilizes.
@@ -0,0 +1,26 @@
1
+ # OmniMod Python SDK
2
+
3
+ Small dependency-free Python client for the OmniMod API.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ python -m pip install omnimod-sdk
9
+ ```
10
+
11
+ The package is published as `omnimod-sdk` and imported as `omnimod_sdk`.
12
+ It requires Python 3.11 or newer and an approved OmniMod API key.
13
+
14
+ ## Usage
15
+
16
+ ```python
17
+ from omnimod_sdk import OmniModClient
18
+
19
+ client = OmniModClient(api_key="omad_live_...")
20
+ result = client.moderate_text("hello community", policy="community-default")
21
+
22
+ if result.is_blocked:
23
+ print("blocked", result.severity)
24
+ ```
25
+
26
+ The SDK starts with text moderation. OpenAPI, TypeScript SDK, image helpers, async helpers, and richer typed policy/event APIs are planned after the core API contract stabilizes.
@@ -0,0 +1,46 @@
1
+ [project]
2
+ name = "omnimod-sdk"
3
+ version = "0.1.0"
4
+ description = "Python SDK for the OmniMod moderation API"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ authors = [
10
+ { name = "OmniMod", email = "info@omnimod.net" },
11
+ ]
12
+ maintainers = [
13
+ { name = "OmniMod", email = "info@omnimod.net" },
14
+ ]
15
+ keywords = [
16
+ "content-moderation",
17
+ "moderation",
18
+ "safety",
19
+ "trust-and-safety",
20
+ ]
21
+ classifiers = [
22
+ "Development Status :: 3 - Alpha",
23
+ "Intended Audience :: Developers",
24
+ "License :: OSI Approved :: MIT License",
25
+ "Operating System :: OS Independent",
26
+ "Programming Language :: Python :: 3",
27
+ "Programming Language :: Python :: 3.11",
28
+ "Programming Language :: Python :: 3.12",
29
+ "Topic :: Internet :: WWW/HTTP",
30
+ "Topic :: Software Development :: Libraries :: Python Modules",
31
+ "Typing :: Typed",
32
+ ]
33
+ dependencies = []
34
+
35
+ [project.urls]
36
+ Homepage = "https://omnimod.net"
37
+ Documentation = "https://omnimod.net/docs"
38
+ Source = "https://github.com/DivyaHemantCareer/OmniModApp"
39
+ Support = "https://omnimod.net/support"
40
+
41
+ [tool.hatch.build.targets.wheel]
42
+ packages = ["src/omnimod_sdk"]
43
+
44
+ [build-system]
45
+ requires = ["hatchling"]
46
+ build-backend = "hatchling.build"
@@ -0,0 +1,25 @@
1
+ """Python SDK for the OmniMod moderation API."""
2
+
3
+ __version__ = "0.1.0"
4
+
5
+ from omnimod_sdk.client import OmniModClient
6
+ from omnimod_sdk.errors import (
7
+ OmniModAPIError,
8
+ OmniModAuthenticationError,
9
+ OmniModConnectionError,
10
+ OmniModError,
11
+ OmniModQuotaError,
12
+ OmniModRateLimitError,
13
+ )
14
+ from omnimod_sdk.models import ModerationResponse
15
+
16
+ __all__ = [
17
+ "ModerationResponse",
18
+ "OmniModAPIError",
19
+ "OmniModAuthenticationError",
20
+ "OmniModClient",
21
+ "OmniModConnectionError",
22
+ "OmniModError",
23
+ "OmniModQuotaError",
24
+ "OmniModRateLimitError",
25
+ ]
@@ -0,0 +1,132 @@
1
+ """Synchronous OmniMod API client."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from typing import Any
7
+ from urllib.error import HTTPError, URLError
8
+ from urllib.request import Request, urlopen
9
+
10
+ from omnimod_sdk import __version__
11
+ from omnimod_sdk.errors import (
12
+ OmniModAPIError,
13
+ OmniModAuthenticationError,
14
+ OmniModConnectionError,
15
+ OmniModQuotaError,
16
+ OmniModRateLimitError,
17
+ )
18
+ from omnimod_sdk.models import ModerationResponse
19
+
20
+ DEFAULT_BASE_URL = "https://api.omnimod.net"
21
+
22
+
23
+ class OmniModClient:
24
+ """Client for the OmniMod moderation API."""
25
+
26
+ def __init__(
27
+ self,
28
+ *,
29
+ api_key: str,
30
+ base_url: str = DEFAULT_BASE_URL,
31
+ timeout: float = 30.0,
32
+ user_agent: str | None = None,
33
+ ) -> None:
34
+ if not api_key:
35
+ raise ValueError("api_key is required")
36
+ self._api_key = api_key
37
+ self._base_url = base_url.rstrip("/")
38
+ self._timeout = timeout
39
+ self._user_agent = user_agent or f"omnimod-python/{__version__}"
40
+
41
+ def moderate_text(
42
+ self,
43
+ text: str,
44
+ *,
45
+ policy: str | None = None,
46
+ force_fresh: bool = False,
47
+ metadata: dict[str, Any] | None = None,
48
+ request_id: str | None = None,
49
+ ) -> ModerationResponse:
50
+ """Moderate text and return the normalized OmniMod response."""
51
+
52
+ if not text:
53
+ raise ValueError("text is required")
54
+ payload: dict[str, Any] = {
55
+ "modality": "text",
56
+ "input": text,
57
+ "language": "auto",
58
+ "force_fresh": force_fresh,
59
+ }
60
+ if policy is not None:
61
+ payload["policy"] = policy
62
+ if metadata is not None:
63
+ payload["metadata"] = metadata
64
+ data = self._post_json("/v1/moderations", payload, request_id=request_id)
65
+ return ModerationResponse.from_dict(data)
66
+
67
+ def _post_json(self, path: str, payload: dict[str, Any], *, request_id: str | None = None) -> dict[str, Any]:
68
+ body = json.dumps(payload).encode("utf-8")
69
+ headers = {
70
+ "Authorization": f"Bearer {self._api_key}",
71
+ "Content-Type": "application/json",
72
+ "Accept": "application/json",
73
+ "User-Agent": self._user_agent,
74
+ }
75
+ if request_id:
76
+ headers["X-Request-ID"] = request_id
77
+ request = Request(
78
+ f"{self._base_url}{path}",
79
+ data=body,
80
+ headers=headers,
81
+ method="POST",
82
+ )
83
+ try:
84
+ with urlopen(request, timeout=self._timeout) as response: # noqa: S310 - URL is explicit SDK client input.
85
+ return _decode_json(response.read())
86
+ except HTTPError as exc:
87
+ raise _api_error(exc) from exc
88
+ except URLError as exc:
89
+ raise OmniModConnectionError(str(exc.reason)) from exc
90
+
91
+
92
+ def _decode_json(body: bytes) -> dict[str, Any]:
93
+ if not body:
94
+ return {}
95
+ value = json.loads(body.decode("utf-8"))
96
+ if not isinstance(value, dict):
97
+ raise OmniModAPIError(status_code=0, message="Expected JSON object response", detail=value)
98
+ return value
99
+
100
+
101
+ def _api_error(exc: HTTPError) -> OmniModAPIError:
102
+ body = exc.read()
103
+ try:
104
+ payload = _decode_json(body)
105
+ except (json.JSONDecodeError, UnicodeDecodeError, OmniModAPIError):
106
+ payload = {"detail": body.decode("utf-8", errors="replace")}
107
+ detail = payload.get("detail")
108
+ code = _detail_code(detail)
109
+ message = _detail_message(detail) or exc.reason or f"HTTP {exc.code}"
110
+ if exc.code in {401, 403}:
111
+ return OmniModAuthenticationError(status_code=exc.code, message=message, detail=detail, code=code)
112
+ if exc.code == 429 and code == "quota_exceeded":
113
+ return OmniModQuotaError(status_code=exc.code, message=message, detail=detail, code=code)
114
+ if exc.code == 429:
115
+ return OmniModRateLimitError(status_code=exc.code, message=message, detail=detail, code=code)
116
+ return OmniModAPIError(status_code=exc.code, message=message, detail=detail, code=code)
117
+
118
+
119
+ def _detail_code(detail: Any) -> str | None:
120
+ if isinstance(detail, dict):
121
+ code = detail.get("code")
122
+ return str(code) if code is not None else None
123
+ return None
124
+
125
+
126
+ def _detail_message(detail: Any) -> str | None:
127
+ if isinstance(detail, dict):
128
+ message = detail.get("message")
129
+ return str(message) if message is not None else None
130
+ if isinstance(detail, str):
131
+ return detail
132
+ return None
@@ -0,0 +1,43 @@
1
+ """SDK exceptions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any
6
+
7
+
8
+ class OmniModError(Exception):
9
+ """Base exception for the OmniMod SDK."""
10
+
11
+
12
+ class OmniModConnectionError(OmniModError):
13
+ """Raised when the SDK cannot reach the OmniMod API."""
14
+
15
+
16
+ class OmniModAPIError(OmniModError):
17
+ """Raised when the OmniMod API returns an error response."""
18
+
19
+ def __init__(
20
+ self,
21
+ *,
22
+ status_code: int,
23
+ message: str,
24
+ detail: Any = None,
25
+ code: str | None = None,
26
+ ) -> None:
27
+ super().__init__(message)
28
+ self.status_code = status_code
29
+ self.message = message
30
+ self.detail = detail
31
+ self.code = code
32
+
33
+
34
+ class OmniModAuthenticationError(OmniModAPIError):
35
+ """Raised for invalid keys or disabled organizations."""
36
+
37
+
38
+ class OmniModQuotaError(OmniModAPIError):
39
+ """Raised when monthly moderation quota is exhausted."""
40
+
41
+
42
+ class OmniModRateLimitError(OmniModAPIError):
43
+ """Raised when the request is rate limited."""
@@ -0,0 +1,77 @@
1
+ """SDK response models."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from typing import Any
7
+
8
+
9
+ @dataclass(frozen=True)
10
+ class ModerationResponse:
11
+ """Normalized moderation response returned by OmniMod."""
12
+
13
+ id: str
14
+ request_id: str
15
+ decision: str
16
+ engine_action: str
17
+ modality: str
18
+ severity: str
19
+ scores: dict[str, float]
20
+ analyzer_results: list[dict[str, Any]]
21
+ policy: dict[str, Any]
22
+ provider: str
23
+ provider_version: str
24
+ provider_status: str
25
+ degraded_reason: str | None
26
+ routing: dict[str, Any]
27
+ language: dict[str, Any]
28
+ provider_usage: dict[str, Any]
29
+ processing_time_ms: float | None
30
+ created_at: str
31
+ cache: dict[str, Any]
32
+ raw: dict[str, Any] = field(repr=False)
33
+
34
+ @classmethod
35
+ def from_dict(cls, payload: dict[str, Any]) -> "ModerationResponse":
36
+ """Create a response model from the OmniMod API payload."""
37
+
38
+ return cls(
39
+ id=str(payload["id"]),
40
+ request_id=str(payload["request_id"]),
41
+ decision=str(payload["decision"]),
42
+ engine_action=str(payload["engine_action"]),
43
+ modality=str(payload["modality"]),
44
+ severity=str(payload["severity"]),
45
+ scores=dict(payload.get("scores") or {}),
46
+ analyzer_results=list(payload.get("analyzer_results") or []),
47
+ policy=dict(payload.get("policy") or {}),
48
+ provider=str(payload.get("provider") or ""),
49
+ provider_version=str(payload.get("provider_version") or ""),
50
+ provider_status=str(payload.get("provider_status") or ""),
51
+ degraded_reason=payload.get("degraded_reason"),
52
+ routing=dict(payload.get("routing") or {}),
53
+ language=dict(payload.get("language") or {}),
54
+ provider_usage=dict(payload.get("provider_usage") or {}),
55
+ processing_time_ms=payload.get("processing_time_ms"),
56
+ created_at=str(payload["created_at"]),
57
+ cache=dict(payload.get("cache") or {}),
58
+ raw=payload,
59
+ )
60
+
61
+ @property
62
+ def is_allowed(self) -> bool:
63
+ """Return whether the final decision is allow."""
64
+
65
+ return self.decision == "allow"
66
+
67
+ @property
68
+ def is_flagged(self) -> bool:
69
+ """Return whether the final decision is flag/review."""
70
+
71
+ return self.decision == "flag"
72
+
73
+ @property
74
+ def is_blocked(self) -> bool:
75
+ """Return whether the final decision is block."""
76
+
77
+ return self.decision == "block"
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,167 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
5
+ from threading import Thread
6
+ from typing import Any
7
+
8
+ import pytest
9
+
10
+ from omnimod_sdk import OmniModClient
11
+ from omnimod_sdk.client import DEFAULT_BASE_URL
12
+ from omnimod_sdk.errors import OmniModQuotaError, OmniModRateLimitError
13
+
14
+
15
+ def test_default_base_url_uses_omnimod_net() -> None:
16
+ assert DEFAULT_BASE_URL == "https://api.omnimod.net"
17
+
18
+
19
+ def test_moderate_text_sends_expected_request() -> None:
20
+ server = _TestServer(response=_moderation_payload())
21
+ try:
22
+ client = OmniModClient(api_key="omad_test_key", base_url=server.url)
23
+
24
+ result = client.moderate_text(
25
+ "hello community",
26
+ policy="community-default",
27
+ metadata={"external_id": "comment-1"},
28
+ request_id="req_sdk_test",
29
+ )
30
+
31
+ assert result.is_allowed is True
32
+ assert result.language == {"detected": "en", "confidence": 1.0}
33
+ assert result.routing == {"mode": "rules_only", "reason": "rules_clear"}
34
+ assert result.provider_usage == {"text_llm_credits": 0}
35
+ assert server.requests == [
36
+ {
37
+ "path": "/v1/moderations",
38
+ "authorization": "Bearer omad_test_key",
39
+ "request_id": "req_sdk_test",
40
+ "body": {
41
+ "modality": "text",
42
+ "input": "hello community",
43
+ "language": "auto",
44
+ "force_fresh": False,
45
+ "policy": "community-default",
46
+ "metadata": {"external_id": "comment-1"},
47
+ },
48
+ }
49
+ ]
50
+ finally:
51
+ server.close()
52
+
53
+
54
+ def test_quota_error_maps_to_specific_exception() -> None:
55
+ server = _TestServer(
56
+ status=429,
57
+ response={
58
+ "detail": {
59
+ "code": "quota_exceeded",
60
+ "message": "Monthly moderation quota exceeded",
61
+ "remaining": 0,
62
+ }
63
+ },
64
+ )
65
+ try:
66
+ client = OmniModClient(api_key="omad_test_key", base_url=server.url)
67
+
68
+ with pytest.raises(OmniModQuotaError) as exc_info:
69
+ client.moderate_text("hello")
70
+
71
+ assert exc_info.value.status_code == 429
72
+ assert exc_info.value.code == "quota_exceeded"
73
+ assert exc_info.value.detail["remaining"] == 0
74
+ finally:
75
+ server.close()
76
+
77
+
78
+ def test_rate_limit_error_maps_to_specific_exception() -> None:
79
+ server = _TestServer(
80
+ status=429,
81
+ response={
82
+ "detail": {
83
+ "code": "rate_limited",
84
+ "message": "Rate limit exceeded",
85
+ "reset_seconds": 60,
86
+ }
87
+ },
88
+ )
89
+ try:
90
+ client = OmniModClient(api_key="omad_test_key", base_url=server.url)
91
+
92
+ with pytest.raises(OmniModRateLimitError) as exc_info:
93
+ client.moderate_text("hello")
94
+
95
+ assert exc_info.value.status_code == 429
96
+ assert exc_info.value.code == "rate_limited"
97
+ assert exc_info.value.detail["reset_seconds"] == 60
98
+ finally:
99
+ server.close()
100
+
101
+
102
+ class _TestServer:
103
+ def __init__(self, *, response: dict[str, Any], status: int = 200) -> None:
104
+ self.requests: list[dict[str, Any]] = []
105
+ self._response = response
106
+ self._status = status
107
+
108
+ outer = self
109
+
110
+ class Handler(BaseHTTPRequestHandler):
111
+ def do_POST(self) -> None: # noqa: N802
112
+ body = self.rfile.read(int(self.headers.get("Content-Length", "0")))
113
+ outer.requests.append(
114
+ {
115
+ "path": self.path,
116
+ "authorization": self.headers.get("Authorization"),
117
+ "request_id": self.headers.get("X-Request-ID"),
118
+ "body": json.loads(body.decode("utf-8")),
119
+ }
120
+ )
121
+ payload = json.dumps(outer._response).encode("utf-8")
122
+ self.send_response(outer._status)
123
+ self.send_header("Content-Type", "application/json")
124
+ self.send_header("Content-Length", str(len(payload)))
125
+ self.end_headers()
126
+ self.wfile.write(payload)
127
+
128
+ def log_message(self, format: str, *args: object) -> None:
129
+ return None
130
+
131
+ self._server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
132
+ self._thread = Thread(target=self._server.serve_forever, daemon=True)
133
+ self._thread.start()
134
+
135
+ @property
136
+ def url(self) -> str:
137
+ host, port = self._server.server_address
138
+ return f"http://{host}:{port}"
139
+
140
+ def close(self) -> None:
141
+ self._server.shutdown()
142
+ self._server.server_close()
143
+ self._thread.join(timeout=2)
144
+
145
+
146
+ def _moderation_payload() -> dict[str, Any]:
147
+ return {
148
+ "id": "mod_123",
149
+ "request_id": "req_sdk_test",
150
+ "decision": "allow",
151
+ "engine_action": "allow",
152
+ "modality": "text",
153
+ "severity": "none",
154
+ "scores": {"profanity": 0.0, "language": 0.0},
155
+ "analyzer_results": [],
156
+ "policy": {"id": "pol_123", "version_id": "pv_123", "version": 1},
157
+ "provider": "omnimod-engine",
158
+ "provider_version": "omnimod-engine-v1",
159
+ "provider_status": "complete",
160
+ "degraded_reason": None,
161
+ "routing": {"mode": "rules_only", "reason": "rules_clear"},
162
+ "language": {"detected": "en", "confidence": 1.0},
163
+ "provider_usage": {"text_llm_credits": 0},
164
+ "processing_time_ms": 12.5,
165
+ "created_at": "2026-05-07T00:00:00Z",
166
+ "cache": {"status": "miss", "content_hash": "sha256:abc"},
167
+ }