#πŸ”’ How to properly work with Literal?

9 messages Β· Page 1 of 1 (latest)

violet wharf
#

I am trying to fix my typing coming from Pylance, but I am struggling with this one.

Argument of type "str" cannot be assigned to parameter "voice" of type "Literal['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']" in function "create"
  Type "str" is incompatible with type "Literal['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']"
    "str" is incompatible with type "Literal['alloy']"
    "str" is incompatible with type "Literal['echo']"
    "str" is incompatible with type "Literal['fable']"
    "str" is incompatible with type "Literal['onyx']"
    "str" is incompatible with type "Literal['nova']"
    "str" is incompatible with type "Literal['shimmer']"PylancereportArgumentType

Below is my snippet of code, the warning occurs at voice=self.voice.

    def speak(self, text, destination, api_key):
        client = OpenAI(api_key=api_key)
        try:
            log.log_ui(logger, "Generating speech via OpenAI")
            response = client.audio.speech.create(
                model="tts-1",
                voice=self.voice, # The error is shown here.
                input=text,
            ) 
        except AuthenticationError as err:
            log.log_ui(logger, f"OpenAI authentication error, please check you API: {err}")
            return

        response.stream_to_file(destination)
        play(destination)

The implementation from the upstream client.audio.speech.create indeed defines voice as a Literal. But I can't figure out how to pass my string as a literal value.
Does anyone have any ideas?

    def create(
        self,
        ...REDACTED...
        voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
        ...REDACTED...
    ) -> _legacy_response.HttpxBinaryResponseContent:
drowsy veldtBOT
#

@violet wharf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

burnt scarab
violet wharf
#

In the constructor of my class.

class OpenAIEngine:

def __init__(self, voice = "onyx"):
        self.voice = voice
burnt scarab
#

There's no type annotations there

violet wharf
#

I was able to work around it using an enumeration:

class OpenAIVoice(Enum):
    ONYX = "onyx"
    ECHO = "echo"
    FABLE = "fable"
    ALLOY = "alloy"
    NOVA = "nova"
    SHIMMER = "shimmer"
    def __init__(self, voice: OpenAIVoice = OpenAIVoice.ONYX):
        self.voice = voice

Not sure if that is a good solution

burnt scarab
#

You need to define it with
voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"],

drowsy veldtBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.