gateframe 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.
- gateframe-0.1.0/LICENSE +21 -0
- gateframe-0.1.0/PKG-INFO +160 -0
- gateframe-0.1.0/README.md +135 -0
- gateframe-0.1.0/gateframe/__init__.py +1 -0
- gateframe-0.1.0/gateframe/audit/__init__.py +1 -0
- gateframe-0.1.0/gateframe/audit/log.py +69 -0
- gateframe-0.1.0/gateframe/core/__init__.py +1 -0
- gateframe-0.1.0/gateframe/core/contract.py +44 -0
- gateframe-0.1.0/gateframe/core/failure.py +27 -0
- gateframe-0.1.0/gateframe/core/rule.py +16 -0
- gateframe-0.1.0/gateframe/rules/__init__.py +1 -0
- gateframe-0.1.0/gateframe/rules/structural.py +60 -0
- gateframe-0.1.0/gateframe.egg-info/PKG-INFO +160 -0
- gateframe-0.1.0/gateframe.egg-info/SOURCES.txt +17 -0
- gateframe-0.1.0/gateframe.egg-info/dependency_links.txt +1 -0
- gateframe-0.1.0/gateframe.egg-info/requires.txt +7 -0
- gateframe-0.1.0/gateframe.egg-info/top_level.txt +1 -0
- gateframe-0.1.0/pyproject.toml +47 -0
- gateframe-0.1.0/setup.cfg +4 -0
gateframe-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Practical Mind
|
|
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.
|
gateframe-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gateframe
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Behavioral validation for LLM outputs in production workflows.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Keywords: llm,validation,behavioral,workflow,ai
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: pydantic<3.0,>=2.0
|
|
19
|
+
Requires-Dist: structlog>=23.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff>=0.4.0; extra == "dev"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# Gateframe
|
|
27
|
+
|
|
28
|
+
Behavioral validation for LLM outputs. Not schema validation - tools like Instructor and Pydantic AI already handle that. gateframe validates whether an LLM output *behaves correctly* given the context it was generated in, stays within decision boundaries, and fails in ways your system can recover from.
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from pydantic import BaseModel
|
|
32
|
+
from gateframe.core.contract import ValidationContract
|
|
33
|
+
from gateframe.core.failure import FailureMode
|
|
34
|
+
from gateframe.rules.structural import StructuralRule
|
|
35
|
+
|
|
36
|
+
class TriageOutput(BaseModel):
|
|
37
|
+
severity: str
|
|
38
|
+
recommendation: str
|
|
39
|
+
confidence: float
|
|
40
|
+
|
|
41
|
+
contract = ValidationContract(
|
|
42
|
+
name="triage_check",
|
|
43
|
+
rules=[
|
|
44
|
+
StructuralRule(schema=TriageOutput),
|
|
45
|
+
],
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
result = contract.validate({
|
|
49
|
+
"severity": "high",
|
|
50
|
+
"recommendation": "Escalate to on-call engineer",
|
|
51
|
+
"confidence": 0.92,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
assert result.passed
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
When validation fails, you get structured failure records - not just a boolean:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
result = contract.validate({"severity": "high"})
|
|
61
|
+
|
|
62
|
+
assert not result.passed
|
|
63
|
+
for failure in result.failures:
|
|
64
|
+
print(failure.message)
|
|
65
|
+
# structural:TriageOutput failed: recommendation: Field required; confidence: Field required.
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Failure modes
|
|
69
|
+
|
|
70
|
+
gateframe distinguishes four failure modes instead of binary pass/fail:
|
|
71
|
+
|
|
72
|
+
**HARD_FAIL** - Stop execution, escalate. The output is wrong in a way that cannot be auto-recovered.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
# Missing required fields in a medical triage output
|
|
76
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.HARD_FAIL)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**SOFT_FAIL** - Flag the output and continue with degraded confidence. Something is off but not critical.
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
# Output has all fields but confidence is below threshold
|
|
83
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.SOFT_FAIL)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**RETRY** - Re-prompt with failure context. The output is fixable by trying again.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
# Malformed JSON that might parse on a second attempt
|
|
90
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.RETRY)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**SILENT_FAIL** - The most dangerous kind. Output looks valid but violates semantic or boundary rules. gateframe makes these visible instead of letting them pass through.
|
|
94
|
+
|
|
95
|
+
## When to use gateframe
|
|
96
|
+
|
|
97
|
+
Use it when:
|
|
98
|
+
- You need to validate LLM outputs beyond schema checks - behavioral contracts, decision boundaries, cross-step propagation
|
|
99
|
+
- You need structured failure records for debugging production incidents
|
|
100
|
+
- You want to distinguish between "retry this", "flag this", and "stop everything"
|
|
101
|
+
|
|
102
|
+
Don't use it when:
|
|
103
|
+
- You only need schema extraction from LLM outputs (use Instructor or Pydantic AI)
|
|
104
|
+
- You need offline model evaluation or benchmarking (use DeepEval or RAGAS)
|
|
105
|
+
- You need content safety filtering (use a dedicated guardrails tool)
|
|
106
|
+
|
|
107
|
+
## Installation
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
pip install gateframe
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
For development:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
git clone https://github.com/practicalmind-ai/gateframe.git
|
|
117
|
+
cd gateframe
|
|
118
|
+
pip install -e ".[dev]"
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Quickstart
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from pydantic import BaseModel
|
|
125
|
+
from gateframe.core.contract import ValidationContract
|
|
126
|
+
from gateframe.rules.structural import StructuralRule
|
|
127
|
+
from gateframe.audit.log import AuditLog
|
|
128
|
+
|
|
129
|
+
# Define your expected output schema
|
|
130
|
+
class AgentResponse(BaseModel):
|
|
131
|
+
action: str
|
|
132
|
+
reasoning: str
|
|
133
|
+
|
|
134
|
+
# Create a contract with rules
|
|
135
|
+
contract = ValidationContract(
|
|
136
|
+
name="agent_response_check",
|
|
137
|
+
rules=[StructuralRule(schema=AgentResponse)],
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
# Validate LLM output
|
|
141
|
+
result = contract.validate({
|
|
142
|
+
"action": "send_email",
|
|
143
|
+
"reasoning": "User requested a follow-up email to the client.",
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
# Log the validation event
|
|
147
|
+
audit = AuditLog()
|
|
148
|
+
audit.record(result)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Running tests
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
pip install -e ".[dev]"
|
|
155
|
+
pytest -q
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# Gateframe
|
|
2
|
+
|
|
3
|
+
Behavioral validation for LLM outputs. Not schema validation - tools like Instructor and Pydantic AI already handle that. gateframe validates whether an LLM output *behaves correctly* given the context it was generated in, stays within decision boundaries, and fails in ways your system can recover from.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
from gateframe.core.contract import ValidationContract
|
|
8
|
+
from gateframe.core.failure import FailureMode
|
|
9
|
+
from gateframe.rules.structural import StructuralRule
|
|
10
|
+
|
|
11
|
+
class TriageOutput(BaseModel):
|
|
12
|
+
severity: str
|
|
13
|
+
recommendation: str
|
|
14
|
+
confidence: float
|
|
15
|
+
|
|
16
|
+
contract = ValidationContract(
|
|
17
|
+
name="triage_check",
|
|
18
|
+
rules=[
|
|
19
|
+
StructuralRule(schema=TriageOutput),
|
|
20
|
+
],
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
result = contract.validate({
|
|
24
|
+
"severity": "high",
|
|
25
|
+
"recommendation": "Escalate to on-call engineer",
|
|
26
|
+
"confidence": 0.92,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
assert result.passed
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
When validation fails, you get structured failure records - not just a boolean:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
result = contract.validate({"severity": "high"})
|
|
36
|
+
|
|
37
|
+
assert not result.passed
|
|
38
|
+
for failure in result.failures:
|
|
39
|
+
print(failure.message)
|
|
40
|
+
# structural:TriageOutput failed: recommendation: Field required; confidence: Field required.
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Failure modes
|
|
44
|
+
|
|
45
|
+
gateframe distinguishes four failure modes instead of binary pass/fail:
|
|
46
|
+
|
|
47
|
+
**HARD_FAIL** - Stop execution, escalate. The output is wrong in a way that cannot be auto-recovered.
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
# Missing required fields in a medical triage output
|
|
51
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.HARD_FAIL)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**SOFT_FAIL** - Flag the output and continue with degraded confidence. Something is off but not critical.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
# Output has all fields but confidence is below threshold
|
|
58
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.SOFT_FAIL)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**RETRY** - Re-prompt with failure context. The output is fixable by trying again.
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
# Malformed JSON that might parse on a second attempt
|
|
65
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.RETRY)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**SILENT_FAIL** - The most dangerous kind. Output looks valid but violates semantic or boundary rules. gateframe makes these visible instead of letting them pass through.
|
|
69
|
+
|
|
70
|
+
## When to use gateframe
|
|
71
|
+
|
|
72
|
+
Use it when:
|
|
73
|
+
- You need to validate LLM outputs beyond schema checks - behavioral contracts, decision boundaries, cross-step propagation
|
|
74
|
+
- You need structured failure records for debugging production incidents
|
|
75
|
+
- You want to distinguish between "retry this", "flag this", and "stop everything"
|
|
76
|
+
|
|
77
|
+
Don't use it when:
|
|
78
|
+
- You only need schema extraction from LLM outputs (use Instructor or Pydantic AI)
|
|
79
|
+
- You need offline model evaluation or benchmarking (use DeepEval or RAGAS)
|
|
80
|
+
- You need content safety filtering (use a dedicated guardrails tool)
|
|
81
|
+
|
|
82
|
+
## Installation
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pip install gateframe
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
For development:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
git clone https://github.com/practicalmind-ai/gateframe.git
|
|
92
|
+
cd gateframe
|
|
93
|
+
pip install -e ".[dev]"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Quickstart
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from pydantic import BaseModel
|
|
100
|
+
from gateframe.core.contract import ValidationContract
|
|
101
|
+
from gateframe.rules.structural import StructuralRule
|
|
102
|
+
from gateframe.audit.log import AuditLog
|
|
103
|
+
|
|
104
|
+
# Define your expected output schema
|
|
105
|
+
class AgentResponse(BaseModel):
|
|
106
|
+
action: str
|
|
107
|
+
reasoning: str
|
|
108
|
+
|
|
109
|
+
# Create a contract with rules
|
|
110
|
+
contract = ValidationContract(
|
|
111
|
+
name="agent_response_check",
|
|
112
|
+
rules=[StructuralRule(schema=AgentResponse)],
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
# Validate LLM output
|
|
116
|
+
result = contract.validate({
|
|
117
|
+
"action": "send_email",
|
|
118
|
+
"reasoning": "User requested a follow-up email to the client.",
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
# Log the validation event
|
|
122
|
+
audit = AuditLog()
|
|
123
|
+
audit.record(result)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Running tests
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
pip install -e ".[dev]"
|
|
130
|
+
pytest -q
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import structlog
|
|
7
|
+
|
|
8
|
+
from gateframe.core.contract import ValidationResult
|
|
9
|
+
|
|
10
|
+
logger = structlog.get_logger()
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AuditEntry:
|
|
14
|
+
__slots__ = (
|
|
15
|
+
"timestamp",
|
|
16
|
+
"contract_name",
|
|
17
|
+
"passed",
|
|
18
|
+
"rules_applied",
|
|
19
|
+
"rules_failed",
|
|
20
|
+
"failures",
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
def __init__(self, result: ValidationResult) -> None:
|
|
24
|
+
self.timestamp = datetime.now(timezone.utc)
|
|
25
|
+
self.contract_name = result.contract_name
|
|
26
|
+
self.passed = result.passed
|
|
27
|
+
self.rules_applied = result.rules_applied
|
|
28
|
+
self.rules_failed = result.rules_failed
|
|
29
|
+
self.failures = [
|
|
30
|
+
{
|
|
31
|
+
"rule_name": f.rule_name,
|
|
32
|
+
"failure_mode": f.failure_mode.value,
|
|
33
|
+
"message": f.message,
|
|
34
|
+
}
|
|
35
|
+
for f in result.failures
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
def to_dict(self) -> dict[str, Any]:
|
|
39
|
+
return {
|
|
40
|
+
"timestamp": self.timestamp.isoformat(),
|
|
41
|
+
"contract_name": self.contract_name,
|
|
42
|
+
"passed": self.passed,
|
|
43
|
+
"rules_applied": self.rules_applied,
|
|
44
|
+
"rules_failed": self.rules_failed,
|
|
45
|
+
"failures": self.failures,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class AuditLog:
|
|
50
|
+
def __init__(self) -> None:
|
|
51
|
+
self._entries: list[AuditEntry] = []
|
|
52
|
+
|
|
53
|
+
def record(self, result: ValidationResult) -> None:
|
|
54
|
+
entry = AuditEntry(result)
|
|
55
|
+
self._entries.append(entry)
|
|
56
|
+
logger.info(
|
|
57
|
+
"validation_event",
|
|
58
|
+
contract=entry.contract_name,
|
|
59
|
+
passed=entry.passed,
|
|
60
|
+
rules_applied=entry.rules_applied,
|
|
61
|
+
rules_failed=entry.rules_failed,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def entries(self) -> list[AuditEntry]:
|
|
66
|
+
return list(self._entries)
|
|
67
|
+
|
|
68
|
+
def clear(self) -> None:
|
|
69
|
+
self._entries.clear()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, Field
|
|
7
|
+
|
|
8
|
+
from gateframe.core.failure import FailureResult
|
|
9
|
+
from gateframe.core.rule import Rule
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ValidationResult(BaseModel):
|
|
13
|
+
passed: bool
|
|
14
|
+
contract_name: str
|
|
15
|
+
failures: list[FailureResult] = Field(default_factory=list)
|
|
16
|
+
rules_applied: int
|
|
17
|
+
rules_failed: int
|
|
18
|
+
timestamp: datetime = Field(
|
|
19
|
+
default_factory=lambda: datetime.now(timezone.utc),
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
model_config = {"frozen": True}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ValidationContract:
|
|
26
|
+
def __init__(self, name: str, rules: list[Rule]) -> None:
|
|
27
|
+
self.name = name
|
|
28
|
+
self.rules = rules
|
|
29
|
+
|
|
30
|
+
def validate(self, output: Any, **context: Any) -> ValidationResult: # noqa: ANN401
|
|
31
|
+
failures: list[FailureResult] = []
|
|
32
|
+
|
|
33
|
+
for rule in self.rules:
|
|
34
|
+
result = rule.validate(output, **context)
|
|
35
|
+
if result is not None:
|
|
36
|
+
failures.append(result)
|
|
37
|
+
|
|
38
|
+
return ValidationResult(
|
|
39
|
+
passed=len(failures) == 0,
|
|
40
|
+
contract_name=self.name,
|
|
41
|
+
failures=failures,
|
|
42
|
+
rules_applied=len(self.rules),
|
|
43
|
+
rules_failed=len(failures),
|
|
44
|
+
)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel, Field
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class FailureMode(Enum):
|
|
11
|
+
HARD_FAIL = "hard_fail"
|
|
12
|
+
SOFT_FAIL = "soft_fail"
|
|
13
|
+
RETRY = "retry"
|
|
14
|
+
SILENT_FAIL = "silent_fail"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class FailureResult(BaseModel):
|
|
18
|
+
rule_name: str
|
|
19
|
+
rule_description: str = ""
|
|
20
|
+
failure_mode: FailureMode
|
|
21
|
+
message: str
|
|
22
|
+
context: dict[str, Any] = Field(default_factory=dict)
|
|
23
|
+
timestamp: datetime = Field(
|
|
24
|
+
default_factory=lambda: datetime.now(timezone.utc),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
model_config = {"frozen": True}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from gateframe.core.failure import FailureResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Rule(ABC):
|
|
10
|
+
def __init__(self, name: str, description: str = "") -> None:
|
|
11
|
+
self.name = name
|
|
12
|
+
self.description = description
|
|
13
|
+
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def validate(self, output: Any, **context: Any) -> FailureResult | None: # noqa: ANN401
|
|
16
|
+
...
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, ValidationError
|
|
6
|
+
|
|
7
|
+
from gateframe.core.failure import FailureMode, FailureResult
|
|
8
|
+
from gateframe.core.rule import Rule
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class StructuralRule(Rule):
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
schema: type[BaseModel],
|
|
15
|
+
failure_mode: FailureMode = FailureMode.HARD_FAIL,
|
|
16
|
+
name: str = "",
|
|
17
|
+
description: str = "",
|
|
18
|
+
) -> None:
|
|
19
|
+
resolved_name = name or f"structural:{schema.__name__}"
|
|
20
|
+
super().__init__(resolved_name, description)
|
|
21
|
+
self.schema = schema
|
|
22
|
+
self.failure_mode = failure_mode
|
|
23
|
+
|
|
24
|
+
def validate(self, output: Any, **context: Any) -> FailureResult | None: # noqa: ANN401
|
|
25
|
+
if isinstance(output, self.schema):
|
|
26
|
+
return None
|
|
27
|
+
|
|
28
|
+
if isinstance(output, dict):
|
|
29
|
+
try:
|
|
30
|
+
self.schema.model_validate(output)
|
|
31
|
+
return None
|
|
32
|
+
except ValidationError as exc:
|
|
33
|
+
return self._build_failure(exc)
|
|
34
|
+
|
|
35
|
+
return FailureResult(
|
|
36
|
+
rule_name=self.name,
|
|
37
|
+
rule_description=self.description,
|
|
38
|
+
failure_mode=self.failure_mode,
|
|
39
|
+
message=(
|
|
40
|
+
f"{self.name} failed: expected dict or {self.schema.__name__}, "
|
|
41
|
+
f"got {type(output).__name__}."
|
|
42
|
+
),
|
|
43
|
+
context={"expected_type": self.schema.__name__, "actual_type": type(output).__name__},
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def _build_failure(self, exc: ValidationError) -> FailureResult:
|
|
47
|
+
errors = exc.errors()
|
|
48
|
+
parts = []
|
|
49
|
+
for err in errors:
|
|
50
|
+
loc = ".".join(str(segment) for segment in err["loc"])
|
|
51
|
+
parts.append(f"{loc}: {err['msg']}")
|
|
52
|
+
detail = "; ".join(parts)
|
|
53
|
+
|
|
54
|
+
return FailureResult(
|
|
55
|
+
rule_name=self.name,
|
|
56
|
+
rule_description=self.description,
|
|
57
|
+
failure_mode=self.failure_mode,
|
|
58
|
+
message=f"{self.name} failed: {detail}.",
|
|
59
|
+
context={"validation_errors": errors},
|
|
60
|
+
)
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gateframe
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Behavioral validation for LLM outputs in production workflows.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Keywords: llm,validation,behavioral,workflow,ai
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: pydantic<3.0,>=2.0
|
|
19
|
+
Requires-Dist: structlog>=23.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff>=0.4.0; extra == "dev"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# Gateframe
|
|
27
|
+
|
|
28
|
+
Behavioral validation for LLM outputs. Not schema validation - tools like Instructor and Pydantic AI already handle that. gateframe validates whether an LLM output *behaves correctly* given the context it was generated in, stays within decision boundaries, and fails in ways your system can recover from.
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from pydantic import BaseModel
|
|
32
|
+
from gateframe.core.contract import ValidationContract
|
|
33
|
+
from gateframe.core.failure import FailureMode
|
|
34
|
+
from gateframe.rules.structural import StructuralRule
|
|
35
|
+
|
|
36
|
+
class TriageOutput(BaseModel):
|
|
37
|
+
severity: str
|
|
38
|
+
recommendation: str
|
|
39
|
+
confidence: float
|
|
40
|
+
|
|
41
|
+
contract = ValidationContract(
|
|
42
|
+
name="triage_check",
|
|
43
|
+
rules=[
|
|
44
|
+
StructuralRule(schema=TriageOutput),
|
|
45
|
+
],
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
result = contract.validate({
|
|
49
|
+
"severity": "high",
|
|
50
|
+
"recommendation": "Escalate to on-call engineer",
|
|
51
|
+
"confidence": 0.92,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
assert result.passed
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
When validation fails, you get structured failure records - not just a boolean:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
result = contract.validate({"severity": "high"})
|
|
61
|
+
|
|
62
|
+
assert not result.passed
|
|
63
|
+
for failure in result.failures:
|
|
64
|
+
print(failure.message)
|
|
65
|
+
# structural:TriageOutput failed: recommendation: Field required; confidence: Field required.
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Failure modes
|
|
69
|
+
|
|
70
|
+
gateframe distinguishes four failure modes instead of binary pass/fail:
|
|
71
|
+
|
|
72
|
+
**HARD_FAIL** - Stop execution, escalate. The output is wrong in a way that cannot be auto-recovered.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
# Missing required fields in a medical triage output
|
|
76
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.HARD_FAIL)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**SOFT_FAIL** - Flag the output and continue with degraded confidence. Something is off but not critical.
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
# Output has all fields but confidence is below threshold
|
|
83
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.SOFT_FAIL)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**RETRY** - Re-prompt with failure context. The output is fixable by trying again.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
# Malformed JSON that might parse on a second attempt
|
|
90
|
+
StructuralRule(schema=TriageOutput, failure_mode=FailureMode.RETRY)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**SILENT_FAIL** - The most dangerous kind. Output looks valid but violates semantic or boundary rules. gateframe makes these visible instead of letting them pass through.
|
|
94
|
+
|
|
95
|
+
## When to use gateframe
|
|
96
|
+
|
|
97
|
+
Use it when:
|
|
98
|
+
- You need to validate LLM outputs beyond schema checks - behavioral contracts, decision boundaries, cross-step propagation
|
|
99
|
+
- You need structured failure records for debugging production incidents
|
|
100
|
+
- You want to distinguish between "retry this", "flag this", and "stop everything"
|
|
101
|
+
|
|
102
|
+
Don't use it when:
|
|
103
|
+
- You only need schema extraction from LLM outputs (use Instructor or Pydantic AI)
|
|
104
|
+
- You need offline model evaluation or benchmarking (use DeepEval or RAGAS)
|
|
105
|
+
- You need content safety filtering (use a dedicated guardrails tool)
|
|
106
|
+
|
|
107
|
+
## Installation
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
pip install gateframe
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
For development:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
git clone https://github.com/practicalmind-ai/gateframe.git
|
|
117
|
+
cd gateframe
|
|
118
|
+
pip install -e ".[dev]"
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Quickstart
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from pydantic import BaseModel
|
|
125
|
+
from gateframe.core.contract import ValidationContract
|
|
126
|
+
from gateframe.rules.structural import StructuralRule
|
|
127
|
+
from gateframe.audit.log import AuditLog
|
|
128
|
+
|
|
129
|
+
# Define your expected output schema
|
|
130
|
+
class AgentResponse(BaseModel):
|
|
131
|
+
action: str
|
|
132
|
+
reasoning: str
|
|
133
|
+
|
|
134
|
+
# Create a contract with rules
|
|
135
|
+
contract = ValidationContract(
|
|
136
|
+
name="agent_response_check",
|
|
137
|
+
rules=[StructuralRule(schema=AgentResponse)],
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
# Validate LLM output
|
|
141
|
+
result = contract.validate({
|
|
142
|
+
"action": "send_email",
|
|
143
|
+
"reasoning": "User requested a follow-up email to the client.",
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
# Log the validation event
|
|
147
|
+
audit = AuditLog()
|
|
148
|
+
audit.record(result)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Running tests
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
pip install -e ".[dev]"
|
|
155
|
+
pytest -q
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
gateframe/__init__.py
|
|
5
|
+
gateframe.egg-info/PKG-INFO
|
|
6
|
+
gateframe.egg-info/SOURCES.txt
|
|
7
|
+
gateframe.egg-info/dependency_links.txt
|
|
8
|
+
gateframe.egg-info/requires.txt
|
|
9
|
+
gateframe.egg-info/top_level.txt
|
|
10
|
+
gateframe/audit/__init__.py
|
|
11
|
+
gateframe/audit/log.py
|
|
12
|
+
gateframe/core/__init__.py
|
|
13
|
+
gateframe/core/contract.py
|
|
14
|
+
gateframe/core/failure.py
|
|
15
|
+
gateframe/core/rule.py
|
|
16
|
+
gateframe/rules/__init__.py
|
|
17
|
+
gateframe/rules/structural.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
gateframe
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "gateframe"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Behavioral validation for LLM outputs in production workflows."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
keywords = ["llm", "validation", "behavioral", "workflow", "ai"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.10",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Typing :: Typed",
|
|
22
|
+
]
|
|
23
|
+
dependencies = [
|
|
24
|
+
"pydantic>=2.0,<3.0",
|
|
25
|
+
"structlog>=23.0",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
dev = [
|
|
30
|
+
"pytest>=7.0",
|
|
31
|
+
"pytest-cov>=4.0",
|
|
32
|
+
"ruff>=0.4.0",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
include = ["gateframe*"]
|
|
37
|
+
exclude = ["tests*"]
|
|
38
|
+
|
|
39
|
+
[tool.pytest.ini_options]
|
|
40
|
+
testpaths = ["tests"]
|
|
41
|
+
|
|
42
|
+
[tool.ruff]
|
|
43
|
+
target-version = "py310"
|
|
44
|
+
line-length = 100
|
|
45
|
+
|
|
46
|
+
[tool.ruff.lint]
|
|
47
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|