While building a vibe coding platform, I started building Mozaik, a library for working with LLMs. Very soon, a need emerged to handle multiple LLM providers - and with that, different request and response formats. To avoid messy code, I applied the Chain of Responsibility design pattern to handle the different responses coming back from an LLM.
Instead of branching logic, each response flows through a chain of small handlers. Each handler checks one thing (structured output, tool call, plain text, empty response) and either handles it or forwards it.
This keeps response handling explicit and composable:
- each handler has a single responsibility
- handlers are easy to test in isolation
- new response types can be added without touching existing logic
Structured output validation is just another handler in the chain, not a special case.


Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.