Skip to content

refactor(examples): migrate Pdf_structured_outputs_on_invoices_and_forms to the Interactions API - #1444

Draft
Sonica-B wants to merge 4 commits into
google-gemini:mainfrom
Sonica-B:refactor/pdf-structured-outputs-interactions-api
Draft

Sonica-B wants to merge 4 commits into
google-gemini:mainfrom
Sonica-B:refactor/pdf-structured-outputs-interactions-api

Conversation

@Sonica-B

Copy link
Copy Markdown

Migrates examples/Pdf_structured_outputs_on_invoices_and_forms.ipynb from client.models.generate_content() to client.interactions.create(), per the Interactions API policy in CONTRIBUTING.md and .gemini/styleguide.md. Same kind of change as #1340 (Role_prompting.ipynb), one notebook per PR.

Changes

  • The three generate_content calls now use client.interactions.create(): the text-only Person example, the extract_structured_data helper (used for the invoice and the form), and the dictionary-schema example at the end.
  • The PDF is still uploaded with client.files.upload (File API, a documented exception) and passed as {"type": "document", "uri": file.uri}, the same shape quickstarts/PDF_Files.ipynb uses.
  • Structured output uses response_format={"type": "text", "mime_type": "application/json", "schema": Model.model_json_schema()} as in quickstarts/JSON_mode.ipynb. Results are read with interaction.output_text and Model.model_validate_json(...), which replaces response.parsed.
  • The "Property ordering with Gemini 2.0" section used types.Schema(..., property_ordering=[...]), a generateContent config field that response_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 to response_format.
  • Prose that listed the legacy schema types (genai.types.Schema, bare Python types) now describes response_format and links to the JSON mode quickstart. %pip install uses -U -q.
  • Unchanged: title, Colab badge, the model selector (variable model_id), the README row, client.models.count_tokens in the token-count cell (as in Counting_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.ipynb passes.
  • 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 on main), all style::line_length on lines this PR does not touch.
  • check_readme_links.py and check_all_links.py on the notebook pass; nbformat validation passes; every cell keeps its metadata.id; the diff is one file.
  • Replayed the edited cells through google-genai 2.9.0 and 2.33.0 against a mocked HTTP transport and checked the request bodies (a document part with the File API URI, the response_format schema with $defs) and the output_text to model_validate_json round 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_content version (their code is unchanged), and the outputs of the edited cells are cleared.

Checklist

  • CLA signed.
  • One notebook in the diff; nbfmt --test and nblint pass.
  • No interaction.outputs, no models.generate_content, no hard-coded keys (userdata.get("GEMINI_API_KEY")).

…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.
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 Pdf_structured_outputs_on_invoices_and_forms.ipynb notebook to align with the latest Interactions API standards. The changes involve replacing legacy model generation methods with the new interactions interface, updating how structured outputs are defined and validated, and ensuring the notebook documentation reflects these modern practices.

Highlights

  • API Migration: Migrated all client.models.generate_content() calls to the newer client.interactions.create() API in accordance with project policy.
  • Structured Output Updates: Updated structured output configurations to use response_format with JSON Schema, replacing legacy response_schema and response_mime_type parameters.
  • Schema Definition: Removed reliance on genai.types.Schema and explicit property_ordering in favor of standard JSON Schema dictionaries, simplifying the schema definition process.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@google-cla

google-cla Bot commented Oct 25, 2026

Copy link
Copy Markdown

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
"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
  1. Maximum line length: 100 characters (instead of PEP 8's 79). (link)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: the prompt string you flagged was split into a parenthesised multi-line string in 485e675, and e3b4b97 splits the one remaining long prompt (the Person example) the same way, with the text unchanged.

@Sonica-B

Copy link
Copy Markdown
Author

@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.
@kkorpal kkorpal self-assigned this Oct 27, 2026
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
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 this pull request may close these issues.

2 participants