Skip to content

feat(gemini): add topK generation parameter support#961

Open
cyppe wants to merge 1 commit intoprism-php:mainfrom
cyppe:feature/add-top-k-support
Open

feat(gemini): add topK generation parameter support#961
cyppe wants to merge 1 commit intoprism-php:mainfrom
cyppe:feature/add-top-k-support

Conversation

@cyppe
Copy link
Contributor

@cyppe cyppe commented Mar 18, 2026

Summary

  • Adds topK as a first-class generation parameter via usingTopK(int $topK), following the same pattern as topP and temperature
  • Wires topK into all three Gemini handlers (Text, Stream, Structured) within the generationConfig object
  • The Gemini API supports topK natively in generationConfig alongside topP, temperature, and maxOutputTokens

Changes

  • src/Concerns/ConfiguresModels.php — Added $topK property and usingTopK() fluent method
  • src/Text/Request.php / src/Structured/Request.php — Added $topK constructor parameter and topK() accessor
  • src/Text/PendingRequest.php / src/Structured/PendingRequest.php — Pass topK through to Request
  • src/Providers/Gemini/Handlers/Text.php, Stream.php, Structured.php — Added 'topK' => $request->topK() to generationConfig
  • docs/core-concepts/text-generation.md — Documented usingTopK parameter

Usage

$response = Prism::text()
    ->using(Provider::Gemini, 'gemini-2.0-flash')
    ->withPrompt('Hello')
    ->usingTopK(40)
    ->asText();

Test plan

  • Added tests for topK in Gemini Text handler (sends topK, omits when not set, works alongside other params)
  • Added test for topK in Gemini Stream handler
  • Added test for topK in Gemini Structured handler
  • Added unit tests for topK in Text and Structured PendingRequest
  • Updated existing Request constructor tests to include topK parameter
  • Full test suite passes (1469 tests, 0 failures)
  • Laravel Pint code style passes
  • PHPStan static analysis passes (0 errors)

🤖 Generated with Claude Code

Add topK as a first-class generation parameter alongside temperature
and topP. Wire it into all three Gemini handlers (Text, Stream,
Structured) via the generationConfig object.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant