public service_client cannot send inline nested `ocr_custom_confg`
Bug
DoclingServiceClient.convert(...) cannot submit ConvertDocumentsOptions(ocr_custom_config=...) when converting a local file source.
ConvertDocumentsOptions validates ocr_custom_config as a dictionary, but the service client then submits local files through the multipart /v1/convert/file/async path. During multipart request construction, httpx rejects the nested dictionary because multipart form data values must be primitive values.
This means neither of the following works:
ocr_custom_config={ "kind": "rapidocr", "backend": "onnxruntime", }
because it passes ConvertDocumentsOptions validation but fails during multipart encoding with:
TypeError: Invalid type for value. Expected primitive type, got <class 'dict'>: {'kind': 'rapidocr', 'backend': 'onnxruntime'}
Nor does this workaround work:
ocr_custom_config=json.dumps({ "kind": "rapidocr", "backend": "onnxruntime", })
because ConvertDocumentsOptions rejects the string before request submission:
ValidationError: 1 validation error for ConvertDocumentsOptions ocr_custom_config Input should be a valid dictionary [type=dict_type, ...]
So the current client API accepts the type required by the model but cannot serialize that type for the local-file conversion path.
The raw JSON endpoint /v1/convert/source can accept the same nested ocr_custom_config dictionary successfully because the request body is JSON, not multipart form data. The issue appears specific to the service client's local-file upload path.
Steps to reproduce
from pathlib import Path
from docling.service_client import DoclingServiceClient
from docling.datamodel.service.options import ConvertDocumentsOptions
SERVICE_URL = "http://localhost:5001"
with DoclingServiceClient(url=SERVICE_URL) as client:
result = client.convert(
source=Path("../data/test-document.jpg"),
options=ConvertDocumentsOptions(
ocr_custom_config={
"kind": "rapidocr",
"backend": "onnxruntime",
}
),
)
markdown = result.document.export_to_markdown()
print(markdown)
Observed result:
TypeError: Invalid type for value. Expected primitive type, got <class 'dict'>: {'kind': 'rapidocr', 'backend': 'onnxruntime'}
The traceback shows the request is being submitted through:
/v1/convert/file/async
and fails while httpx is encoding multipart form data:
File ".../docling/service_client/client.py", line 1135, in _submit_convert_task response = self._request_with_retry( method="POST", path="/v1/convert/file/async", data=data, files=files, ... )
File ".../httpx/_multipart.py", line 81, in DataField.init raise TypeError( "Invalid type for value. Expected primitive type," f" got {type(value)}: {value!r}" )
If trying to work around the multipart limitation by passing JSON as a string:
import json
from pathlib import Path
from docling.service_client import DoclingServiceClient
from docling.datamodel.service.options import ConvertDocumentsOptions
SERVICE_URL = "http://localhost:5001"
with DoclingServiceClient(url=SERVICE_URL) as client:
result = client.convert(
source=Path("../data/test-document.jpg"),
options=ConvertDocumentsOptions(
ocr_custom_config=json.dumps({
"kind": "rapidocr",
"backend": "onnxruntime",
})
),
)
Observed result:
ValidationError: 1 validation error for ConvertDocumentsOptions ocr_custom_config Input should be a valid dictionary [type=dict_type, input_value='{"kind": "rapidocr", "backend": "onnxruntime"}', input_type=str]
Expected behavior:
DoclingServiceClient.convert(...) should be able to submit nested conversion options such as ocr_custom_config for local file sources.
Docling version
docling-2.104.0 docling-slim-2.104.0
Python version
3.13
Hi @here4dadata, i dug into this and found the root cause and a fix approach.
When source is a local file, convert() uploads it as multipart/form-data to /v1/convert/file/async and sends every option as a form field, roughly:
files = self._source_to_upload_files(source)
data = options.model_dump(mode="json", exclude_none=True) # ocr_custom_config stays a nested dict
data["target_type"] = ...
self._http_client.request("POST", ".../convert/file/async", data=data, files=files)
Multipart form fields can only carry primitives or lists of primitives, so httpx's encoder rejects the nested ocr_custom_config dict with TypeError: Expected primitive type, got dict and it happens in client-side, before the request is even sent.
The source/URL path (/v1/convert/source/async) isn't affected because it posts a JSON body, which carries nested dicts fine. So this breaks every nested option on the file-upload path: ocr_custom_config, table_structure_custom_config, layout_custom_config, picture_classification_custom_config.
Fix approach
- Client: JSON-encode any non-primitive form value before building the multipart
data, at each upload site (convert + chunk, sync + async). Primitives and primitive lists pass through unchanged. - Shared model (
ConvertDocumentsOptions): add amode="before"validator on the dict-typed config fields thatjson.loads()a string input, so the server reconstructs the dict from the JSON string. No-op for the JSON/source path, where the value is already a dict. - Tests: a file submit serializes
ocr_custom_configto a JSON string, and the model round-trips a JSON-string config.
One thing worth confirming: this assumes docling-serve validates file uploads through the shared ConvertDocumentsOptions model. If it parses the form fields differently, the decode half may need a matching tweak server-side, but the client-side encoding fix is correct regardless, since today it's a hard crash.
Happy to open a PR with the above if this direction looks good and aligns with the maintainers' expectations!