Upload files to use with the Incredible agentic model. This system uses pre-signed upload URLs for fast, direct client-to-storage uploads that bypass your server, then automatically processes files to extract metadata and content.
Best Practice: Upload Once, Reuse ForeverUpload files once during setup or when users first add them. Store the file_id and reuse it across unlimited chat requests. Avoid re-uploading the same file repeatedly—this wastes time and resources.
The Python SDK provides a simple one-line upload method that handles all the steps automatically:
Copy
from incredible_python import Incredibleclient = Incredible(api_key="YOUR_API_KEY")# Upload a file - SDK handles everything automaticallywith open("report.pdf", "rb") as f: file = client.files.upload(file=f)# Use the file_id in any endpointprint(f"File uploaded! ID: {file.file_id}")# Now use it with Agent, Conversation, or Answer endpointsresponse = client.agent( messages=[{"role": "user", "content": "Summarize this", "file_ids": [file.file_id]}], tools=[...])
from incredible_python import Incredibleclient = Incredible(api_key="YOUR_API_KEY")# ============================================# Upload with SDK - One line, handles everything!# ============================================with open("report.pdf", "rb") as f: file = client.files.upload(file=f)file_id = file.file_idprint(f"✅ File uploaded! ID: {file_id}")# 💾 IMPORTANT: Store file_id in your database!# save_to_database(user_id, file_id, "report.pdf")# ============================================# Reuse Forever (Use unlimited times)# ============================================# Use with Answer endpoint - quick Q&A about the fileresponse = client.answer( query="Summarize this report", file_ids=[file_id])print(response.answer)# Use with Conversation endpoint - multi-turn discussionresponse = client.conversation( messages=[ {"role": "user", "content": "What are the key findings?", "file_ids": [file_id]} ])print(response.response)# Use with Agent endpoint - complex tasks with tool callingresponse = client.agent( messages=[ {"role": "user", "content": "Analyze this report and create a chart", "file_ids": [file_id]} ], tools=[...] # Your tool definitions)# Same file_id works across all endpoints, forever!
Here’s how to implement a proper file management system that uploads once and reuses forever:
Copy
# Example: File library implementationfrom incredible_python import Incrediblefrom datetime import datetimeclass FileLibrary: """ Manages file uploads and reuse for Incredible API. Upload once, store file_id, reuse forever. """ def __init__(self, api_key, database): self.client = Incredible(api_key=api_key) self.db = database def upload_file(self, user_id, file_path, filename): """ Upload a file ONCE and store the file_id. Call this only when user first adds a file. """ # Check if file already uploaded existing = self.db.get_file_by_name(user_id, filename) if existing: print(f"✓ File already uploaded, reusing file_id: {existing['file_id']}") return existing['file_id'] # Upload using SDK - handles all steps automatically! with open(file_path, 'rb') as f: file = self.client.files.upload(file=f) # Store file_id in database (CRITICAL!) self.db.save_file({ "user_id": user_id, "file_id": file.file_id, "filename": filename, "uploaded_at": datetime.now() }) print(f"✓ File uploaded successfully: {file.file_id}") return file.file_id def get_user_files(self, user_id): """ Retrieve all files for a user. Show these in your UI so users can reuse them. """ return self.db.get_files_by_user(user_id) def answer_with_files(self, message, file_ids): """ Use stored file_ids for Q&A - no re-uploading! """ return self.client.answer(query=message, file_ids=file_ids) def conversation_with_files(self, messages): """ Use stored file_ids in conversation - no re-uploading! Messages can include file_ids in user messages. """ return self.client.conversation(messages=messages)# Usage Example:library = FileLibrary(api_key="ik_...", database=db)# Upload once (only when user first adds file)file_id = library.upload_file( user_id="user123", file_path="report.pdf", filename="Q4_Report.pdf")# Later: Show user their file libraryuser_files = library.get_user_files("user123")print("Your files:")for file in user_files: print(f" - {file['filename']} (ID: {file['file_id']})")# Use file in unlimited requests (no re-upload needed!)response1 = library.answer_with_files( message="Summarize this report", file_ids=[file_id])print(response1.answer)# Or use in a conversation with file contextresponse2 = library.conversation_with_files( messages=[ {"role": "user", "content": "What are the key metrics?", "file_ids": [file_id]} ])print(response2.response) # ← Same file_id, instant access!
Key Implementation Points:
Check before uploading: Always check if file already exists in your database
Store file_ids permanently: Save them alongside user data
Show file library to users: Let them see and select previously uploaded files
Files are automatically processed when you call /confirm-upload. Here’s what happens for each file type:
File Type
Processing
Metadata Extracted
CSV
Parse structure
Columns, row count, data types, preview
Excel (.xlsx, .xls)
Parse sheets
Sheet names, columns per sheet, row counts
PDF
OCR text extraction
Page count, extracted text, text length
Images (.png, .jpg, .gif, .webp)
Analyze dimensions
Width, height, format, file size
JSON
Parse structure
Keys, depth, structure overview
Text (.txt, .md)
Read content
Character count, line count, preview
PDF OCR:
PDFs are processed using OCR (Optical Character Recognition) to extract text content, making scanned documents and images-in-PDFs searchable and usable by the agentic model.
After uploading and confirming files once, attach them to any number of messages using file_ids:
Copy
{ "messages": [ { "role": "user", "content": "Analyze the sales data and create a chart", "file_ids": ["550e8400-...", "660e8400-..."] } ]}
Reuse Files Across SessionsThe same file_id can be used:
In multiple messages within a conversation
Across different conversations
By the same user indefinitely
No expiration—files remain available until deleted
What the Agent Sees:
File metadata (columns, row counts, structure)
Content previews (first rows of CSV, PDF text excerpts)
Full file access in the sandboxed Python environment
Example Capabilities:
Copy
# The agent can write Python code like this:import pandas as pdimport matplotlib.pyplot as plt# Files are automatically available in the sandboxdf = pd.read_csv("data.csv")summary = df.describe()plt.plot(df['date'], df['sales'])plt.savefig("chart.png")
The agent can read your files, process data, create visualizations, and generate new files—all automatically.