Skip to content

[Refactor] Extract helper methods in guardrail handlers to fix PLR0915#24802

Merged
yuneng-berri merged 1 commit intomainfrom
litellm_/modest-easley
Mar 30, 2026
Merged

[Refactor] Extract helper methods in guardrail handlers to fix PLR0915#24802
yuneng-berri merged 1 commit intomainfrom
litellm_/modest-easley

Conversation

@yuneng-berri
Copy link
Copy Markdown
Collaborator

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 using extract_text_from_a2a_response

Anthropic 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() — builds GenericGuardrailAPIInputs with optional fields

Testing

  • Ran ruff check --select PLR0915 locally — both files pass
  • Ran full ruff check on both files — all checks passed

Type

🧹 Refactoring

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]>
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 30, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Mar 30, 2026 5:43pm

Request Review

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq bot commented Mar 30, 2026

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing litellm_/modest-easley (92e7aaa) with main (a278933)

Open in CodSpeed

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 30, 2026

Greptile Summary

This 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:

  • A2AGuardrailHandler.process_output_streaming_response: inline parsing and text-collection logic extracted into _parse_streaming_responses() and _collect_text_from_parsed_chunks().
  • AnthropicMessagesHandler.process_output_response: inline request-data setup, content extraction, and inputs-building logic extracted into _prepare_request_data(), _get_response_content(), _extract_from_content_blocks(), and _build_guardrail_inputs().
  • The extract_text_from_a2a_response import was moved from process_output_streaming_response into the new _collect_text_from_parsed_chunks helper; this lazy-import pattern is consistent with how the import was used before and causes no functional difference.
  • No tests were modified or removed, and existing tests for AnthropicMessagesHandler continue to cover the refactored paths.

Confidence Score: 5/5

Safe 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.

Important Files Changed

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
Loading

Reviews (1): Last reviewed commit: "[Refactor] Extract helper methods in gua..." | Re-trigger Greptile

@yuneng-berri yuneng-berri merged commit 5cec43c into main Mar 30, 2026
79 of 118 checks passed
@yuneng-berri yuneng-berri deleted the litellm_/modest-easley branch March 30, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants