Hello,
I'm currently working on an iPad app that combines the flexibility of handwritten notes with advanced mathematical computation, including linear algebra, calculus, and solving systems of equations.
One of its core features is to recognize handwritten mathematical notation and converting it into a structured representation that can subsequently be parsed and evaluated.
For this, I want to use the SystemLanguageModel() together with Guided Generation.
I created a custom set of "MathToken" enums using @Generable. The idea is to let the model recognize mathematical handwriting and directly return an array of these tokens.
The tokens represent both basic mathematical elements (integers, identifiers, operators, etc.) and more complex structures such as matrices, vectors, function applications, derivatives, integrals, and systems of equations.
The resulting tokens are then passed to a parser, which constructs a structured statement that can eventually be evaluated.
For handwriting input, I exported the relevant content from PaperKit/PencilKit as a UIImage and attached it to the LanguageModelSession.
I've attached both the MathToken definition and my current prompting approach.
Unfortunately, the recognition accuracy is not yet sufficient for a reliable user experience.
I've experimented with different prompting strategies:
- Very short and general instructions.
- More detailed descriptions what to watch for
- Explicit rules explaining when each token should be used
However, none of these approaches has significantly improved the results. The model frequently selects tokens that contradict the instructions it was given.
Some examples:
- The model sometimes generates an identifier containing a number instead of using integer or real.
- Instead of using structured tokens like functionApplication, it uses identifiers and parentheses instead
- For ordinary multiplication, such as 3 × 5, it sometimes generates the product token, which is specifically intended for mathematical product notation (capital Pi).
These are not ambiguous cases. I've explicitly described the distinctions in my instructions, yet the model still makes these mistakes.
I'm aware that prompting and model context size have their limitations, so I'm trying to understand whether my overall approach is appropriate.
Prompting: Is there a better way to guide the model towards consistently selecting the correct enum cases?
Guided Generation: Is an enum with many associated values and recursively nested [MathToken] arrays a suitable structure for Guided Generation? Could the complexity or structure of my MathToken definition itself be contributing to the problem? How could a more suitable structure look like (which I could transform into the MathTokens)?
Model capabilities: Is recognizing handwritten mathematics and mapping it directly to such a structured representation simply too complex a task for the on-device model in its current form?
Architecture: Could this problem benefit form splitting it into multiple stages? What stages would you recommend?
I'd be particularly interested in understanding whether there are any known limitations or recommended patterns when using Guided Generation for tasks involving complex, recursively structured output.
I've included my current implementation and the MathToken definition below/attached for context.
Thank you for your time and any suggestions!