I’ve been writing OCaml code that needs to interface with Python for a while, and I kept running into the same issue: every project required the same long stretch of boilerplate. ctypes declarations, C stubs with CAMLparam and CAMLreturn, Python wrappers, Dune configs. None of it was the interesting part of the work. It was just the cost of getting across the language boundary.
After repeating this enough times, I started noticing how mechanical the entire process was. You take a function signature from an .mli file and translate it into three different layers. It doesn’t really involve problem-solving. It’s just structured repetition. That was the moment I realized I should automate it.
So I built Polyglot FFI.
The goal is straightforward: write your interface once, and let the tool handle the rest.
polyglot-ffi generate crypto.mli
That one command generates the ctypes layer, the C wrappers with proper GC handling, the Python module with type hints, and the corresponding build configuration. It supports primitives, records, tuples, lists, Option, Result, and nested types.
I just released v0.5.2, which fixes some memory safety bugs I found while testing Option types. In certain cases, a NULL pointer could slip through and cause crashes. Those are the details I wanted the tool to handle, so developers don’t have to worry about them.
What I’m looking for now is feedback from the OCaml community. I’ve used this tool in my own projects, but I know there are type system edges I haven’t hit yet. I would also appreciate contributors, especially around Rust or Go support, or work on bidirectional bindings.
The project is MIT licensed.
Install with: pip install polyglot-ffi
Docs: https://polyglotffi.com
GitHub: https://github.com/chizy7/polyglot-ffi
If you’ve ever found yourself rewriting the same FFI code over and over, you might find this useful. I’d love to hear your thoughts.