Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AISecurityLab Hackagent Response Helpers

From Leeroopedia
Knowledge Sources
Domains Security_Testing, Contracts
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete helper functions for extracting text content and guardrail metadata from model responses across multiple formats (Completion, OpenAI-style, dict, string), provided by the HackAgent attacks package.

Description

'extract_response_content() normalizes a model response — whether a Completion contract, an OpenAI-style object with choices, a legacy dict with generated_text, or a plain string — into a single Optional[str] text value. is_guardrail_response() checks whether a response was blocked by a guardrail (via the adapter_type == "guardrail" marker on dicts, or Completion.blocked). get_guardrail_info() extracts the guardrail metadata dict from a blocked response.

Usage

  • Attack techniques: Techniques call extract_response_content() to get text from heterogeneous model responses.
  • Guardrail detection: Use is_guardrail_response() and get_guardrail_info() to handle guardrail blocks.

Code Reference

Source Location

Signature

def extract_response_content(
    response: Any,
    logger: Optional[logging.Logger] = None,
) -> Optional[str]:
    """Extract text content from a Completion or legacy LLM response."""

def is_guardrail_response(response: Any) -> bool:
    """Return True if response is a guardrail-blocked response."""

def get_guardrail_info(response: Any) -> Dict[str, Any]:
    """Extract guardrail metadata from a blocked response."""

Import

from hackagent.attacks._lib.response import (
    extract_response_content, is_guardrail_response, get_guardrail_info,
)

I/O Contract

Inputs

Name Type Required Description
response Any Yes Completion, OpenAI-style object, dict, or string
logger Logger No Custom logger (defaults to module logger)

Outputs

Name Type Description
extract_response_content() Optional[str] Extracted text or None
is_guardrail_response() bool True if blocked by guardrail
get_guardrail_info() Dict[str, Any] Guardrail metadata dict

Usage Examples

Extract Text from a Completion

from hackagent.attacks._lib.response import extract_response_content

# Works with Completion, OpenAI-style, dict, or string
text = extract_response_content(model_response)
if text:
    print(f"Model said: {text}")

Check for Guardrail Block

from hackagent.attacks._lib.response import is_guardrail_response, get_guardrail_info

if is_guardrail_response(model_response):
    info = get_guardrail_info(model_response)
    print(f"Blocked by guardrail: {info.get('message', '')}")

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment