contractgate 0.3.0__tar.gz → 0.3.1__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.
- {contractgate-0.3.0 → contractgate-0.3.1}/CHANGELOG.md +14 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/PKG-INFO +1 -1
- {contractgate-0.3.0 → contractgate-0.3.1}/pyproject.toml +1 -1
- contractgate-0.3.1/src/contractgate/_version.py +1 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/llm.py +46 -29
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/models.py +6 -4
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/test_llm_gate.py +25 -6
- contractgate-0.3.0/src/contractgate/_version.py +0 -1
- {contractgate-0.3.0 → contractgate-0.3.1}/.gitignore +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/LICENSE +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/README.md +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/__init__.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/_transport.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/async_client.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/client.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/contract.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/exceptions.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/src/contractgate/validator.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/__init__.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/conftest.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/fixtures/contracts/compliance_mode.yaml +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/fixtures/contracts/user_events.yaml +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/fixtures/contracts/with_transform.yaml +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/test_client_async.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/test_client_sync.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/test_contract_parse.py +0 -0
- {contractgate-0.3.0 → contractgate-0.3.1}/tests/test_validator.py +0 -0
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to the `contractgate` Python SDK.
|
|
4
4
|
|
|
5
|
+
## 0.3.1 — 2026-10-13
|
|
6
|
+
|
|
7
|
+
Fixes from end-to-end testing against production. No API changes.
|
|
8
|
+
|
|
9
|
+
- Fix: `LLMGate(contract_id="<name>")` crashed with `KeyError: 'created_at'`.
|
|
10
|
+
`GET /contracts` returns summary rows without timestamps;
|
|
11
|
+
`ContractResponse.created_at` / `updated_at` are now optional.
|
|
12
|
+
- Fix: a rejected LLM output now returns `LLMGateResult(ok=False, violations=[...])`
|
|
13
|
+
instead of raising `ValidationFailedError`. The gateway answers 422 when every
|
|
14
|
+
event fails, which is always true for a gate's single-event batch, so
|
|
15
|
+
`validate_only` and the `auto_repair` loop never saw the violations. A 422
|
|
16
|
+
without an egress body (e.g. a deprecated version pin) is still raised.
|
|
17
|
+
- Default `base_url` is `https://api.datacontractgate.com` (was the dashboard host).
|
|
18
|
+
|
|
5
19
|
## 0.3.0 — 2026-10-11
|
|
6
20
|
|
|
7
21
|
RFC-094. Adds the `contractgate.llm` submodule for LLM output
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: contractgate
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: Python SDK for DataContractGate — high-performance semantic contract enforcement gateway.
|
|
5
5
|
Project-URL: Homepage, https://datacontractgate.com
|
|
6
6
|
Project-URL: Source, https://github.com/nightmoose/contractgate
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "contractgate"
|
|
7
|
-
version = "0.3.
|
|
7
|
+
version = "0.3.1"
|
|
8
8
|
description = "Python SDK for DataContractGate — high-performance semantic contract enforcement gateway."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.1"
|
|
@@ -26,7 +26,7 @@ from typing import Any, Awaitable, Callable, Dict, List, Optional
|
|
|
26
26
|
|
|
27
27
|
from contractgate.async_client import AsyncClient
|
|
28
28
|
from contractgate.client import Client
|
|
29
|
-
from contractgate.exceptions import ContractGateError
|
|
29
|
+
from contractgate.exceptions import ContractGateError, ValidationFailedError
|
|
30
30
|
from contractgate.models import ContractResponse, EgressResponse, Violation
|
|
31
31
|
|
|
32
32
|
_UUID_RE = re.compile(
|
|
@@ -291,6 +291,21 @@ def _result_from_egress(
|
|
|
291
291
|
)
|
|
292
292
|
|
|
293
293
|
|
|
294
|
+
def _egress_from_rejection(err: ValidationFailedError) -> EgressResponse:
|
|
295
|
+
"""Turn a 422 egress rejection back into an ``EgressResponse``.
|
|
296
|
+
|
|
297
|
+
The gateway answers 422 when every event in the batch fails, which is
|
|
298
|
+
always the case for a gate's single-event batch. The body is still a full
|
|
299
|
+
egress response with per-field violations, so a failed validation is a
|
|
300
|
+
result (``ok=False``), not an exception. Any other 422 (e.g. a deprecated
|
|
301
|
+
version pin) has no ``outcomes`` and is re-raised.
|
|
302
|
+
"""
|
|
303
|
+
body = err.body
|
|
304
|
+
if isinstance(body, dict) and "outcomes" in body and "failed" in body:
|
|
305
|
+
return EgressResponse.from_dict(body)
|
|
306
|
+
raise err
|
|
307
|
+
|
|
308
|
+
|
|
294
309
|
def _append_user_message(messages: List[Dict[str, Any]], hint: str) -> List[Dict[str, Any]]:
|
|
295
310
|
return list(messages) + [{"role": "user", "content": hint}]
|
|
296
311
|
|
|
@@ -315,7 +330,7 @@ class LLMGate:
|
|
|
315
330
|
api_key: str,
|
|
316
331
|
contract_id: str,
|
|
317
332
|
llm: Any,
|
|
318
|
-
base_url: str = "https://
|
|
333
|
+
base_url: str = "https://api.datacontractgate.com",
|
|
319
334
|
version: Optional[str] = None,
|
|
320
335
|
disposition: Optional[str] = None,
|
|
321
336
|
max_retries: int = _DEFAULT_MAX_RETRIES,
|
|
@@ -407,6 +422,18 @@ class LLMGate:
|
|
|
407
422
|
)
|
|
408
423
|
return self._resolved[contract_id]
|
|
409
424
|
|
|
425
|
+
def _egress(self, payload: Dict[str, Any], *, dry_run: bool = False) -> EgressResponse:
|
|
426
|
+
try:
|
|
427
|
+
return self._client.egress(
|
|
428
|
+
contract_id=self._resolve(self._contract_id),
|
|
429
|
+
events=[payload],
|
|
430
|
+
version=self._version,
|
|
431
|
+
disposition=self._disposition,
|
|
432
|
+
dry_run=dry_run,
|
|
433
|
+
)
|
|
434
|
+
except ValidationFailedError as err:
|
|
435
|
+
return _egress_from_rejection(err)
|
|
436
|
+
|
|
410
437
|
def validate_only(
|
|
411
438
|
self,
|
|
412
439
|
response: Any,
|
|
@@ -415,13 +442,7 @@ class LLMGate:
|
|
|
415
442
|
) -> LLMGateResult:
|
|
416
443
|
"""Validate a response the caller already produced."""
|
|
417
444
|
payload = extract_json(response)
|
|
418
|
-
egress = self.
|
|
419
|
-
contract_id=self._resolve(self._contract_id),
|
|
420
|
-
events=[payload],
|
|
421
|
-
version=self._version,
|
|
422
|
-
disposition=self._disposition,
|
|
423
|
-
dry_run=dry_run,
|
|
424
|
-
)
|
|
445
|
+
egress = self._egress(payload, dry_run=dry_run)
|
|
425
446
|
return _result_from_egress(egress, raw_response=response, attempts=1)
|
|
426
447
|
|
|
427
448
|
def mask_prompt(
|
|
@@ -479,12 +500,7 @@ class LLMGate:
|
|
|
479
500
|
hints.append(hint)
|
|
480
501
|
current = _append_user_message(current, hint)
|
|
481
502
|
continue
|
|
482
|
-
egress = self.
|
|
483
|
-
contract_id=self._resolve(self._contract_id),
|
|
484
|
-
events=[payload],
|
|
485
|
-
version=self._version,
|
|
486
|
-
disposition=self._disposition,
|
|
487
|
-
)
|
|
503
|
+
egress = self._egress(payload)
|
|
488
504
|
last = _result_from_egress(egress, raw_response=resp, attempts=attempt, hints=hints)
|
|
489
505
|
if egress.failed == 0 or not auto_repair or attempt >= total_attempts:
|
|
490
506
|
return last
|
|
@@ -509,7 +525,7 @@ class AsyncLLMGate:
|
|
|
509
525
|
api_key: str,
|
|
510
526
|
contract_id: str,
|
|
511
527
|
llm: Any,
|
|
512
|
-
base_url: str = "https://
|
|
528
|
+
base_url: str = "https://api.datacontractgate.com",
|
|
513
529
|
version: Optional[str] = None,
|
|
514
530
|
disposition: Optional[str] = None,
|
|
515
531
|
max_retries: int = _DEFAULT_MAX_RETRIES,
|
|
@@ -592,6 +608,18 @@ class AsyncLLMGate:
|
|
|
592
608
|
)
|
|
593
609
|
return self._resolved[contract_id]
|
|
594
610
|
|
|
611
|
+
async def _egress(self, payload: Dict[str, Any], *, dry_run: bool = False) -> EgressResponse:
|
|
612
|
+
try:
|
|
613
|
+
return await self._client.egress(
|
|
614
|
+
contract_id=await self._resolve(self._contract_id),
|
|
615
|
+
events=[payload],
|
|
616
|
+
version=self._version,
|
|
617
|
+
disposition=self._disposition,
|
|
618
|
+
dry_run=dry_run,
|
|
619
|
+
)
|
|
620
|
+
except ValidationFailedError as err:
|
|
621
|
+
return _egress_from_rejection(err)
|
|
622
|
+
|
|
595
623
|
async def validate_only(
|
|
596
624
|
self,
|
|
597
625
|
response: Any,
|
|
@@ -599,13 +627,7 @@ class AsyncLLMGate:
|
|
|
599
627
|
dry_run: bool = False,
|
|
600
628
|
) -> LLMGateResult:
|
|
601
629
|
payload = extract_json(response)
|
|
602
|
-
egress = await self.
|
|
603
|
-
contract_id=await self._resolve(self._contract_id),
|
|
604
|
-
events=[payload],
|
|
605
|
-
version=self._version,
|
|
606
|
-
disposition=self._disposition,
|
|
607
|
-
dry_run=dry_run,
|
|
608
|
-
)
|
|
630
|
+
egress = await self._egress(payload, dry_run=dry_run)
|
|
609
631
|
return _result_from_egress(egress, raw_response=response, attempts=1)
|
|
610
632
|
|
|
611
633
|
async def mask_prompt(
|
|
@@ -649,12 +671,7 @@ class AsyncLLMGate:
|
|
|
649
671
|
hints.append(hint)
|
|
650
672
|
current = _append_user_message(current, hint)
|
|
651
673
|
continue
|
|
652
|
-
egress = await self.
|
|
653
|
-
contract_id=await self._resolve(self._contract_id),
|
|
654
|
-
events=[payload],
|
|
655
|
-
version=self._version,
|
|
656
|
-
disposition=self._disposition,
|
|
657
|
-
)
|
|
674
|
+
egress = await self._egress(payload)
|
|
658
675
|
last = _result_from_egress(egress, raw_response=resp, attempts=attempt, hints=hints)
|
|
659
676
|
if egress.failed == 0 or not auto_repair or attempt >= total_attempts:
|
|
660
677
|
return last
|
|
@@ -193,8 +193,10 @@ class ContractResponse:
|
|
|
193
193
|
name: str
|
|
194
194
|
description: Optional[str]
|
|
195
195
|
multi_stable_resolution: str
|
|
196
|
-
|
|
197
|
-
|
|
196
|
+
# ``GET /contracts`` returns summary rows without timestamps; only the
|
|
197
|
+
# single-contract endpoints include them.
|
|
198
|
+
created_at: Optional[str]
|
|
199
|
+
updated_at: Optional[str]
|
|
198
200
|
version_count: int
|
|
199
201
|
latest_stable_version: Optional[str]
|
|
200
202
|
|
|
@@ -205,8 +207,8 @@ class ContractResponse:
|
|
|
205
207
|
name=raw["name"],
|
|
206
208
|
description=raw.get("description"),
|
|
207
209
|
multi_stable_resolution=raw.get("multi_stable_resolution", "strict"),
|
|
208
|
-
created_at=raw
|
|
209
|
-
updated_at=raw
|
|
210
|
+
created_at=raw.get("created_at"),
|
|
211
|
+
updated_at=raw.get("updated_at"),
|
|
210
212
|
version_count=int(raw.get("version_count", 0)),
|
|
211
213
|
latest_stable_version=raw.get("latest_stable_version"),
|
|
212
214
|
)
|
|
@@ -137,6 +137,13 @@ def _fail_egress_body(payload: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
|
|
140
|
+
def _egress_status(body: Dict[str, Any]) -> int:
|
|
141
|
+
"""Status the gateway really sends: 422 when every event failed."""
|
|
142
|
+
if body.get("failed", 0) > 0 and body.get("passed", 0) == 0:
|
|
143
|
+
return 422
|
|
144
|
+
return 207 if body.get("failed", 0) > 0 else 200
|
|
145
|
+
|
|
146
|
+
|
|
140
147
|
def _ingest_body_with_transform(transformed: Dict[str, Any]) -> Dict[str, Any]:
|
|
141
148
|
return {
|
|
142
149
|
"total": 1,
|
|
@@ -273,7 +280,7 @@ def test_llmgate_negative_max_retries_clamps_to_zero():
|
|
|
273
280
|
|
|
274
281
|
def _client_with_egress(response_body: Dict[str, Any]) -> Client:
|
|
275
282
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
276
|
-
return httpx.Response(
|
|
283
|
+
return httpx.Response(_egress_status(response_body), json=response_body)
|
|
277
284
|
|
|
278
285
|
return Client(
|
|
279
286
|
base_url="https://gw",
|
|
@@ -305,6 +312,18 @@ def test_validate_only_failure_returns_sanitized_result():
|
|
|
305
312
|
assert result.violations[0].kind == ViolationKind.RANGE_VIOLATION
|
|
306
313
|
|
|
307
314
|
|
|
315
|
+
def test_non_egress_422_is_still_raised():
|
|
316
|
+
from contractgate import ValidationFailedError
|
|
317
|
+
|
|
318
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
319
|
+
return httpx.Response(422, json={"error": "version 1.0 is deprecated"})
|
|
320
|
+
|
|
321
|
+
client = Client(base_url="https://gw", api_key="k", transport=httpx.MockTransport(handler))
|
|
322
|
+
gate = LLMGate(api_key="k", contract_id="11111111-1111-4111-8111-111111111111", llm=MagicMock(), client=client)
|
|
323
|
+
with pytest.raises(ValidationFailedError):
|
|
324
|
+
gate.validate_only({"user_id": "a"})
|
|
325
|
+
|
|
326
|
+
|
|
308
327
|
def test_validate_only_asserts_request_shape():
|
|
309
328
|
captured: Dict[str, Any] = {}
|
|
310
329
|
|
|
@@ -375,7 +394,7 @@ class _ToggleTransport(httpx.MockTransport):
|
|
|
375
394
|
def _handler(self, request: httpx.Request) -> httpx.Response:
|
|
376
395
|
self.hits += 1
|
|
377
396
|
body = next(self._bodies)
|
|
378
|
-
return httpx.Response(
|
|
397
|
+
return httpx.Response(_egress_status(body), json=body)
|
|
379
398
|
|
|
380
399
|
|
|
381
400
|
def test_completion_auto_repair_passes_on_second_try():
|
|
@@ -570,14 +589,14 @@ def test_result_is_dataclass_with_expected_fields():
|
|
|
570
589
|
|
|
571
590
|
def _contract_row(cid: str, name: str) -> Dict[str, Any]:
|
|
572
591
|
return {
|
|
592
|
+
# The real ``GET /contracts`` summary row (src/contract.rs
|
|
593
|
+
# ContractSummary): no description or timestamps.
|
|
573
594
|
"id": cid,
|
|
574
595
|
"name": name,
|
|
575
|
-
"description": None,
|
|
576
596
|
"multi_stable_resolution": "strict",
|
|
577
|
-
"created_at": "2026-10-12T00:00:00Z",
|
|
578
|
-
"updated_at": "2026-10-12T00:00:00Z",
|
|
579
|
-
"version_count": 1,
|
|
580
597
|
"latest_stable_version": "1.0.0",
|
|
598
|
+
"version_count": 1,
|
|
599
|
+
"store_event_payloads": False,
|
|
581
600
|
}
|
|
582
601
|
|
|
583
602
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.3.0"
|
|
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
|
|
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
|
|
File without changes
|