Skip to content

[litertlm] Refactor Python API with ABC and simplify usage pattern.#1548

Merged
copybara-service[bot] merged 1 commit intomainfrom
litert_lm_pr_871969731
Mar 6, 2026
Merged

[litertlm] Refactor Python API with ABC and simplify usage pattern.#1548
copybara-service[bot] merged 1 commit intomainfrom
litert_lm_pr_871969731

Conversation

@copybara-service
Copy link
Copy Markdown
Contributor

[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:

  • 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.

@copybara-service copybara-service Bot force-pushed the litert_lm_pr_871969731 branch 3 times, most recently from b147ede to 579febe Compare March 6, 2026 22:59
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
@copybara-service copybara-service Bot force-pushed the litert_lm_pr_871969731 branch from 579febe to f19df98 Compare March 6, 2026 23:14
@copybara-service copybara-service Bot merged commit f19df98 into main Mar 6, 2026
@copybara-service copybara-service Bot deleted the litert_lm_pr_871969731 branch March 6, 2026 23:14
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.

1 participant