Repository navigation
refactor(examples): migrate Pdf_structured_outputs_on_invoices_and_forms to the Interactions API - #1444
refactor(examples): migrate Pdf_structured_outputs_on_invoices_and_forms to the Interactions API#1444Sonica-B wants to merge 4 commits into
Conversation
…rms to the Interactions API Replace the three client.models.generate_content() calls with client.interactions.create(). The PDF is still uploaded with the File API and passed as a document input; results are requested with response_format (JSON Schema from the Pydantic models, or a dictionary in the last cell) and read through interaction.output_text and model_validate_json(). Update the prose that described the legacy schema types, and replace the Gemini 2.0 propertyOrdering example, which has no equivalent in response_format, with a dictionary-schema example. Headings and the Colab badge are unchanged apart from the last section title.
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request updates the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request updates the Pdf_structured_outputs_on_invoices_and_forms.ipynb notebook to use the new Interactions API (client.interactions.create()) instead of the legacy client.models.generate_content(). It updates the markdown explanations and code examples to demonstrate defining schemas using Pydantic models (with model_json_schema() and model_validate_json()) as well as standard JSON Schema dictionaries. Additionally, the pip installation command is updated to include -U -q. Feedback is provided regarding a line in the dictionary schema example that exceeds the maximum line length of 100 characters.
| " \"required\": [\"invoice_number\", \"date\", \"vendor\", \"total_amount\"],\n", | ||
| "}\n", | ||
| "\n", | ||
| "prompt = \"Extract invoice details: Invoice #12345 dated 2024-01-15 from Acme Corp for $1,250.00\"\n", |
There was a problem hiding this comment.
This line exceeds the maximum line length of 100 characters specified in the style guide (line 171). Please split the prompt string across multiple lines to improve readability and adhere to the style guide.
| "prompt = \"Extract invoice details: Invoice #12345 dated 2024-01-15 from Acme Corp for $1,250.00\"\n", | |
| "prompt = (\n", | |
| " \"Extract invoice details: Invoice #12345 dated 2024-01-15 \"\n", | |
| " \"from Acme Corp for $1,250.00\"\n", | |
| "),\n", |
References
- Maximum line length: 100 characters (instead of PEP 8's 79). (link)
|
@googlebot I signed it! |
Wrap the prompt in parentheses so the raw notebook line stays within the 100-character limit; the string value is unchanged.
Wrap the over-100-character prompt in an implicit string concatenation to follow the style guide line limit. The string value is unchanged.
…ed-outputs-interactions-api # Conflicts: # examples/Pdf_structured_outputs_on_invoices_and_forms.ipynb
Migrates
examples/Pdf_structured_outputs_on_invoices_and_forms.ipynbfromclient.models.generate_content()toclient.interactions.create(), per the Interactions API policy inCONTRIBUTING.mdand.gemini/styleguide.md. Same kind of change as #1340 (Role_prompting.ipynb), one notebook per PR.Changes
generate_contentcalls now useclient.interactions.create(): the text-onlyPersonexample, theextract_structured_datahelper (used for the invoice and the form), and the dictionary-schema example at the end.client.files.upload(File API, a documented exception) and passed as{"type": "document", "uri": file.uri}, the same shapequickstarts/PDF_Files.ipynbuses.response_format={"type": "text", "mime_type": "application/json", "schema": Model.model_json_schema()}as inquickstarts/JSON_mode.ipynb. Results are read withinteraction.output_textandModel.model_validate_json(...), which replacesresponse.parsed.types.Schema(..., property_ordering=[...]), agenerateContentconfig field thatresponse_format(a plain JSON Schema) has no counterpart for, and Gemini 2.0 is no longer in the model selector. It is replaced by a short "Define the schema as a dictionary" section that passes a hand-written JSON Schema toresponse_format.genai.types.Schema, bare Python types) now describesresponse_formatand links to the JSON mode quickstart.%pip installuses-U -q.model_id), the README row,client.models.count_tokensin the token-count cell (as inCounting_Tokens.ipynb), and the Invoice/Form cells.I left the older "Gemini 2.0" wording in two section headings and the other pre-existing lint warnings alone to keep the diff to the migration. Happy to clean those up in a follow-up.
How I tested
The notebook was migrated and validated structurally only:
python tools/nbfmt_cli.py --test examples/Pdf_structured_outputs_on_invoices_and_forms.ipynbpasses.python tools/nblint_cli.py --repo=google-gemini/cookbook --branch=main examples/Pdf_structured_outputs_on_invoices_and_forms.ipynb: 0 errors, 8 warnings (11 onmain), allstyle::line_lengthon lines this PR does not touch.check_readme_links.pyandcheck_all_links.pyon the notebook pass;nbformatvalidation passes; every cell keeps itsmetadata.id; the diff is one file.google-genai2.9.0 and 2.33.0 against a mocked HTTP transport and checked the request bodies (adocumentpart with the File API URI, theresponse_formatschema with$defs) and theoutput_texttomodel_validate_jsonround trip. This does not exercise the live service.Not executed end to end against the Gemini API yet: no cell has been run against the live service, which is why this is a draft. Until that run, the saved outputs of the token-count, Invoice and Form cells are the ones from the previous
generate_contentversion (their code is unchanged), and the outputs of the edited cells are cleared.Checklist
nbfmt --testandnblintpass.interaction.outputs, nomodels.generate_content, no hard-coded keys (userdata.get("GEMINI_API_KEY")).