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

Vertex AI fails with default value in Field #837

Closed
2 of 4 tasks
cmitsakis opened this issue Jul 14, 2024 · 0 comments · May be fixed by #846
Closed
2 of 4 tasks

Vertex AI fails with default value in Field #837

cmitsakis opened this issue Jul 14, 2024 · 0 comments · May be fixed by #846

Comments

@cmitsakis
Copy link

  • This is actually a bug report.
  • I am not getting good LLM Results
  • I have tried asking for help in the community on discord or discussions and have not received a response.
  • I have tried searching the documentation and have not found an answer.

What Model are you using?

  • Gemini 1.5 Pro (on Vertex AI)

Describe the bug
Since Vertex AI doesn't support Optional, the documentation recommends we use Field(default=0) as a workaround. But it doesn't work for me.

To Reproduce
The following code fails, but if I remove the default=0 it works.

client = instructor.from_vertexai(gm.GenerativeModel("gemini-1.5-pro"))

class UserModel(BaseModel):
    age: int = Field(description="age", default=0)

response = client.create(
    messages=[
        {
            "role": "user",
            "content": """Jason is 20 years old""",
        },
    ],
    response_model=UserModel,
)
Traceback (most recent call last):
  File "/path/to/main.py", line 27, in <module>
    response = client.create(
               ^^^^^^^^^^^^^^
  File "/path/to/venv/lib/python3.12/site-packages/instructor/client.py", line 91, in create
    return self.create_fn(
           ^^^^^^^^^^^^^^^
  File "/path/to/venv/lib/python3.12/site-packages/instructor/patch.py", line 140, in new_create_sync
    response_model, new_kwargs = handle_response_model(
                                 ^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/venv/lib/python3.12/site-packages/instructor/process_response.py", line 447, in handle_response_model
    contents, generation_config = vertexai_process_json_response(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/venv/lib/python3.12/site-packages/instructor/client_vertexai.py", line 84, in vertexai_process_json_response
    response_schema = _create_gemini_json_schema(model)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/venv/lib/python3.12/site-packages/instructor/client_vertexai.py", line 18, in _create_gemini_json_schema
    "required": schema_without_refs["required"],
                ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'required'

This is the line that causes the exception:

"required": schema_without_refs["required"],

Expected behavior
no exception

cmitsakis added a commit to cmitsakis/fork-instructor that referenced this issue Jul 16, 2024
cmitsakis added a commit to cmitsakis/fork-instructor that referenced this issue Jul 30, 2024
cmitsakis added a commit to cmitsakis/fork-instructor that referenced this issue Jul 31, 2024
@jxnl jxnl closed this as completed Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants