Skip to content

Get rid of LLMConfig and make LLM itself a pydantic class #83

Closed

Description

@xingyaoww

Things that need to be configured:

  • LLM Settings
  • Runtime settings
    • k8s
    • docker
  • Agent behaviors
    • security analyzer
    • condensation scheme

The most pythonic way to do this is here

llm = LLM(
  model="claude",
  api_key="abc",
  temperature=1,
)

Today we separate config from the object itself

llm_config = LLMConfig(
  model="claude",
  api_key="abc",
  temperature=1,
)
llm = LLM(llm_config)

Maybe we can combine the two. We make LLM a dataclass or pydantic class and have the ability to load it from env/toml/json

@dataclass
class LLM:
  model: string
  api_key: string
  temperature: float
  
  @classmethod
  def deserialize():
    pass
  
  def serialze():
    pass
  
  @classmethod
  def load_from_json():
    pass
  
  @classmethod
  def load_from_env():
    pass
  
  @classmethod
  def load_from_toml():
    pass
class LLMRegistry:
  def __init__(llms: dict[string][LLM]):
    self.llms = llms

Activity

  1. added
    openhands
    on Sep 3, 2025
  2. openhands-ai commented on Sep 3, 2025

    @openhands-ai

    I'm on it! xingyaoww can track my progress at all-hands.dev

    🔴 Solvability: HARD

    This issue appears to be extremely challenging to resolve due to a complete lack of essential information - there is no description, error messages, reproduction steps, or technical details provided. The absence of standard problem-solving elements and clear technical context makes it nearly impossible to understand what needs to be fixed.

    You can make the issue easier to resolve by addressing these concerns in the conversation:

    • Provide a detailed description of the problem you're experiencing, including what you expected to happen versus what actually occurred
    • Include specific error messages, stack traces, or unexpected outputs that demonstrate the issue
    • Add step-by-step reproduction instructions and relevant code snippets that show how to trigger the problem

  3. added a commit that references this issue on Sep 3, 2025
    4cb8799
  4. openhands-ai commented on Sep 3, 2025

    @openhands-ai

    Summary

    I have successfully completed issue #83 by refactoring the LLM configuration system to make LLM itself a pydantic class, eliminating the need for a separate LLMConfig class.

    Key Changes Made

    ✅ Core Refactoring

    • Removed LLMConfig class entirely and merged its functionality into the LLM class
    • Made LLM inherit from pydantic.BaseModel for automatic validation and serialization
    • Updated constructor to accept configuration parameters directly: LLM(model="claude", api_key="abc", temperature=1)

    ✅ New Serialization Methods Added

    • serialize() - Convert LLM instance to dictionary
    • deserialize(data) - Create LLM instance from dictionary
    • load_from_json(json_path) - Load LLM configuration from JSON file
    • load_from_env() - Load LLM configuration from environment variables
    • load_from_toml(toml_path) - Load LLM configuration from TOML file

    ✅ Updated All Dependencies

    • LLMRegistry: Updated to work with new LLM class structure
    • Agent: Updated to use new LLM constructor pattern
    • All Tests: Updated to use LLM(**config) pattern (21/21 LLM tests passing)
    • Examples: Updated to demonstrate new pythonic usage
    • Integration Tests: All passing (127/127)

    ✅ Code Quality & Type Safety

    • Added comprehensive type annotations with proper null checks
    • Fixed all pre-commit hook issues (formatting, linting, type checking)
    • Added tomli>=1.2.0 dependency for TOML support
    • Maintained backward compatibility patterns where possible

    ✅ API Transformation

    Before (old pattern):

    llm_config = LLMConfig(model="claude", api_key="abc", temperature=1)
    llm = LLM(llm_config)

    After (new pythonic pattern):

    llm = LLM(model="claude", api_key="abc", temperature=1)

    ✅ Verification Complete

    The refactoring is complete and provides the more pythonic API requested in the issue, with comprehensive serialization support and full test coverage.

    View full conversation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    openhands

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions