Fix on_request callback not triggering for API specs#642
Merged
andyland merged 3 commits intoLightning-AI:mainfrom Dec 12, 2025
Merged
Fix on_request callback not triggering for API specs#642andyland merged 3 commits intoLightning-AI:mainfrom
on_request callback not triggering for API specs#642andyland merged 3 commits intoLightning-AI:mainfrom
Conversation
- Add server reference to LitSpec for callback access - Trigger on_request callback in OpenAIEmbeddingSpec and OpenAISpec endpoints - Add __getstate__ to exclude server from pickling - Add tests for callback triggering with specs
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #642 +/- ##
===================================
Coverage 85% 85%
===================================
Files 39 39
Lines 3203 3212 +9
===================================
+ Hits 2710 2721 +11
+ Misses 493 491 -2 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #634 where the on_request callback was not being triggered for spec-based endpoints (OpenAIEmbeddingSpec and OpenAISpec). The fix enables request tracking and other callback functionality for API specifications.
Key changes:
- Added server reference storage in spec instances for callback access
- Implemented callback triggering in both OpenAI spec endpoint methods
- Added pickle exclusion for server reference to prevent serialization issues
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/litserve/server.py | Sets _server reference on spec instances during endpoint registration to enable callback access |
| src/litserve/specs/base.py | Adds __getstate__ to exclude _server from pickling, preventing serialization errors in worker processes |
| src/litserve/specs/openai_embedding.py | Triggers on_request callback in embeddings_endpoint method before queueing requests |
| src/litserve/specs/openai.py | Triggers on_request callback in chat_completion method before processing requests |
| tests/unit/test_callbacks.py | Adds test coverage for callback triggering in both OpenAI specs |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
on_request callback not triggering for API specs
andyland
approved these changes
Dec 12, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #634: Active requests not working when an API Spec is defined.
The
on_requestcallback was not being triggered for spec-based endpoints like OpenAIEmbeddingSpec and OpenAISpec. This PR adds the callback trigger to the spec endpoint methods and ensures the spec has access to the server's callback runner.Changes:
PR review
Anyone in the community is free to review the PR once the tests have passed.
Did you have fun?
Make sure you had fun coding 🙃