dspy icon indicating copy to clipboard operation
dspy copied to clipboard

KeyError: 'finish_reason' in dsp/modules/gpt3.py

Open Fuyujia799 opened this issue 9 months ago • 0 comments

Hello, I encountered an issue while using the dsp/modules/gpt3.py There is a problem in the following line: https://github.com/stanfordnlp/dspy/blame/d63d24f250c698459a9737da6bd337ce3f8d349c/dsp/modules/gpt3.py#L183

completed_choices = [c for c in choices if c["finish_reason"] != "length"]

Actually, in some cases, the API response does not include the finish_reason key, which results in a KeyError.

    File "./gpt3.py", line 183, in <listcomp>
    completed_choices = [c for c in choices if c["finish_reason"] != "length"]

KeyError: 'finish_reason'

After testing, I found that this issue can be resolved by modifying the line to:

completed_choices = [c for c in choices if c.get("finish_reason", "not_length") != "length"]

The expression c.get("finish_reason", "not_length") != "length" ensures that even if the finish_reason key is missing, the code does not throw a KeyError and instead safely compares the default value "not_length".

By using the get method, I hope we can ensure that the code does not throw an error even if the finish_reason key is missing.

Thank you for looking into this issue.

Fuyujia799 avatar May 21 '24 01:05 Fuyujia799