[Refactor] Extract helper methods in guardrail handlers to fix PLR0915#24802
[Refactor] Extract helper methods in guardrail handlers to fix PLR0915#24802yuneng-berri merged 1 commit intomainfrom
Conversation
Ruff flagged `process_output_streaming_response` (A2A) and `process_output_response` (Anthropic) for exceeding 50 statements. Extract inline logic into private helpers to bring both under the limit. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
|
Greptile SummaryThis PR is a pure mechanical refactoring of two guardrail handler files to reduce statement counts and satisfy the Ruff PLR0915 linter rule. No behavior changes are introduced — all extracted helper methods preserve the original logic verbatim. Key changes:
Confidence Score: 5/5Safe to merge — purely mechanical code organisation with no behavioral changes. All changes are straight-line extractions; logic paths in both files are preserved exactly. No tests were weakened or removed, no new functionality was introduced, and the linter violation is clearly addressed. All remaining observations are at the P2 style level at most. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/a2a/chat/guardrail_translation/handler.py | Extracts _parse_streaming_responses and _collect_text_from_parsed_chunks from process_output_streaming_response; logic is behaviorally identical to the original. |
| litellm/llms/anthropic/chat/guardrail_translation/handler.py | Extracts four private helpers (_prepare_request_data, _get_response_content, _extract_from_content_blocks, _build_guardrail_inputs) from process_output_response; all logic is preserved verbatim. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
subgraph A2A["A2AGuardrailHandler"]
A[process_output_streaming_response] --> B[_parse_streaming_responses\nnew helper]
A --> C[_collect_text_from_parsed_chunks\nnew helper]
C --> D[extract_text_from_a2a_response\nlazy import]
A --> E[guardrail_to_apply.apply_guardrail]
A --> F[_apply_text_to_path]
end
subgraph Anthropic["AnthropicMessagesHandler"]
G[process_output_response] --> H[_get_response_content\nnew static helper]
G --> I[_extract_from_content_blocks\nnew helper]
I --> J[_extract_output_text_and_images]
G --> K[_prepare_request_data\nnew helper]
G --> L[_build_guardrail_inputs\nnew static helper]
G --> M[guardrail_to_apply.apply_guardrail]
G --> N[_apply_guardrail_responses_to_output]
end
Reviews (1): Last reviewed commit: "[Refactor] Extract helper methods in gua..." | Re-trigger Greptile
Summary
Failure Path (Before Fix)
CI failed with Ruff PLR0915 (too many statements > 50) on two guardrail handler methods:
A2AGuardrailHandler.process_output_streaming_response(54 statements)AnthropicMessagesHandler.process_output_response(54 statements)Fix
Extract inline logic into private helper methods to bring both methods under the 50-statement limit. Pure mechanical extraction with no behavior changes.
A2A handler:
_parse_streaming_responses()— parses JSON-RPC items from responses_collect_text_from_parsed_chunks()— collects text usingextract_text_from_a2a_responseAnthropic handler:
_prepare_request_data()— ensures request_data has the response key and metadata_get_response_content()— extracts content list from dict or object response_extract_from_content_blocks()— iterates content blocks to extract text/images/tool calls_build_guardrail_inputs()— buildsGenericGuardrailAPIInputswith optional fieldsTesting
ruff check --select PLR0915locally — both files passruff checkon both files — all checks passedType
🧹 Refactoring