push behavior header

This commit is contained in:
Luca Beurer-Kellner
2025-04-08 10:49:39 +02:00
parent c92ae4c442
commit 9d7dbb1bd5
5 changed files with 15 additions and 1 deletions
+1 -1
View File
@@ -339,7 +339,7 @@ Then, this combined key can be passed as `Authorization` header only.
- `Invariant-Guardrail-Service-Authorization`: Additional header value to specify a different API key to use for Guardrails evaluation specifically. `Invariant-Authorization` will then only be used to interact with the configured Explorer instance, not for guardrailing.
- `Invariant-Guardrails`: Guardrailing rules to be checked for a specific request. This list of rules will replace all rules being evaluated, i.e. no additional rules will be pulled from [Explorer](https://explorer.invariantlabs.ai).
- `Invariant-NoPush`: Disables pushing to Explorer alltogether. Gateway will still pull and evaluate the guardrailing rules stored in Explorer, but it will no longer push the resulting trace.
- `Invariant-Push`: Configures push behavior of Gateway. Valid values are `push` (default, pushes to Explorer), `skip` (does not push anything)
---
+11
View File
@@ -24,7 +24,10 @@ class RequestContext:
guardrail_authorization: Optional[str] = None
# the set of guardrails to enforce for this request
guardrails: Optional[GuardrailRuleSet] = None
# configuration parameters for this request
config: Dict[str, Any] = None
# push behavior
push_behavior: str = 'push'
_created_via_factory: bool = field(
default=False, init=True, repr=False, compare=False
@@ -55,6 +58,13 @@ class RequestContext:
if key != "guardrails_from_file"
}
# Read potential Invariant-NoPush header
push_behavior = 'push'
if push_behavior := request.headers.get("Invariant-Push"):
if not push_behavior.lower() in ["push", "skip"]:
raise fastapi.HTTPException(status_code=400, detail="Invalid value for Invariant-Push header. Valid values are 'push' and 'skip'.")
push_behavior = push_behavior.lower()
# If no guardrails are configured and the config specifies
# guardrails_from_file, use those instead.
if (
@@ -95,6 +105,7 @@ class RequestContext:
guardrail_authorization=guardrail_service_authorization,
guardrails=guardrails,
config=context_config,
push_behavior=push_behavior,
_created_via_factory=True,
)
+1
View File
@@ -196,6 +196,7 @@ async def push_to_explorer(
invariant_authorization=context.invariant_authorization,
metadata=[create_metadata(context, merged_response)],
annotations=[annotations] if annotations else None,
push_behavior=context.push_behavior,
)
+1
View File
@@ -437,6 +437,7 @@ async def push_to_explorer(
invariant_authorization=context.invariant_authorization,
metadata=[create_metadata(context, response_json)],
annotations=[annotations] if annotations else None,
push_behavior=context.push_behavior,
)
+1
View File
@@ -520,6 +520,7 @@ async def push_to_explorer(
messages=[messages],
annotations=[annotations],
metadata=[create_metadata(context, merged_response)],
push_behavior=context.push_behavior,
)