feat: add standalone guardrail methods and enforce strict model validation#189
feat: add standalone guardrail methods and enforce strict model validation#189abhijitjavelin merged 6 commits intomainfrom
Conversation
There was a problem hiding this comment.
Hello @abhijitjavelin, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request introduces standalone guardrail methods to the Javelin SDK and enforces stricter model validation. It adds a new GuardrailsService with methods for applying trust/safety and prompt injection detection, as well as a general apply_guardrails method for applying multiple guardrails at once. It also includes a method for listing available guardrails. The changes also include updates to the Request model to support guardrail-related parameters and modifications to the ProviderService, RouteService, SecretService, and TemplateService to enforce model validation when creating or updating resources.
Highlights
- Guardrails Service: A new
GuardrailsServiceis added, providing methods to apply trust/safety checks, prompt injection detection, and a general guardrails application, along with a method to list available guardrails. - Standalone Guardrail Methods: Standalone methods
apply_trustsafety,apply_promptinjectiondetection,apply_guardrails, andlist_guardrailsare added to theJavelinClientfor easier access to guardrail functionalities. - Model Validation: Model validation is enforced in
ProviderService,RouteService,SecretService, andTemplateServicewhen creating or updating resources, ensuring that input data conforms to the expected model schema. - Request Model Updates: The
Requestmodel is updated to include parameters for guardrail-related requests, such as specifying a guardrail to apply or listing available guardrails.
Changelog
Click here to see the changelog
- examples/guardrails/test_guardrails.py
- A new example file is added to demonstrate the usage of the new guardrail methods, including applying trust/safety checks, prompt injection detection, applying multiple guardrails, and listing available guardrails.
- javelin_sdk/client.py
- The
GuardrailsServiceis imported and initialized within theJavelinClient. - The
_construct_urlmethod is updated to handle guardrail-related endpoints. - Standalone guardrail methods (
apply_trustsafety,apply_promptinjectiondetection,apply_guardrails,list_guardrails) are added to theJavelinClientas lambda functions calling the corresponding methods inGuardrailsService. - The
set_headersmethod is updated to include the new guardrail methods.
- The
- javelin_sdk/models.py
- The
RouteConfigmodel is modified to remove thebudgetfield (line 145). - The
Requestmodel is updated to includeguardrailandlist_guardrailsparameters (lines 483, 484, 502, 503).
- The
- javelin_sdk/services/guardrails_service.py
- A new
GuardrailsServiceis created, containing methods for applying trust/safety checks, prompt injection detection, applying multiple guardrails, and listing available guardrails. - Includes error handling for guardrail responses.
- A new
- javelin_sdk/services/provider_service.py
- Model validation is enforced in
create_provider,acreate_provider,update_provider, andaupdate_providermethods by usingProvider.model_validateto ensure the input data conforms to theProvidermodel schema (lines 63, 75, 126, 136).
- Model validation is enforced in
- javelin_sdk/services/route_service.py
- Model validation is enforced in
create_route,acreate_route,update_route, andaupdate_routemethods by usingRoute.model_validateto ensure the input data conforms to theRoutemodel schema (lines 67, 76, 125, 135).
- Model validation is enforced in
- javelin_sdk/services/secret_service.py
- Model validation is enforced in
create_secret,acreate_secret,update_secret, andaupdate_secretmethods by usingSecret.model_validateto ensure the input data conforms to theSecretmodel schema (lines 46, 54, 101, 138). - Restricted fields are checked during secret updates to prevent modification of
api_key.
- Model validation is enforced in
- javelin_sdk/services/template_service.py
- Model validation is enforced in
create_template,acreate_template,update_template, andaupdate_templatemethods by usingTemplate.model_validateto ensure the input data conforms to theTemplatemodel schema (lines 46, 57, 106, 115).
- Model validation is enforced in
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
A new guardrail's in place,
Protecting text with grace,
Injection's denied,
Trust and safety applied,
Making the digital space safe.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
The pull request introduces standalone guardrail methods and enforces strict model validation. The addition of guardrail functionalities is a valuable enhancement to the Javelin SDK. However, there are a few areas that could benefit from further refinement to ensure robustness and maintainability.
Summary of Findings
- Missing Error Handling in Guardrails Service: The
_handle_guardrails_responsemethod inGuardrailsServiceonly handles specific HTTP error codes (400, 401, 403, 429) and raises exceptions. It should also include a generic error handling for other 4xx errors to provide more informative feedback to the user. - Inconsistent Validation Approach: The
create_provider,acreate_provider,update_provider,aupdate_provider,create_route,acreate_route,update_route,aupdate_route,create_secret,acreate_secret,update_secret,aupdate_secret,create_template,acreate_template,update_template, andaupdate_templatemethods in various services now accept bothdictand Model instances, and usemodel_validatefordictinputs. This approach is good, but should be applied consistently across all similar methods to ensure a uniform API. - Duplicated Guardrails Methods: The guardrails methods (
apply_trustsafety,apply_promptinjectiondetection,apply_guardrails,list_guardrails) are defined twice inJavelinClient, once within the class and again within theset_headersmethod. This duplication should be removed to avoid confusion and potential maintenance issues.
Merge Readiness
The pull request introduces important functionality, but there are some issues that should be addressed before merging. Specifically, the error handling in GuardrailsService should be improved, and the duplicated guardrails methods in JavelinClient should be removed. I am unable to approve this pull request, and recommend that it not be merged until these issues are addressed. Users should have others review and approve this code before merging.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…m:getjavelin/javelin-python into feat_standalone_guardrails
No description provided.