"Invalid JSON payload" error when using Pydantic schema with Gemini
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 12.5k
- Forks
- 998
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 10
Description
I am using the following code:
from typing import List
import llm
from pydantic import BaseModel
PROMPT = """
Extract financial data and current liabilities from the balance sheet
provided in TJX's 10-K filing. Provide values for 2023 and 2024.
"""
class CapitalEmployed(BaseModel):
year: int
total_assets: str
cash: str
accounts_payable: str
merchandise_credits: str
dividends_payable: str
sales_taxes: str
other_liabilities: str
goodwill: str
class Schema(BaseModel):
items: List[CapitalEmployed]
model = llm.get_model("gemini-2.0-pro-exp-02-05")
response = model.prompt(
PROMPT,
attachments=[llm.Attachment(url="https://investor.tjx.com/static-files/051d2f5a-1839-4131-bda4-a485eb755d83")],
schema=Schema,
)
print(response.text())
I expect this code to return a list of items but I get the following error:
Traceback (most recent call last):
File "/home/jonathans/llm-scraping/test.py", line 36, in <module>
print(response.text())
File "/home/jonathans/llm-scraping/.venv/lib/python3.9/site-packages/llm/models.py", line 424, in text
self._force()
File "/home/jonathans/llm-scraping/.venv/lib/python3.9/site-packages/llm/models.py", line 421, in _force
list(self)
File "/home/jonathans/llm-scraping/.venv/lib/python3.9/site-packages/llm/models.py", line 467, in __iter__
for chunk in self.model.execute(
File "/home/jonathans/llm-scraping/.venv/lib/python3.9/site-packages/llm_gemini.py", line 344, in execute
raise llm.ModelError(event["error"]["message"])
llm.errors.ModelError: Invalid JSON payload received. Unknown name "$defs" at 'generation_config.response_schema': Cannot find field.
Invalid JSON payload received. Unknown name "$ref" at 'generation_config.response_schema.properties[0].value.items': Cannot find field.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at llm_gemini.py around execute() line 344 and trace how the Pydantic Schema becomes generation_config.response_schema. Reproduce the supplied example and inspect the resulting $defs and $ref fields; done means the request no longer raises the reported Invalid JSON payload errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- ai
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100