[litertlm] Refactor Python API with ABC and simplify usage pattern.#1548
Merged
copybara-service[bot] merged 1 commit intomainfrom Mar 6, 2026
Merged
[litertlm] Refactor Python API with ABC and simplify usage pattern.#1548copybara-service[bot] merged 1 commit intomainfrom
copybara-service[bot] merged 1 commit intomainfrom
Conversation
b147ede to
579febe
Compare
This change refactors the LiteRT LM Python API to provide a more idiomatic and
simplified interface for users. Key changes include:
- Abstract Base Classes (ABC): Introduced AbstractEngine and AbstractConversation
in a new interfaces.py module. The C++ implementation classes are registered as
virtual subclasses, allowing for proper inheritance checks (e.g.,
isinstance(engine, AbstractEngine)) across the C++/Python boundary.
- Simplified Engine & Conversation Lifecycle:
- Users can now directly instantiate litert_lm.Engine with configuration
parameters, which internally handles ModelAssets and EngineSettings.
- Added engine.create_conversation() to eliminate the need for manual
ConversationConfig and Conversation.create calls.
- send_message and send_message_async now support both str and dict inputs. String inputs are automatically wrapped into a user-role message.
- Moved the Backend enum to Python (interfaces.py).
- Internal Refactoring: Updated C++ bindings to support the new factory methods and
simplified logging initialization using a mapping-based approach.
- Enhanced Testing: Updated existing tests to the new API and added new test cases
for ABC inheritance, simplified conversation creation, and string input support.
LiteRT-LM-PiperOrigin-RevId: 879841369
579febe to
f19df98
Compare
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.
[litertlm] Refactor Python API with ABC and simplify usage pattern.
This change refactors the LiteRT LM Python API to provide a more idiomatic and
simplified interface for users. Key changes include:
in a new interfaces.py module. The C++ implementation classes are registered as
virtual subclasses, allowing for proper inheritance checks (e.g.,
isinstance(engine, AbstractEngine)) across the C++/Python boundary.
parameters, which internally handles ModelAssets and EngineSettings.
ConversationConfig and Conversation.create calls.
simplified logging initialization using a mapping-based approach.
for ABC inheritance, simplified conversation creation, and string input support.