instructor icon indicating copy to clipboard operation
instructor copied to clipboard

AttributeError: 'NoneType' object has no attribute 'upper'

Open vladislavkoz opened this issue 9 months ago • 4 comments

  • [ ] This is actually a bug report.

What Model are you using?

  • [ ] gemini-2.0-flash

instructor==1.7.8, google-genai==1.9.0

Describe the bug I use the following client self.client = instructor.from_genai( client=genai.Client( vertexai=True, project=app_config.gcp_project_id, location=app_config.gcp_vertex_region, ), mode=self.extractor_config.gemini.instructor_mode, # InstructorMode.GENAI_STRUCTURED_OUTPUTS )

Also, I use "self.client.chat.completions.create_with_completion" with response_model: Pydantic model. My response model contains Optional fields: field_1: Optional[str] = None

To Reproduce Call create_with_completion() func.

Screenshots

Image Image

I assume it's because google doesn't support optional fields. Just want to confirm that it's the reason.

Side note, in addition to this, I had to add "jsonref" to my dependencies. Not sure whether it's instructor or "google-genai" thing.

vladislavkoz avatar Apr 02 '25 10:04 vladislavkoz

BTW, it was possible to use Optional fields with "google-generativeai" client + InstructorMode.GEMINI_JSON

Now with "google-genai" only: "InstructorMode.GENAI_STRUCTURED_OUTPUTS" and "InstructorMode.GENAI_TOOLS" allowed and both have issues with optional fields

I assume that it's related to -> https://github.com/instructor-ai/instructor/issues/1442

vladislavkoz avatar Apr 02 '25 10:04 vladislavkoz

Hmm Gemini_JSON was parsing the raw chat completion, that's how we got the optionals. I've tried to avoid that and instead use just structured outputs and tool calling for the genai sdk.

Could you give this a try and see if it works for you now?

from pydantic import BaseModel
from typing import Optional
from google import genai
import instructor
import os
from google import generativeai


class User(BaseModel):
    name: str
    age: Optional[int]
    nationality: Optional[str]


client = instructor.from_genai(
    genai.Client(api_key=os.getenv("GOOGLE_API_KEY")),
    mode=instructor.Mode.GENAI_STRUCTURED_OUTPUTS,
)

resp = client.chat.completions.create(
    model="gemini-2.0-flash",
    messages=[{"role": "user", "content": "Ivan is a software engineer"}],
    response_model=User,
)

print(resp)

ivanleomk avatar Apr 08 '25 12:04 ivanleomk

I didn't try it yet, will check a bit later, but I already see at least 1 diff. I use vertexAI(instead of AI studio with api key).

vladislavkoz avatar Apr 08 '25 13:04 vladislavkoz

@ivanleomk Same error with the suggested solution.

    schema_type = schema_type.upper()
AttributeError: 'NoneType' object has no attribute 'upper'

vladislavkoz avatar Apr 14 '25 13:04 vladislavkoz

Recent fixes to Gemini streaming and schema handling (commits 61718fb6, 02f4b0bb, 61dce8f7) likely resolve this. Please verify with latest version.

jxnl avatar Oct 27 '25 19:10 jxnl