class HybridResponse
A dataclass that encapsulates a complete hybrid response containing both text content and graphical elements with their placeholders and metadata.
/tf/active/vicechatdev/e-ink-llm/hybrid_response_handler.py
32 - 37
simple
Purpose
HybridResponse serves as a data container for responses that combine textual information with graphics. It stores the text content, graphic specifications, placeholder information for where graphics should be inserted, and additional metadata. This class is typically used in systems that generate documents or reports with embedded visualizations, allowing for structured representation of mixed-content responses before rendering to formats like PDF.
Source Code
class HybridResponse:
"""Complete hybrid response with text and graphics"""
text_content: str
graphics: List[GraphicSpec]
placeholders: List[GraphicPlaceholder]
metadata: Dict[str, Any]
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
text_content: A string containing the textual portion of the response. This is the main text that may contain references or placeholders for graphics.
graphics: A list of GraphicSpec objects that define the specifications for each graphic element to be generated or included in the response. Each GraphicSpec contains details about the type, data, and rendering parameters for a graphic.
placeholders: A list of GraphicPlaceholder objects that indicate where in the text_content each graphic should be positioned. These placeholders map graphics to their intended locations in the final rendered output.
metadata: A dictionary containing arbitrary key-value pairs for additional information about the response, such as generation timestamps, source information, processing parameters, or any other contextual data.
Return Value
As a dataclass, instantiation returns a HybridResponse object with the four specified attributes initialized. The object serves as an immutable-by-convention data structure that can be passed between components for processing, rendering, or serialization.
Class Interface
Methods
__init__(text_content: str, graphics: List[GraphicSpec], placeholders: List[GraphicPlaceholder], metadata: Dict[str, Any]) -> None
Purpose: Initializes a new HybridResponse instance with text content, graphics, placeholders, and metadata. Auto-generated by dataclass decorator.
Parameters:
text_content: The textual content of the responsegraphics: List of GraphicSpec objects defining the graphics to includeplaceholders: List of GraphicPlaceholder objects indicating where graphics should be placedmetadata: Dictionary of additional metadata about the response
Returns: None - initializes the instance
__repr__() -> str
Purpose: Returns a string representation of the HybridResponse instance. Auto-generated by dataclass decorator.
Returns: String representation showing all field values
__eq__(other: object) -> bool
Purpose: Compares two HybridResponse instances for equality based on all fields. Auto-generated by dataclass decorator.
Parameters:
other: Another object to compare with
Returns: True if all fields are equal, False otherwise
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
text_content |
str | The textual content of the hybrid response, potentially containing placeholder references for graphics | instance |
graphics |
List[GraphicSpec] | List of graphic specifications defining the visual elements to be included in the response | instance |
placeholders |
List[GraphicPlaceholder] | List of placeholder objects that map graphics to their intended positions in the text content | instance |
metadata |
Dict[str, Any] | Dictionary containing additional metadata and contextual information about the response | instance |
Dependencies
dataclassestyping
Required Imports
from dataclasses import dataclass
from typing import Dict, Any, List
from graphics_generator import GraphicSpec, GraphicPlaceholder
Usage Example
from dataclasses import dataclass
from typing import Dict, Any, List
from graphics_generator import GraphicSpec, GraphicPlaceholder, GraphicType
# Create graphic specifications
graphic1 = GraphicSpec(type=GraphicType.BAR_CHART, data={'values': [1, 2, 3]}, title='Sample Chart')
# Create placeholders
placeholder1 = GraphicPlaceholder(id='chart1', position=100)
# Instantiate HybridResponse
response = HybridResponse(
text_content='Here is the analysis: [GRAPHIC:chart1] shows the results.',
graphics=[graphic1],
placeholders=[placeholder1],
metadata={'generated_at': '2024-01-01', 'version': '1.0'}
)
# Access attributes
print(response.text_content)
print(f'Number of graphics: {len(response.graphics)}')
print(f'Metadata: {response.metadata}')
Best Practices
- This is a dataclass, so it automatically generates __init__, __repr__, and __eq__ methods. Use it as an immutable data container.
- Ensure that the number and IDs of placeholders correspond correctly to the graphics list to maintain proper mapping.
- The metadata dictionary should be used for non-structural information that doesn't fit into the other fields.
- When creating instances, ensure all required fields are provided as dataclasses require all fields without defaults to be specified.
- Consider using frozen=True in the dataclass decorator if immutability is desired: @dataclass(frozen=True).
- The placeholders should reference positions or identifiers in the text_content where graphics will be inserted during rendering.
- Validate that GraphicSpec objects in the graphics list are properly configured before creating a HybridResponse instance.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class HybridResponseHandler 77.1% similar
-
function demo_hybrid_response 72.2% similar
-
class GraphicPlaceholder 68.7% similar
-
class HybridPDFGenerator 67.2% similar
-
class HybridPromptEnhancer 66.1% similar