Repository navigation
feat(guardrails): expose streaming knobs on generic_guardrail_api #31730
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
Changes from all commits
5313818
069c79d
3a20cd7
572acf0
647a0c6
2b30215
7f07fc3
2b464fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| from typing import TYPE_CHECKING | ||
| from typing import TYPE_CHECKING, Any, Optional | ||
|
|
||
| from litellm.types.guardrails import SupportedGuardrailIntegrations | ||
|
|
||
|
|
@@ -8,9 +8,23 @@ | |
| from litellm.types.guardrails import Guardrail, LitellmParams | ||
|
|
||
|
|
||
| def _get_config_value(litellm_params: Any, optional_params: Any, attribute_name: str) -> Optional[Any]: | ||
|
Comment on lines
1
to
+11
Contributor
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.
Rule Used: In this repo, prefer Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| if optional_params is not None: | ||
| value = ( | ||
| optional_params.get(attribute_name) | ||
| if isinstance(optional_params, dict) | ||
| else getattr(optional_params, attribute_name, None) | ||
| ) | ||
| if value is not None: | ||
| return value | ||
| return getattr(litellm_params, attribute_name, None) | ||
|
|
||
|
|
||
| def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"): | ||
| import litellm | ||
|
|
||
| optional_params = getattr(litellm_params, "optional_params", None) | ||
|
|
||
| _generic_guardrail_api_callback = GenericGuardrailAPI( | ||
| api_base=litellm_params.api_base, | ||
| api_key=litellm_params.api_key, | ||
|
|
@@ -22,6 +36,8 @@ def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail" | |
| guardrail_name=guardrail.get("guardrail_name", ""), | ||
| event_hook=litellm_params.mode, | ||
| default_on=litellm_params.default_on, | ||
| streaming_end_of_stream_only=_get_config_value(litellm_params, optional_params, "streaming_end_of_stream_only"), | ||
| streaming_sampling_rate=_get_config_value(litellm_params, optional_params, "streaming_sampling_rate"), | ||
| ) | ||
|
|
||
| litellm.logging_callback_manager.add_litellm_callback(_generic_guardrail_api_callback) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_get_config_valueadds broadAnyannotations for untyped config shapes, which increases the static typing budget this repo tracks. The project guidance prefersobjectfor broad parameters because it still acceptsNonewithout addingAny; this helper can keep the same runtime behavior by typing the inputs and return asobject.Rule Used: In this repo, prefer
objectoverOptional[Any]... (source)Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!