c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
120 lines
3.6 KiB
Plaintext
120 lines
3.6 KiB
Plaintext
---
|
|
title: "FileContent"
|
|
id: filecontent
|
|
slug: "/filecontent"
|
|
description: "`FileContent` represents file payloads in chat messages, including base64 data, MIME type, filename, and provider-specific metadata."
|
|
---
|
|
|
|
# FileContent
|
|
|
|
`FileContent` represents a file payload that can be attached to a [`ChatMessage`](chatmessage.mdx). Use it when a chat model accepts file inputs, such as PDFs or other documents, together with the user's text prompt.
|
|
|
|
If you need the full list of parameters and methods, see the [`FileContent` API reference](/reference/data-classes-api#filecontent).
|
|
|
|
## Attributes
|
|
|
|
```python
|
|
@dataclass
|
|
class FileContent:
|
|
base64_data: str
|
|
mime_type: str | None = None
|
|
filename: str | None = None
|
|
extra: dict[str, Any] = field(default_factory=dict)
|
|
validation: bool = True
|
|
```
|
|
|
|
- `base64_data` stores the file content as a base64-encoded string.
|
|
- `mime_type` identifies the file type, for example `application/pdf`. Providing it explicitly is recommended because many model providers require it.
|
|
- `filename` is optional, but some providers use it when processing uploaded files.
|
|
- `extra` can store provider-specific metadata. Values should be JSON serializable.
|
|
- `validation` checks that `base64_data` is valid and tries to infer the MIME type when one is not provided.
|
|
|
|
## Create from a file path
|
|
|
|
Use `from_file_path` to read a local file, base64-encode it, infer the MIME type from the path, and populate the filename.
|
|
|
|
```python
|
|
from haystack.dataclasses import ChatMessage, FileContent
|
|
|
|
file_content = FileContent.from_file_path("data/attention-is-all-you-need.pdf")
|
|
|
|
message = ChatMessage.from_user(
|
|
content_parts=[
|
|
file_content,
|
|
"Summarize the key ideas in this paper.",
|
|
]
|
|
)
|
|
```
|
|
|
|
Pass `filename` or `extra` when a provider expects a specific filename or provider-specific options:
|
|
|
|
```python
|
|
file_content = FileContent.from_file_path(
|
|
"data/report.pdf",
|
|
filename="quarterly-report.pdf",
|
|
extra={"source": "finance"},
|
|
)
|
|
```
|
|
|
|
## Create from a URL
|
|
|
|
Use `from_url` to download a file and convert it into a `FileContent` instance.
|
|
|
|
```python
|
|
from haystack.dataclasses import FileContent
|
|
|
|
file_content = FileContent.from_url(
|
|
"https://example.com/reports/quarterly-report.pdf",
|
|
timeout=30,
|
|
)
|
|
```
|
|
|
|
If no filename is provided, Haystack uses the final path segment of the URL.
|
|
|
|
## Create from base64 data
|
|
|
|
If you already have file bytes, encode them and pass the MIME type explicitly.
|
|
|
|
```python
|
|
import base64
|
|
from pathlib import Path
|
|
|
|
from haystack.dataclasses import FileContent
|
|
|
|
data = Path("data/manual.pdf").read_bytes()
|
|
file_content = FileContent(
|
|
base64_data=base64.b64encode(data).decode("utf-8"),
|
|
mime_type="application/pdf",
|
|
filename="manual.pdf",
|
|
)
|
|
```
|
|
|
|
Set `validation=False` only when the base64 data and MIME type are already trusted and you want to skip validation.
|
|
|
|
## Inspect files in a ChatMessage
|
|
|
|
After adding `FileContent` to a `ChatMessage`, use the `file` and `files` properties to access file payloads.
|
|
|
|
```python
|
|
from haystack.dataclasses import ChatMessage, FileContent
|
|
|
|
file_content = FileContent.from_file_path("data/invoice.pdf")
|
|
message = ChatMessage.from_user(content_parts=[file_content, "Extract the invoice total."])
|
|
|
|
print(message.file)
|
|
print(message.files)
|
|
```
|
|
|
|
`message.file` returns the first file payload, or `None` if there are no files. `message.files` returns all file payloads.
|
|
|
|
## Serialization
|
|
|
|
Use `to_dict` and `from_dict` to serialize and restore file content.
|
|
|
|
```python
|
|
payload = file_content.to_dict()
|
|
restored = FileContent.from_dict(payload)
|
|
```
|
|
|
|
For tracing, Haystack replaces the full base64 payload with a placeholder so large files are not sent to the tracing backend.
|