Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/actions/generate-gaos/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Composite action that installs the Speakeasy generator (with caching) and
# runs ./scripts/generate_gaos.sh to generate google/genai/_gaos/ from
# .speakeasy/openapi.json.
name: Generate GAOS SDK
description: Install the Speakeasy generator and generate google/genai/_gaos/.

inputs:
speakeasy-license-token:
description: Speakeasy license token or API key.
required: true
speakeasy-version:
description: Speakeasy generator git tag.
required: false
default: v2.959.1

runs:
using: composite
steps:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24'

- name: Cache Speakeasy Generator
id: cache-generator
uses: actions/cache@v4
with:
path: ~/go/bin/generate
key: speakeasy-generator-${{ inputs.speakeasy-version }}

- name: Install Speakeasy Generator
if: steps.cache-generator.outputs.cache-hit != 'true'
shell: bash
run: |
git clone --depth 1 --branch "${{ inputs.speakeasy-version }}" https://github.com/speakeasy-api/openapi-generation.git /tmp/openapi-generation
cd /tmp/openapi-generation
go install ./cmd/generate
rm -rf /tmp/openapi-generation

- name: Generate GAOS SDK
shell: bash
env:
SPEAKEASY_LICENSE_TOKEN: ${{ inputs.speakeasy-license-token }}
run: |
./scripts/generate_gaos.sh
36 changes: 0 additions & 36 deletions .github/workflows/import.yml

This file was deleted.

39 changes: 0 additions & 39 deletions .github/workflows/mypy.yml

This file was deleted.

54 changes: 54 additions & 0 deletions .github/workflows/sync-gaos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Commits the GAOS SDK generated by a successful `Tests` run on main back to
# main, since google/genai/_gaos/ is not exported from Piper.
#
# This reuses the `gaos-sdk` artifact from that run instead of generating the
# SDK a second time, and only runs after its tests pass.
name: Sync GAOS SDK on Main

on:
workflow_run:
workflows: [Tests]
types: [completed]
branches: [main]

jobs:
commit:
if: >-
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push'
runs-on: ubuntu-latest
permissions:
contents: write
actions: read
steps:
- name: Checkout the tested commit
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}

- name: Replace GAOS SDK with the generated one
run: rm -rf google/genai/_gaos

- name: Download GAOS SDK from the Tests run
uses: actions/download-artifact@v4
with:
name: gaos-sdk
path: google/genai/_gaos
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Commit and push changes if updated
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A google/genai/_gaos/
if git diff --staged --quiet; then
echo "No changes to commit. GAOS SDK is up to date."
exit 0
fi
git commit -m "chore: regenerate GAOS SDK [skip ci]"
# Fast-forward only. If main has moved on, its own Tests run will
# trigger a newer sync, so skip rather than push a stale SDK.
if ! git push origin HEAD:main; then
echo "main moved past ${{ github.event.workflow_run.head_sha }}; skipping."
fi
108 changes: 108 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Generates the GAOS (Interactions) SDK once per run and then runs mypy and the
# import test against it.
#
# Note: the job ids (`mypy`, `test-import`) determine the check-run names
# ("mypy (3.11)", "test-import (3.11)", ...) that copy.bara.sky requires, so
# keep them stable.
name: Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
generate-gaos:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Generate GAOS SDK
uses: ./.github/actions/generate-gaos
with:
speakeasy-license-token: ${{ secrets.SPEAKEASY_LICENSE_TOKEN }}

- name: Upload GAOS SDK
uses: actions/upload-artifact@v4
with:
name: gaos-sdk
path: google/genai/_gaos/
if-no-files-found: error
retention-days: 1

mypy:
needs: generate-gaos
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11', '3.12', '3.13', '3.14', '3.15']
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Replace GAOS SDK with the generated one
run: rm -rf google/genai/_gaos

- name: Download GAOS SDK
uses: actions/download-artifact@v4
with:
name: gaos-sdk
path: google/genai/_gaos

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mypy
sudo apt-get update && sudo apt-get install -y libjpeg-dev zlib1g-dev
pip install -r requirements.txt

- name: Run mypy ${{ matrix.python-version }}
run: mypy google/genai/

test-import:
needs: generate-gaos
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11', '3.12', '3.13', '3.14', '3.15']
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Replace GAOS SDK with the generated one
run: rm -rf google/genai/_gaos

- name: Download GAOS SDK
uses: actions/download-artifact@v4
with:
name: gaos-sdk
path: google/genai/_gaos

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel
pip install pytest
pip install .

- name: Run import test
run: |
pytest google/genai/tests/imports/test_no_optional_imports.py
107 changes: 107 additions & 0 deletions .speakeasy/addons/model_validator/types/interactions/interaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# pylint: skip-file
from typing import Any, List
import pydantic


@classmethod
def _maybe_coerce_outputs(cls, data: Any) -> Any:
legacy_lyria_models = ("lyria-3-pro-preview", "lyria-3-clip-preview")
if not isinstance(data, dict):
return data
if (
not isinstance(data.get("model"), str)
or data["model"] not in legacy_lyria_models
):
return data
if "steps" in data:
return data
if "outputs" not in data:
return {**data, "steps": []}
outputs = data["outputs"]
if not isinstance(outputs, list):
return {**data, "steps": []}

coerced = {**data}
coerced.pop("outputs")
coerced["steps"] = [{"type": "model_output", "content": outputs}]
return coerced


@pydantic.model_validator(mode="before")
@classmethod
def _coerce_outputs_to_steps(cls, data: Any) -> Any:
return cls._maybe_coerce_outputs(data)


# Applied to `Interaction(BaseModel)`: use `super().model_construct(...)`
# rather than `pydantic.BaseModel.model_construct(...)` so `cls` stays bound to
# `Interaction` through its MRO.
@classmethod
def model_construct(cls, _fields_set=None, **values):
# Coerce legacy lyria `outputs` -> `steps` here as well: validators
# do not run on model_construct (used by deferred SSE parsing).
return super().model_construct(
_fields_set, **cls._maybe_coerce_outputs(values)
)


@pydantic.model_validator(mode="after")
def _populate_output_helpers(self):
steps = self.steps if isinstance(self.steps, list) else []

text_parts: List[str] = []
collecting = False
for step in reversed(steps):
step_type = getattr(step, "type", None)
if step_type == "user_input":
break
if step_type == "thought":
continue
if step_type != "model_output":
if collecting:
break
continue
content = getattr(step, "content", None)
if not isinstance(content, list):
continue
should_stop = False
for item in reversed(content):
if getattr(item, "type", None) == "text":
collecting = True
text = getattr(item, "text", None)
text_parts.append(text if isinstance(text, str) else "")
elif collecting:
should_stop = True
break
if should_stop:
break
# Always derived from the trailing model-output text; a str, "" when none.
self.output_text = "".join(reversed(text_parts))

output_image = None
output_audio = None
output_video = None
for step in reversed(steps):
step_type = getattr(step, "type", None)
if step_type == "user_input":
break
if step_type != "model_output":
continue
content = getattr(step, "content", None)
if not isinstance(content, list):
continue
for item in reversed(content):
content_type = getattr(item, "type", None)
if content_type == "image" and output_image is None:
output_image = item
if content_type == "audio" and output_audio is None:
output_audio = item
if content_type == "video" and output_video is None:
output_video = item
if output_image is not None and self.output_image is None:
self.output_image = output_image
if output_audio is not None and self.output_audio is None:
self.output_audio = output_audio
if output_video is not None and self.output_video is None:
self.output_video = output_video
return self
Loading
Loading