Skip to content

Conversation

@poshinchen
Copy link
Contributor

@poshinchen poshinchen commented Dec 18, 2025

Description

Allow customer to pass custom client to OpenAIModel and GeminiModel. This solves the issue of [BUG] OpenAI Implementation does not support passing custom HTTP client.

Note

  1. It will be customer's responsibility to close the client as SDK will not close it.
  2. Based on the discussion in the main issue, we would extend client to be a client_factory in the future. Just as of now, this change only support the single client.

Example

Users can now pass custom http client to the OpenAIModel and GeminiModel. Below is the example of the OpenAIModel.

import asyncio
import httpx
from strands import Agent
from strands.models.openai import OpenAIModel

client = httpx.AsyncClient()

agent = Agent(
    model=OpenAIModel(
        model_id="gpt-4o-mini-2024-07-18",
        client=client,
        client_args = {
           "api_key": "your-openai-key"
        }
    )
)

async def chat(prompt: str):
    result = await agent.invoke_async(prompt)
    print(result)

async def main():
    await chat("this works")
    await chat("this should work too")
    # close the client
    client.close()

if __name__ == "__main__":
    asyncio.run(main())

Related Issues

#1036
Some parts of #1103

Documentation PR

Will update the documentation for this change.

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link

codecov bot commented Dec 18, 2025

Codecov Report

❌ Patch coverage is 82.00000% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/strands/models/openai.py 76.31% 3 Missing and 6 partials ⚠️

📢 Thoughts on this report? Let us know!

@poshinchen poshinchen force-pushed the feat/allow-openai-model-custom-client branch from bf7af85 to 5448261 Compare December 18, 2025 20:54
@github-actions github-actions bot removed the size/m label Dec 18, 2025
@poshinchen poshinchen marked this pull request as ready for review December 18, 2025 20:57
@poshinchen poshinchen requested a review from a team December 18, 2025 20:57
@poshinchen poshinchen force-pushed the feat/allow-openai-model-custom-client branch from 5448261 to 0734f39 Compare December 18, 2025 21:46
@github-actions github-actions bot added size/m and removed size/m labels Dec 18, 2025
@poshinchen poshinchen force-pushed the feat/allow-openai-model-custom-client branch from 0734f39 to 3208acf Compare December 18, 2025 21:46
@github-actions github-actions bot added size/m and removed size/m labels Dec 18, 2025
Copy link

@AraiYuno AraiYuno left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Copy link
Member

@zastrowm zastrowm left a comment

Choose a reason for hiding this comment

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

One question/comment about the close method, but otherwise approved

@poshinchen poshinchen force-pushed the feat/allow-openai-model-custom-client branch from 3208acf to 1463df0 Compare December 19, 2025 17:32
@github-actions github-actions bot removed the size/m label Dec 19, 2025
zastrowm
zastrowm previously approved these changes Dec 19, 2025
@poshinchen poshinchen force-pushed the feat/allow-openai-model-custom-client branch from b805bfc to 7eb595e Compare December 19, 2025 18:52
@github-actions github-actions bot added size/m and removed size/m labels Dec 19, 2025
@poshinchen poshinchen deployed to auto-approve December 19, 2025 18:52 — with GitHub Actions Active
Copy link
Member

@dbschmigelski dbschmigelski left a comment

Choose a reason for hiding this comment

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

nit: lets just make sure the commit message also reflects that we are updating gemini too

@poshinchen poshinchen force-pushed the feat/allow-openai-model-custom-client branch from 7eb595e to 237357d Compare December 19, 2025 19:20
@github-actions github-actions bot added size/m and removed size/m labels Dec 19, 2025
@poshinchen poshinchen force-pushed the feat/allow-openai-model-custom-client branch from 237357d to 9c98024 Compare December 19, 2025 19:25
@github-actions github-actions bot removed the size/m label Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants