Skip to content

Conversation

sanveer-osahan
Copy link
Contributor

This pull request introduces support for custom language models (LLMs) by replacing the model_api_key parameter with a more flexible model_client_options parameter. It also updates the documentation, configuration, initialization logic, and tests to reflect this enhancement.

Core Changes for Custom LLM Support:

  • Configuration Updates:

    • Replaced model_api_key with model_client_options in the StagehandConfig class, allowing users to specify multiple options such as apiKey and baseUrl for custom LLMs. (stagehand/config.py, [1] [2]
  • Initialization Logic:

    • Updated the initialization logic in stagehand/main.py to handle model_client_options and extract apiKey from it, falling back to the MODEL_API_KEY environment variable if necessary. (stagehand/main.py, stagehand/main.pyL71-R74)
    • Removed redundant logic for populating model_client_options dynamically. (stagehand/main.py, stagehand/main.pyL92-L96)
  • LLM Client Enhancements:

    • Modified stagehand/llm/client.py to support baseURL in addition to api_base for custom LLM configurations. (stagehand/llm/client.py, stagehand/llm/client.pyL57-R57)

Documentation Updates:

  • README.md:
    • Added a new section on LLM customization, including an example of how to configure StagehandConfig with model_client_options for a custom LLM. (README.md, README.mdR163-R178)

Test Coverage:

  • Unit Tests:
    • Updated existing tests to replace model_api_key with model_client_options. (tests/unit/llm/test_llm_integration.py, [1]; tests/unit/test_client_api.py, [2]; tests/unit/test_client_initialization.py, [3]
    • Added new tests to validate custom LLM configurations, including overrides and environment variable fallbacks. (tests/unit/test_client_initialization.py, tests/unit/test_client_initialization.pyR206-R234)

@sanveer-osahan sanveer-osahan changed the title Adding support for LLM Customisation Adding initial support for LLM Customisation Jul 2, 2025
miguelg719
miguelg719 previously approved these changes Jul 3, 2025
@miguelg719 miguelg719 dismissed their stale review July 3, 2025 23:01

request changes

Copy link
Collaborator

@miguelg719 miguelg719 left a comment

Choose a reason for hiding this comment

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

let's address the comments, I made a branch contrib/132 that you can set as the target to merge to (to run full CI)

@sanveer-osahan sanveer-osahan changed the base branch from main to contrib/132 July 9, 2025 21:15
Co-authored-by: Miguel <36487034+miguelg719@users.noreply.github.com>
Comment on lines -92 to -96
# Handle model-related settings
self.model_client_options = {}
if self.model_api_key and "apiKey" not in self.model_client_options:
self.model_client_options["apiKey"] = self.model_api_key

Copy link
Collaborator

Choose a reason for hiding this comment

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

need to check

@laoshancun
Copy link

LGTM

@miguelg719 miguelg719 merged commit 5f29b21 into browserbase:contrib/132 Aug 19, 2025
miguelg719 added a commit that referenced this pull request Aug 19, 2025
* Adding initial support for LLM Customisation (#132)

* Adding support for LLM Customisation

* Added back model_api_key config

* Fixed README

* Update README.md

Co-authored-by: Miguel <36487034+miguelg719@users.noreply.github.com>

* Update stagehand/llm/client.py

---------

Co-authored-by: Miguel <36487034+miguelg719@users.noreply.github.com>

* Update stagehand/config.py

* minor updates

* Apply suggestions from code review

* changeset

* accept apiKey in model_client_options

* passthrough apiKey if none in model_api_key

---------

Co-authored-by: Sanveer Singh Osahan <sanveer.singh@atlan.com>
@github-actions github-actions bot mentioned this pull request Aug 19, 2025
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.

3 participants