-
Notifications
You must be signed in to change notification settings - Fork 9
Guardrails: Added config management integration #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rkritika1508
merged 37 commits into
ProjectTech4DevAI:main
from
tattle-made:feat/guardrails-config-integration
Feb 17, 2026
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
96f295b
Added config management integration with guardrails
rkritika1508 acaf633
Added guardrails in config and version API
rkritika1508 c4abc0a
fixed integration with jobs
rkritika1508 82a93ff
removed env var
rkritika1508 63edd88
removed redundant code
rkritika1508 5c57537
fixed tests
rkritika1508 c9ea457
resolved comments
rkritika1508 155fea7
resolved comment
rkritika1508 60e0bec
updated url
rkritika1508 d49df4a
update code and fixed tests
rkritika1508 744658e
removed redundant changes
rkritika1508 f584c6d
Merge branch 'main' into feat/guardrails-config-integration
rkritika1508 11ddb58
removed redundant code
rkritika1508 67f615e
updated code
rkritika1508 c82bdb5
updated tests
rkritika1508 8b235a8
updates
rkritika1508 ea91f81
precommit
rkritika1508 c62e619
updated schema
rkritika1508 646a46f
renamed to list_validators_config
rkritika1508 a48ad89
renamed to list_validators_config
rkritika1508 43ae677
resolved comment
rkritika1508 43b97d0
Merge branch 'feat/guardrails-config-integration' of https://github.c…
rkritika1508 b9e656e
precommit
rkritika1508 48eb714
removed redundant tests
rkritika1508 7d29f21
removed tests
rkritika1508 cf1dbe8
removed tests
rkritika1508 dd2892b
removed tests
rkritika1508 0272dec
fixed tests
rkritika1508 7fe03e6
fixed tests
rkritika1508 10ea155
precommit
rkritika1508 ce03f18
updated guardrails
rkritika1508 229fba0
resolved comment
rkritika1508 6be1923
precommit
rkritika1508 069993f
added verify api
rkritika1508 99f81f3
resolved comments
rkritika1508 fc5424b
precommit
rkritika1508 a1c9346
fixed test
rkritika1508 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Verify the provided API key and return the resolved auth context. | ||
|
|
||
| This endpoint validates the `X-API-KEY` header and returns `user_id`, `organization_id`, and `project_id` for the authenticated key. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,18 @@ | |
| import httpx | ||
|
|
||
| from app.core.config import settings | ||
| from app.models.llm.request import Validator | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def call_guardrails( | ||
| input_text: str, guardrail_config: list[dict], job_id: UUID | ||
| def run_guardrails_validation( | ||
| input_text: str, | ||
| guardrail_config: list[Validator | dict[str, Any]], | ||
| job_id: UUID, | ||
| project_id: int | None, | ||
| organization_id: int | None, | ||
| suppress_pass_logs: bool = True, | ||
| ) -> dict[str, Any]: | ||
| """ | ||
| Call the Kaapi guardrails service to validate and process input text. | ||
|
|
@@ -19,14 +25,26 @@ def call_guardrails( | |
| input_text: Text to validate and process. | ||
| guardrail_config: List of validator configurations to apply. | ||
| job_id: Unique identifier for the request. | ||
| project_id: Project identifier expected by guardrails API. | ||
| organization_id: Organization identifier expected by guardrails API. | ||
| suppress_pass_logs: Whether to suppress successful validation logs in guardrails service. | ||
|
|
||
| Returns: | ||
| JSON response from the guardrails service with validation results. | ||
| """ | ||
| validators = [ | ||
| validator.model_dump(mode="json") | ||
| if isinstance(validator, Validator) | ||
| else validator | ||
| for validator in guardrail_config | ||
| ] | ||
|
|
||
| payload = { | ||
| "request_id": str(job_id), | ||
| "project_id": project_id, | ||
| "organization_id": organization_id, | ||
| "input": input_text, | ||
| "validators": guardrail_config, | ||
| "validators": validators, | ||
| } | ||
|
|
||
| headers = { | ||
|
|
@@ -38,16 +56,17 @@ def call_guardrails( | |
| try: | ||
| with httpx.Client(timeout=10.0) as client: | ||
| response = client.post( | ||
| settings.KAAPI_GUARDRAILS_URL, | ||
| f"{settings.KAAPI_GUARDRAILS_URL}/", | ||
| json=payload, | ||
| params={"suppress_pass_logs": str(suppress_pass_logs).lower()}, | ||
| headers=headers, | ||
| ) | ||
|
|
||
| response.raise_for_status() | ||
| return response.json() | ||
| except Exception as e: | ||
| logger.warning( | ||
| f"[call_guardrails] Service unavailable. Bypassing guardrails. job_id={job_id}. error={e}" | ||
| f"[run_guardrails_validation] Service unavailable. Bypassing guardrails. job_id={job_id}. error={e}" | ||
| ) | ||
|
|
||
| return { | ||
|
|
@@ -58,3 +77,93 @@ def call_guardrails( | |
| "rephrase_needed": False, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def list_validators_config( | ||
| organization_id: int | None, | ||
| project_id: int | None, | ||
| input_validator_configs: list[Validator] | None, | ||
| output_validator_configs: list[Validator] | None, | ||
| ) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]: | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Fetch validator configurations by IDs for input and output guardrails. | ||
|
|
||
| Calls: | ||
| GET /validators/configs/?organization_id={organization_id}&project_id={project_id}&ids={uuid} | ||
| """ | ||
| input_validator_config_ids = [ | ||
| validator_config.validator_config_id | ||
| for validator_config in (input_validator_configs or []) | ||
| ] | ||
| output_validator_config_ids = [ | ||
| validator_config.validator_config_id | ||
| for validator_config in (output_validator_configs or []) | ||
| ] | ||
|
|
||
| if not input_validator_config_ids and not output_validator_config_ids: | ||
| return [], [] | ||
|
|
||
| headers = { | ||
| "accept": "application/json", | ||
| "Authorization": f"Bearer {settings.KAAPI_GUARDRAILS_AUTH}", | ||
| "Content-Type": "application/json", | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| endpoint = f"{settings.KAAPI_GUARDRAILS_URL}/validators/configs/" | ||
|
|
||
| def _build_params(validator_ids: list[UUID]) -> dict[str, Any]: | ||
|
Comment on lines
112
to
114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing blank line before nested function definition (Black violation). The CI pipeline reports that Black formatting changed this file. The missing blank line between the Proposed fix endpoint = f"{settings.KAAPI_GUARDRAILS_URL}/validators/configs/"
+
def _build_params(validator_ids: list[UUID]) -> dict[str, Any]:🤖 Prompt for AI Agents |
||
| params = { | ||
| "organization_id": organization_id, | ||
| "project_id": project_id, | ||
| "ids": [str(validator_config_id) for validator_config_id in validator_ids], | ||
| } | ||
| return {key: value for key, value in params.items() if value is not None} | ||
|
|
||
| try: | ||
| with httpx.Client(timeout=10.0) as client: | ||
|
|
||
| def _fetch_by_ids(validator_ids: list[UUID]) -> list[dict[str, Any]]: | ||
| if not validator_ids: | ||
| return [] | ||
|
|
||
| response = client.get( | ||
| endpoint, | ||
| params=_build_params(validator_ids), | ||
| headers=headers, | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| payload = response.json() | ||
| if not isinstance(payload, dict): | ||
| raise ValueError( | ||
| "Invalid validators response format: expected JSON object." | ||
| ) | ||
|
|
||
| if not payload.get("success", False): | ||
| raise ValueError( | ||
| "Validator config fetch failed: `success` is false." | ||
| ) | ||
|
|
||
| validators = payload.get("data", []) | ||
| if not isinstance(validators, list): | ||
| raise ValueError( | ||
| "Invalid validators response format: `data` must be a list." | ||
| ) | ||
|
|
||
| return [ | ||
| validator for validator in validators if isinstance(validator, dict) | ||
| ] | ||
|
|
||
| input_guardrails = _fetch_by_ids(input_validator_config_ids) | ||
| output_guardrails = _fetch_by_ids(output_validator_config_ids) | ||
| return input_guardrails, output_guardrails | ||
|
|
||
| except Exception as e: | ||
| logger.warning( | ||
| "[list_validators_config] Guardrails service unavailable or invalid response. " | ||
| "Proceeding without input/output guardrails. " | ||
| f"input_validator_config_ids={input_validator_config_ids}, output_validator_config_ids={output_validator_config_ids}, " | ||
| f"organization_id={organization_id}, " | ||
| f"project_id={project_id}, endpoint={endpoint}, error={e}" | ||
| ) | ||
| return [], [] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.