AttachmentMetadata
Metadata for individual file attachments.
USAGE
AttachmentMetadata(
filename,
file_type,
size_bytes,
content_type,
processing_time_ms=None,
error=None,
)This class captures essential information about file processing results, enabling debugging, performance monitoring, and analytics for file attachment workflows.
The metadata is automatically collected during file processing and can be accessed via the Attachments.metadata property for inspection and logging.
Parameters
filename : str-
The name of the file (without path).
file_type : str-
File extension without the dot (e.g., ‘pdf’, ‘png’, ‘py’).
size_bytes : int-
File size in bytes.
content_type : str-
Category of content: ‘image’, ‘pdf’, ‘text’, ‘error’, ‘unsupported’.
processing_time_ms : Optional[float] = None-
Time taken to process the file in milliseconds.
error : Optional[str] = None-
Error message if processing failed.
Examples
Accessing File Metadata
import talk_box as tb
files = tb.Attachments("report.pdf", "image.png", "data.csv")
# Process files (happens automatically during chat)
bot = tb.ChatBot().provider_model("openai:gpt-4-turbo")
conversation = bot.chat(files.with_prompt("Analyze these files"))
# Inspect metadata
for meta in files.metadata:
print(f"File: {meta.filename}")
print(f"Type: {meta.content_type}")
print(f"Size: {meta.size_bytes:,} bytes")
print(f"Processing time: {meta.processing_time_ms:.1f}ms")
if meta.error:
print(f"Error: {meta.error}")
print("---")Performance Monitoring
# Monitor processing performance for optimization
large_files = tb.Attachments("big_report.pdf", "large_image.png")
# ... process files ...
total_time = sum(m.processing_time_ms for m in large_files.metadata)
total_size = sum(m.size_bytes for m in large_files.metadata)
print(f"Processed {len(large_files.metadata)} files")
print(f"Total size: {total_size:,} bytes")
print(f"Total time: {total_time:.1f}ms")
print(f"Avg speed: {total_size/total_time*1000:.0f} bytes/sec")Error Detection and Handling
files = tb.Attachments("good_file.pdf", "missing_file.txt", "corrupted.png")
# ... process files ...
# Check for errors
failed_files = [m for m in files.metadata if m.error]
successful_files = [m for m in files.metadata if not m.error]
print(f"Successfully processed: {len(successful_files)} files")
if failed_files:
print("Failed files:")
for meta in failed_files:
print(f" {meta.filename}: {meta.error}")