ollama - 💡(How to fix) Fix API support for attaching files (PDF, JSON, images, bytes/base64) like the UI [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#14721Fetched 2026-04-08 00:32:35
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
1
Participants
Timeline (top)
labeled ×1
RAW_BUFFERClick to expand / collapse

The UI can directly attach sources like PDFs, JSON, and images and use them automatically as context.

I want the same on the API side, especially for Python.

The main point is that Ollama should process these automatically and include them in context, without me having to manually extract content and inject it into the prompt.

Example Python use case: send a prompt, attach a PDF/JSON/image, and let Ollama use them directly.

This would make API usage much cleaner and bring it closer to the native feature set.

extent analysis

Fix Plan

To enable automatic processing of attached sources like PDFs, JSON, and images on the API side, we will implement a multipart/form-data upload endpoint.

Steps

  • Update the API endpoint to accept multipart/form-data requests
  • Use a library like python-magic to detect the file type and PyPDF2 for PDF processing
  • Modify the prompt processing logic to include the attached file content

Example Code

import os
from flask import Flask, request, jsonify
from PyPDF2 import PdfReader
import json

app = Flask(__name__)

@app.route('/process', methods=['POST'])
def process_prompt():
    # Get the prompt and attached file
    prompt = request.form['prompt']
    file = request.files['file']

    # Detect file type and process accordingly
    if file.filename.endswith('.pdf'):
        pdf_reader = PdfReader(file)
        text = ''
        for page in pdf_reader.pages:
            text += page.extract_text()
        # Include the extracted text in the prompt context
        context = {'prompt': prompt, 'text': text}
    elif file.filename.endswith('.json'):
        data = json.load(file)
        # Include the JSON data in the prompt context
        context = {'prompt': prompt, 'data': data}
    else:
        # Handle other file types or errors
        return jsonify({'error': 'Unsupported file type'}), 400

    # Call the Ollama API with the updated context
    # ...

    return jsonify({'result': 'Success'}), 200

if __name__ == '__main__':
    app.run(debug=True)

Verification

Test the API endpoint by sending a POST request with a prompt and an attached file (e.g., PDF or JSON). Verify that the Ollama API processes the attached file correctly and includes its content in the prompt context.

Extra Tips

  • Ensure proper error handling for unsupported file types and other potential errors.
  • Consider adding authentication and authorization mechanisms to the API endpoint.
  • Use a robust library for file type detection and processing to handle various file formats and edge cases.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

ollama - 💡(How to fix) Fix API support for attaching files (PDF, JSON, images, bytes/base64) like the UI [1 participants]