Skip to content

Fix on_request callback not triggering for API specs#642

Merged
andyland merged 3 commits intoLightning-AI:mainfrom
bhimrazy:fix-on-request-callback-specs
Dec 12, 2025
Merged

Fix on_request callback not triggering for API specs#642
andyland merged 3 commits intoLightning-AI:mainfrom
bhimrazy:fix-on-request-callback-specs

Conversation

@bhimrazy
Copy link
Collaborator

@bhimrazy bhimrazy commented Dec 3, 2025

What does this PR do?

Fixes #634: Active requests not working when an API Spec is defined.

The on_request callback 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:

  • Modified LitSpec to store server reference for callback access
  • Added on_request callback trigger in OpenAIEmbeddingSpec.embeddings_endpoint
  • Added on_request callback trigger in OpenAISpec.chat_completion
  • Added getstate to LitSpec to exclude server from pickling
  • Added tests to verify callback triggering for both specs

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 🙃

bhimrazy and others added 2 commits December 3, 2025 14:53
- 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
@codecov
Copy link

codecov bot commented Dec 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85%. Comparing base (6fd40c1) to head (ada672b).
⚠️ Report is 1 commits behind head on main.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bhimrazy bhimrazy marked this pull request as ready for review December 12, 2025 09:16
Copilot AI review requested due to automatic review settings December 12, 2025 09:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bhimrazy bhimrazy changed the title Fix on_request callback not triggering for API specs Fix on_request callback not triggering for API specs Dec 12, 2025
@andyland andyland merged commit 2570834 into Lightning-AI:main Dec 12, 2025
26 checks passed
@bhimrazy bhimrazy deleted the fix-on-request-callback-specs branch December 12, 2025 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Active requests not working when an API Spec is defined

3 participants