Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry Bad request 'content' property is required #244

Closed
Guiforge opened this issue Dec 1, 2023 · 0 comments · Fixed by #245
Closed

Retry Bad request 'content' property is required #244

Guiforge opened this issue Dec 1, 2023 · 0 comments · Fixed by #245

Comments

@Guiforge
Copy link
Contributor

Guiforge commented Dec 1, 2023

Thank you for this lib ! 🚀
Describe the bug
When there is a retry, a 400 error is raised from the API. The error message states that the 'content' property is required in the 'messages.1' object.

To Reproduce

Try create extraction will failed

import os

import dotenv
import instructor
from openai import AzureOpenAI
from pydantic import BaseModel

dotenv.load_dotenv()

azure_openai_client = AzureOpenAI(
    api_key=os.environ["AZURE_OPENAI_KEY"],
    api_version="2023-12-01-preview",
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)

client: AzureOpenAI = instructor.patch(azure_openai_client)

class UserDetail(BaseModel):
    name: str

user = client.chat.completions.create(
    model=os.environ["AZURE_OPENAI_DEPLOYMENT"],  # gpt-35-turbo
    response_model=UserDetail,
    messages=[
        {"role": "user", "content": "Extract nothing"},
    ],
    temperature=0.0,
)
$> python poc.py
Traceback (most recent call last):
  File "/private/tmp/poc/poc.py", line 23, in <module>
    user = client.chat.completions.create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/instructor/patch.py", line 260, in new_chatcompletion_sync
    response = retry_sync(
               ^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/instructor/patch.py", line 186, in retry_sync
    response = func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/openai/_utils/_utils.py", line 301, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 598, in create
    return self._post(
           ^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 1063, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 842, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 885, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "'content' is a required property - 'messages.1'", 'type': 'invalid_request_error', 'param': None, 'code': None}}

Expected behavior
I imagine we prefer Specific error or JsonDecode error or Pydantic validation error

$> python poc.py
Traceback (most recent call last):
  File "/private/tmp/poc/poc.py", line 26, in <module>
    user = client.chat.completions.create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/instructor/patch.py", line 263, in new_chatcompletion_sync
    response = retry_sync(
               ^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/instructor/patch.py", line 209, in retry_sync
    raise e
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/instructor/patch.py", line 191, in retry_sync
    return process_response(
           ^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/instructor/patch.py", line 128, in process_response
    model = response_model.from_response(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/instructor/function_calls.py", line 218, in from_response
    return cls.model_validate_json(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/poc/.venv/lib/python3.11/site-packages/pydantic/main.py", line 532, in model_validate_json
    return cls.__pydantic_validator__.validate_json(json_data, strict=strict, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 2 validation errors for UserDetail
name
  Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.5/v/missing
age
  Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.5/v/missing

Idea 💡

These lines :

kwargs["messages"].append(dump_message(response.choices[0].message))  # type: ignore
kwargs["messages"].append(response.choices[0].message)
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 a pull request may close this issue.

1 participant