Skip to content

Update openai_classifier.py to add a client option#362

Open
ycysuk wants to merge 1 commit into
2FastLabs:mainfrom
ycysuk:patch-1
Open

Update openai_classifier.py to add a client option#362
ycysuk wants to merge 1 commit into
2FastLabs:mainfrom
ycysuk:patch-1

Conversation

@ycysuk

@ycysuk ycysuk commented Jul 22, 2025

Copy link
Copy Markdown

Feature request: add openai compatiable API to OpenAIClassifier

Fixes #361

Summary

Changes

Add a client option to pass by a openai compatiable API, same as OpenAIAgent

class OpenAIClassifierOptions:
    def __init__(self,
                 api_key: str,
                 model_id: Optional[str] = None,
                 inference_config: Optional[Dict[str, Any]] = None,
                 client: Optional[Any] = None):
        self.api_key = api_key
        self.model_id = model_id
        self.inference_config = inference_config or {}
        self.client = client

class OpenAIClassifier(Classifier):
    def __init__(self, options: OpenAIClassifierOptions):
        ...
        if options.client:
            self.client = options.client
        else:
            self.client = OpenAI(api_key=options.api_key)
        ...

User experience

client = OpenAI(api_key='your api key', base_url='openai compatiable api')
classifier=OpenAIClassifier(OpenAIClassifierOptions(
            api_key=api_key,
            model_id='your model id',
            client=client))

Checklist

If your change doesn't seem to apply, please leave them unchecked.

  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented
  • I have linked this PR to an existing issue (required)
Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Add `client` option to pass by a openai compatiable API, same as `OpenAIAgent`
@cornelcroi

Copy link
Copy Markdown
Collaborator

Hey @ycysuk, the client injection approach is the right design. One correctness issue: the api_key required check still fires even when a custom client is provided. Users who pass their own client instance shouldn't need to supply a dummy key — the guard should be if not options.client and not options.api_key: raise ValueError(...). Also missing a docs update for the OpenAI classifier page showing how to use the new option.

CI has not run yet, please make sure it passes before requesting re-review.

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.

Feature request: add openai compatiable API to OpenAIClassifier

2 participants