dabla opened a new pull request, #77437:
URL: https://github.com/apache/airflow/pull/77437
Adds OCR of documents through a vendor model to common.ai: `LLMOCROperator`
and `@task.llm_ocr`. A PDF goes in, its pages come out as Markdown, one dict
per page in the shape `DocumentLoaderOperator` returns.
```python
@task.llm_ocr(
llm_conn_id="mistral_default",
model_id="mistral:mistral-ocr-latest",
pages_per_request=16,
max_concurrency=4,
)
async def ocr_pdf(document: dict) -> bytes:
"""Download the PDF to read."""
return await download(document)
pages = ocr_pdf.expand(document=list_documents())
```
This is a draft to discuss the shape, as asked on #74201. It is independent
of that PR and touches none of its files.
**What the operator does**
- It cuts a document that has more pages than one request may carry (pypdf,
the existing `pdf` extra), sends `max_concurrency` parts at once on the task's
event loop, and returns the pages in document order.
- A part that is not answered in time is sent again as two halves, down to
single pages.
- Errors are typed so a retry policy can tell them apart: `OCRBusyError`
(429/5xx, carries `Retry-After`), `OCRTimeoutError`, and `OCRError` for a
refused document.
- The decorated function returns the document and may be `async def`, so a
download runs on the same event loop and the document never goes through an
XCom.
- `output_processor` on the decorator returns the task's result from the
pages and the arguments of the function it names, so a mapped task can say
which document its pages belong to.
**How the vendor is kept out of the operator**
The `model_id` prefix selects an `OCRAdapter`, the same idea as the batch
adapters. The first adapter is `mistral` (httpx2, native async, no SDK). It
speaks the Mistral OCR API, which Mistral itself, Mistral Document AI on Azure
AI Foundry and gateways such as LiteLLM all serve, so the connection says where
and the adapter stays the same.
**Why cut in the operator**
Some deployments limit the pages of one document: Azure AI Foundry refuses
more than 30, even when only a few pages are selected. Cutting, running parts
concurrently and reassembling is orchestration, not vendor logic, so the
adapter only declares its limit and the operator does the rest. The docs also
show the other way, cutting in a task of its own so every part is a mapped task
instance with its own retry.
**Open questions**
1. An operator and decorator of its own with an adapter registry, or an
`ocr` backend of `DocumentLoaderOperator`? The provider's AGENTS.md asks for
three concrete cases before an abstraction; there is one adapter today.
2. Connection: the `mistral` adapter reads a `pydanticai` or `http`
connection, with `ocr_path` and `max_pages_per_request` in the extra. Is that
the right place for both?
3. `output_processor`: the same callable is planned for `@task.agent` in a
separate PR. Keep it here, or land it there first?
**Notes**
- Requires Airflow 3.2+ (`BaseAsyncOperator` through `common.compat`); the
`common-compat` floor is left as it is with `# use next version`, since the
async connection helpers are in 1.23.0.
- Tests: 65 new unit tests (registry, PDF cutting, Mistral adapter against
an httpx2 mock transport, operator, decorator).
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes (please specify the tool below)
Generated-by: Claude Code following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]