AttachmentMetadata
Metadata for individual file attachments.
USAGE
AttachmentMetadata(
filename,
file_type,
size_bytes,
content_type,=None,
processing_time_ms=None,
error )
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
= tb.Attachments("report.pdf", "image.png", "data.csv")
files
# Process files (happens automatically during chat)
= tb.ChatBot().provider_model("openai:gpt-4-turbo")
bot = bot.chat(files.with_prompt("Analyze these files"))
conversation
# 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
= tb.Attachments("big_report.pdf", "large_image.png")
large_files
# ... process files ...
= sum(m.processing_time_ms for m in large_files.metadata)
total_time = sum(m.size_bytes for m in large_files.metadata)
total_size
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
= tb.Attachments("good_file.pdf", "missing_file.txt", "corrupted.png")
files
# ... process files ...
# Check for errors
= [m for m in files.metadata if m.error]
failed_files = [m for m in files.metadata if not m.error]
successful_files
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}")