Principle:Googleapis Python genai Response Processing
| Knowledge Sources | |
|---|---|
| Domains | NLP, Data_Extraction |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
A structured approach to extracting and interpreting generated content from language model inference responses.
Description
Response Processing is the final step in any generation pipeline. Model responses contain multiple layers of information: the generated text, individual content parts, candidate alternatives, usage metadata (token counts, latency), safety ratings, and optional function call requests. Effective response processing extracts the relevant data for the application's needs, handles edge cases (empty responses, blocked content, multiple candidates), and provides downstream consumers with clean, typed data.
Usage
Apply response processing after every generation call. For simple text extraction, use the convenience .text property. For applications that need structured data, iterate over .parts. For function-calling workflows, check .function_calls. For monitoring and billing, access .usage_metadata. Always handle the case where properties may return None (e.g., when content is blocked by safety filters).
Theoretical Basis
Model responses follow a Candidate-Content-Part hierarchy:
# Response structure (pseudo-code)
Response
└── candidates: list[Candidate]
└── content: Content
└── parts: list[Part]
├── text: str
├── function_call: FunctionCall
└── inline_data: Blob
└── finish_reason: FinishReason
└── safety_ratings: list[SafetyRating]
└── usage_metadata: UsageMetadata
├── prompt_token_count: int
├── candidates_token_count: int
└── total_token_count: int
Convenience properties (.text, .parts, .function_calls) shortcut into the first candidate for the common single-candidate case.