Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Handle sync guardrail calls to avoid awaitable error#21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gabor-openai merged 1 commit intomainfromdev/steven/sync_client_bug
Oct 16, 2025

Conversation

@steven10a
Copy link
Collaborator

Fixes reportedbug where using a sync client caused aTypeError: object ChatCompletion can't be used in 'await' expression error when LLM based guardrails are executed.

  • Root cause was the guardrail LLM is initialized as the same class as the actual client and we run our guardrails asynchronously.
  • Fix: Wrap sync methods with blocking callables in a background executor allowing all guardrails to continue to execute asynchronously

Changes:

  • Helper method_invoke_openai_callable added to properly handle sync and async traffic from all api endpoints we support
  • All LLM based checks now use the new helper
  • Added unit tests to catch this in the future

CopilotAI review requested due to automatic review settingsOctober 16, 2025 17:39
Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where using synchronous OpenAI clients causedTypeError: object ChatCompletion can't be used in 'await' expression errors in LLM-based guardrails. The root cause was that guardrail LLMs were initialized as the same class as the actual client, but guardrails always execute asynchronously. The fix wraps synchronous methods in a background executor to allow uniform async execution.

Key changes:

  • Added_invoke_openai_callable helper to handle both sync and async OpenAI SDK methods
  • Updated all LLM-based checks to use the new helper for calling OpenAI APIs
  • Added unit tests to verify sync client support

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
FileDescription
src/guardrails/checks/text/llm_base.pyImplements_invoke_openai_callable and_request_chat_completion helpers; updatesrun_llm to support sync/async clients
src/guardrails/checks/text/prompt_injection_detection.pyUpdates_call_prompt_injection_detection_llm to use_invoke_openai_callable
src/guardrails/checks/text/hallucination_detection.pyUpdateshallucination_detection to use_invoke_openai_callable
tests/unit/checks/test_llm_base.pyAdds test for sync client support inrun_llm
tests/unit/checks/test_prompt_injection_detection.pyAdds test for sync response handling in prompt injection detection

Tip: Customize your code reviews with copilot-instructions.md.Create the file orlearn how to get started.

else:
try:
fromopenaiimportAsyncAzureOpenAI,AzureOpenAI# type: ignore
exceptException:# pragma: no cover - optional dependency

Choose a reason for hiding this comment

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

The bareexcept Exception is too broad. Consider catchingImportError specifically since this is handling an optional dependency import.

Suggested change
exceptException:# pragma: no cover - optional dependency
exceptImportError:# pragma: no cover - optional dependency

Copilot uses AI. Check for mistakes.
Comment on lines +41 to +42
defcreate(self,**kwargs:Any)->Any:
_=kwargs

Choose a reason for hiding this comment

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

The unused variable assignment_ = kwargs is unnecessary. If the intention is to indicate kwargs are intentionally unused, this can be omitted as Python allows unused parameters.

Copilot uses AI. Check for mistakes.
Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

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

This is just in a test, I think it's fine


class_SyncResponses:
defparse(self,**kwargs:Any)->Any:
_=kwargs

Choose a reason for hiding this comment

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

The unused variable assignment_ = kwargs is unnecessary. If the intention is to indicate kwargs are intentionally unused, this can be omitted as Python allows unused parameters.

Suggested change
_ = kwargs

Copilot uses AI. Check for mistakes.
Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

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

This is just in a test, I think it's fine

@gabor-openaigabor-openai self-requested a reviewOctober 16, 2025 17:43
Copy link
Collaborator

@gabor-openaigabor-openai left a comment

Choose a reason for hiding this comment

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

Thank you!

@gabor-openaigabor-openai merged commit515bd41 intomainOct 16, 2025
3 checks passed
@gabor-openaigabor-openai deleted the dev/steven/sync_client_bug branchOctober 16, 2025 17:44
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotCopilot left review comments

@gabor-openaigabor-openaigabor-openai approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@steven10a@gabor-openai

[8]ページ先頭

©2009-2025 Movatter.jp