GitLab AI Gateway Contribution

My contribution to GitLab's AI Gateway: I reorganized middleware code so it is easier to find and maintain.

What changed

  • Improved code structure by moving middleware into its own module.
  • Kept old behavior working during the refactor.
  • Improved test coverage for middleware and logging.

What I worked on

  • I moved InternalEventMiddleware into a dedicated module.
  • I added and updated tests after the move.
  • I fixed circular import and test issues during review.

I refactored InternalEventMiddleware in GitLab AI Gateway. I moved it out of a crowded base module into its own middleware file, and moved its tests with it.

This was part of a wider cleanup to make middleware easier to find, test, and maintain, without breaking existing imports.

What I shipped in MR !2291:

  • moved the middleware into ai_gateway/api/middleware/internal_event.py,
  • split and updated the tests in the middleware test module,
  • kept old imports working by re-exporting where needed.

During review, maintainers flagged a circular import and some logger-related test failures. I fixed both, and the MR merged after final approval.

I followed up with two more merged MRs:

  • !2726 extracted DistributedTraceMiddleware into its own file in the same way,
  • !4586 added tests that validate every flow config YAML file.

Links: MR !2291 • Issue #859 • MR !2726 • MR !4586