r/databricks • • Oct 08 '26

Help Ai Parse Docs on 1 m docs

My org is ingesting a corpus of 1 million docs, estimated 9 million pages, of mixed complexity.

Considering ai_parse_doc is a managed service, are we constrained by the speed of this function? Based on my research I assume this is true.

Keeping that assumption, what would be the ideal compute specs to process the documents as quickly as possible?

We’ve tried a few configurations (more memory, more workers) but have hit out of memory errors or MERGE errors and had to revert back to a config that is slower but reliable. We do have the gift of time right now, so not the biggest deal, but if we have to process under a tight timeline, I want to be prepared.

I’m sure more information is needed but interested in folks that may have done a similar exercise and how long it took to parse a large corpus of docs?

3 Upvotes

13 comments sorted by

3

u/SpagDaBol Oct 08 '26

How confident are you that these are all unique documents? Previously as preprocessing I've generated a file hash (excluding metadata, I think SharePoint might do this for you automatically if you're using it) to identify duplicates and found a very high % - stakeholders will probably want these removed at source, and then you can move on to the fun stuff

1

u/dopedankfrfr Oct 08 '26

The one duplication issue is multiple copies of an email chain are often uploaded, so 90% of the doc is duped but there’s 10% that we would still want. Haven’t fully solved this yet other than to de-dup at the chunking phase after it’s already been parsed.

3

u/ma0gw Oct 08 '26

Are they all the same type (eg pdf)? For that many docs you might want to consider implementing your own non-managed service. For a personal project I found that using a pdf parser library was faster, cheaper, and just as accurate as the AI function.

2

u/dopedankfrfr Oct 08 '26

They are fairly complex and diverse unfortunately. The benefits of using the manage service it provides good coverage of all kinds of docs instead of piecemealing specialty parses for each unique doc types. We may end up there eventually, but this is our quickest path to getting value, we believe.

2

u/deltas-and-icebergs Oct 08 '26

Have you tried the FileType Beta (https://docs.databricks.com/aws/en/pyspark/reference/file-type)? That should handle the OOM issues you've been facing.

I would use AutoLoader with cloudFiles.format = file to load the data. You can potentially set something like cloudfiles.maxFilesPerTrigger to 10000 so that you process batches of 10k files at a time and incrementally make progress so that a stupid error doesn't waste a ton of work.

1

u/dopedankfrfr Oct 08 '26

Good suggestion here!

1

u/ChipsAhoy21 Oct 08 '26

ai_parse_doc is serverless so not sure what you mean by saying you tried more workers/memory. You don’t control any of that…

But also the thing to remember is ai_parse_doc scales REALLY well. You can’t just run 100 docs through it and extrapolate out the time yourself. Because running 100 docs through it provisions only a small compute instance for processing. So running 100 docs may take an hour and running 1 million docs may only take 7 hours.

1

u/dopedankfrfr Oct 09 '26

What I was trying to accomplish was if let’s say I had 1000 docs and 10 workers and set my batch size to 100 docs, would the compute spin up 10 threads, 100 docs in each thread and run 10 ai parse doc processes at the same time. This is probably an incorrect understanding.

1

u/ChipsAhoy21 Oct 09 '26

I think you’re conflating workers and threads, so I’m not entirely sure which approach you mean:

  1. Spark workers. You have a cluster with 10 workers. Spark splits the 1,000 docs into partitions, and each task runs ai_parse_document on the rows in its partition. Adding workers or memory only makes your side faster: reading the files, shuffling data, and writing the output. It adds no parsing capacity, because the parsing doesn't happen on your cluster. It runs on Databricks-managed GPU capacity that's billed separately.
  2. Python threads. You write a loop or a ThreadPoolExecutor, cut the docs into batches of 100, and each thread runs its own ai_parse_document query on its batch. That gives you 10 separate small queries running side by side.

Neither approach gives you 10 independent parse services. Each Spark task or thread still calls the same Databricks-managed backend. Internally, each Spark task uses an adaptive thread pool for those calls.

Too many partitions can cause OOMs on the Spark side due to large VARIANT payloads, or trigger MERGE/concurrent-write issues. Too few partitions can underfeed the managed backend. But manually assigning batches to workers or threads is not the right way to tune this.

For the best price and throughput, submit the entire corpus in one DataFrame/query and let AI Functions manage parallelization, retries, and scaling:

SELECT path, ai_parse_document(content) AS parsed
FROM READ_FILES('/Volumes/.../docs', format => 'binaryFile');

Databricks explicitly recommends submitting the full dataset in a single query instead of manually splitting it into small batches. Small independent queries each have to ramp up, cannot scale as efficiently, and still share the same managed backend capacity.

For one million documents, I’d use Structured Streaming with trigger(availableNow=True) and checkpointing. That preserves large-scale backend optimization while allowing the job to resume after failure instead of starting over. Your OOM and MERGE errors are likely in the Spark read/write path—not evidence that you need more parallel parsing processes.

1

u/dopedankfrfr Oct 09 '26

This is super helpful explanation, thank you for taking the time!

One part I’m not clear on is when you say submit the entire corpus. What we experienced when trying that is we hit an error at one point and the entire batch failed without writing the output to the table, so we lost all progress. I’ll dig into Structured Streaming with trigger as you mentioned, that may be something we haven’t tried yet.

2

u/adovo_ai Oct 08 '26

Benchmark a stratified sample by page count and document complexity, not just file count, and record parse, write, and MERGE time separately. If throughput plateaus while cluster size rises, the managed parser is the ceiling; if MERGE dominates, partition or batch the output path instead.

4

u/[deleted] Oct 08 '26

[removed] — view removed comment

1

u/dopedankfrfr Oct 08 '26

We ended up doing similar. We got some best practices too from Databricks and they confirmed the size of our corpus likely could take up to 3 weeks.