From 191fae4afb2ed2f6b7e4d9c1a28a05f43458dfad Mon Sep 17 00:00:00 2001 From: djlongy <35615878+djlongy@users.noreply.github.com> Date: Wed, 23 Sep 2026 18:24:42 +1000 Subject: [PATCH] fix: send Markdown uploads as text/markdown The published image runs Python 3.11, whose mimetypes table has no .md entry, and the slim base ships no /etc/mime.types. httpx then labels every Markdown upload application/octet-stream. Open WebUI only reads a file as plain text when the type is text/* or the extension is a known source extension, so with a content extraction engine configured (Docling, Tika) each .md went through that engine and came back re-rendered. Docling in particular breaks GFM tables that contain inline code. Fall back to text/markdown for .md and .markdown when mimetypes has no answer. Registering the type globally with mimetypes.add_type would also work but changes process-wide state for connectors that embed oikb. --- src/oikb/client.py | 15 ++++++++++++++- tests/test_kb_sync.py | 11 +++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/oikb/client.py b/src/oikb/client.py index 9c2a07d..1a7563f 100644 --- a/src/oikb/client.py +++ b/src/oikb/client.py @@ -3,11 +3,24 @@ from __future__ import annotations import json +import mimetypes +import os from typing import Any import httpx +# Python 3.11 has no .md entry and slim images ship no /etc/mime.types, so a +# Markdown upload went out as application/octet-stream and Open WebUI handed it +# to its document extraction engine (Docling, Tika) instead of reading it as text. +_TEXT_TYPES = {".md": "text/markdown", ".markdown": "text/markdown"} + + +def _content_type(filename: str) -> str: + ext = os.path.splitext(filename)[1].lower() + return mimetypes.guess_type(filename)[0] or _TEXT_TYPES.get(ext, "application/octet-stream") + + class OikbClient: """Stateless HTTP client for the Open WebUI KB API. @@ -84,7 +97,7 @@ class OikbClient: resp = self._http.post( "/files/", - files={"file": (filename, file_content)}, + files={"file": (filename, file_content, _content_type(filename))}, data={"metadata": json.dumps(metadata)}, ) resp.raise_for_status() diff --git a/tests/test_kb_sync.py b/tests/test_kb_sync.py index 478b67c..f0a410b 100644 --- a/tests/test_kb_sync.py +++ b/tests/test_kb_sync.py @@ -1,9 +1,12 @@ +import mimetypes from unittest.mock import AsyncMock, Mock import httpx import pytest +import respx from click.testing import CliRunner +from oikb.client import OikbClient from oikb.connectors import BaseConnector, ManifestEntry from oikb.kb_sync import group_entries_by_kb, run_entries_sync from oikb.sync import SyncCancelled, SyncResult @@ -145,3 +148,11 @@ def test_upload_error_includes_server_detail(): client.upload_file.side_effect = httpx.HTTPStatusError("Bad Request", request=response.request, response=response) result = run_entries_sync(client, [{"source": "one", "kb-id": "kb"}], resolve_connector=lambda *a, **kw: Source({"a.txt": b"a"}), quiet=True) assert "extraction failed" in result.errors[0] + + +@respx.mock +def test_upload_sends_markdown_as_text_without_system_mime_types(monkeypatch): + monkeypatch.setattr(mimetypes, "guess_type", lambda *a, **kw: (None, None)) + route = respx.post("https://webui.example/api/v1/files/").mock(return_value=httpx.Response(200, json={"id": "f"})) + OikbClient("https://webui.example", "token").upload_file(b"| a |\n| - |\n", "doc.md", "kb", "hash") + assert b"Content-Type: text/markdown" in route.calls.last.request.content