Repository navigation
fix(generic_guardrail_api): return tool_calls on the guardrail response #41013
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
0b19b55
f0f06ae
47cf317
f4d92fb
e5cab21
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 |
|---|---|---|
|
|
@@ -336,6 +336,16 @@ def _build_request_headers(self) -> dict: | |
| headers.update(self.headers) | ||
| return headers | ||
|
|
||
| @staticmethod | ||
| def _echo_sent_tool_calls( | ||
| guardrailed: GenericGuardrailAPIInputs, *, sent: GenericGuardrailAPIInputs | ||
| ) -> GenericGuardrailAPIInputs: | ||
| """Keep the tool calls the guardrail did not return, so the handlers do not report them missing.""" | ||
| sent_tool_calls: Final = sent.get("tool_calls") | ||
| if not sent_tool_calls or "tool_calls" in guardrailed: | ||
| return guardrailed | ||
| return {**guardrailed, "tool_calls": sent_tool_calls} | ||
|
|
||
| def _build_guardrail_return_inputs( | ||
| self, | ||
| *, | ||
|
|
@@ -365,6 +375,8 @@ def _build_guardrail_return_inputs( | |
| ) | ||
| if rows_to_write_back is not None: | ||
| return_inputs["structured_messages"] = list(rows_to_write_back) | ||
| if guardrail_response.tool_calls: | ||
| return_inputs["tool_calls"] = list(guardrail_response.tool_calls) | ||
|
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. Medium: Multi-choice streams bypass text redaction In a post-call policy pipeline using this guardrail, a caller can request Before forwarding these rewrites on this path, implement per-choice tool-call delivery or make unsupported rewrites fail closed rather than releasing the original stream.
Contributor
Author
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. Leaving this: outside pipelines it fails closed, and inside them the executor rolls back every guardrail's undeliverable rewrite. Happy to change the executor separately |
||
| if guardrail_response.stream_holdback_chars is not None: | ||
| return_inputs["stream_holdback_chars"] = guardrail_response.stream_holdback_chars | ||
| return return_inputs | ||
|
|
@@ -499,13 +511,16 @@ async def apply_guardrail( | |
| blocked_content=True, | ||
| ) | ||
|
|
||
| return self._build_guardrail_return_inputs( | ||
| texts=texts, | ||
| images=images, | ||
| tools=tools, | ||
| structured_messages=structured_messages, | ||
| shown_messages=guardrail_request.structured_messages, | ||
| guardrail_response=guardrail_response, | ||
| return self._echo_sent_tool_calls( | ||
| self._build_guardrail_return_inputs( | ||
| texts=texts, | ||
| images=images, | ||
| tools=tools, | ||
| structured_messages=structured_messages, | ||
| shown_messages=guardrail_request.structured_messages, | ||
| guardrail_response=guardrail_response, | ||
| ), | ||
| sent=inputs, | ||
| ) | ||
|
|
||
| except GuardrailRaisedException: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.