Overview
Concrete backend-independent dataclass representing a model completion response, normalizing LiteLLM, OpenAI-style, and dict/mapping responses into a uniform shape with text, reasoning, tool calls, usage, and error fields.
Description
ModelResponse is a dataclass that captures everything a model completion returns regardless of backend: text, reasoning_content, tool_calls, usage, finish_reason, model, metadata, error, and guardrail. The class provides three factory classmethods: from_value() dispatches to the right normalizer based on the input type; from_litellm() normalizes LiteLLM ModelResponse objects (extracting choices, hidden params, response headers); and failed() constructs an error response from an exception. The ok property returns True when there is no error and no guardrail block.
Usage
- Model backends: Every model adapter wraps its provider response in ModelResponse.from_value().
- Attack techniques: Attacks read response.text and check response.ok to decide next steps.
- Error handling: Use ModelResponse.failed(exc) to represent provider failures uniformly.
Code Reference
Source Location
Signature
@dataclass
class ModelResponse:
text: str
raw_response: Any = None
reasoning_content: Optional[str] = None
tool_calls: list[Dict[str, Any]] = field(default_factory=list)
usage: Optional[Dict[str, Any]] = None
finish_reason: Optional[str] = None
model: Optional[str] = None
response_id: Optional[str] = None
created: Optional[int] = None
system_fingerprint: Optional[str] = None
metadata: Dict[str, Any] = field(default_factory=dict)
error: Optional[LLMError] = None
guardrail: Optional[GuardrailInfo] = None
latency_ms: Optional[int] = None
@property
def ok(self) -> bool: ...
@classmethod
def failed(cls, error: Exception) -> "ModelResponse": ...
@property
def reasoning(self) -> Optional[str]: ...
@classmethod
def from_value(cls, value: Any) -> "ModelResponse": ...
@classmethod
def from_litellm(cls, value: Any) -> "ModelResponse": ...
Import
from hackagent.models.response import ModelResponse
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| value |
Any |
Yes |
LiteLLM response, OpenAI-style object, dict, or string
|
Outputs
| Name |
Type |
Description
|
| return |
ModelResponse |
Normalized response dataclass
|
| .ok |
bool |
True if no error and no guardrail block
|
| .text |
str |
Extracted completion text
|
| .reasoning |
Optional[str] |
Reasoning content (backend-independent name)
|
Usage Examples
Normalize a LiteLLM Response
from hackagent.models.response import ModelResponse
import litellm
resp = litellm.completion(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hi"}])
normalized = ModelResponse.from_value(resp)
print(normalized.text, normalized.ok, normalized.usage)
Represent a Provider Failure
from hackagent.models.response import ModelResponse
try:
# ... call provider ...
pass
except Exception as exc:
response = ModelResponse.failed(exc)
print(response.ok) # False
print(response.error.category) # Exception class name
Access Reasoning Content
from hackagent.models.response import ModelResponse
normalized = ModelResponse.from_value(litellm_response)
if normalized.reasoning:
print("Model reasoning:", normalized.reasoning)
Related Pages
Implements Principle