Skip to content

fix: add runtime reranking model support#509

Open
sanmaxdev wants to merge 1 commit into
OpenRouterTeam:mainfrom
sanmaxdev:fix/reranking-model-runtime
Open

fix: add runtime reranking model support#509
sanmaxdev wants to merge 1 commit into
OpenRouterTeam:mainfrom
sanmaxdev:fix/reranking-model-runtime

Conversation

@sanmaxdev

Copy link
Copy Markdown

Summary

  • add a runtime rerankingModel() factory to createOpenRouter()
  • implement OpenRouterRerankingModel for AI SDK v3 reranking calls
  • map OpenRouter /rerank responses into AI SDK ranking results
  • add unit coverage for provider exposure and rerank request/response mapping

Closes #500

Testing

  • pnpm typecheck
  • pnpm vitest --config vitest.node.config.ts --run src/reranking/index.test.ts
  • pnpm stylecheck
  • pnpm build

@perry-the-pr-reviewer perry-the-pr-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perry's Review

Adds runtime support for rerankingModel() on the OpenRouter provider — a new RerankingModelV3 implementation, its response schema, settings type, provider wiring, and unit tests.

Verdict: 💬 Comments / questions

Details

Risk: 🟡 Medium

CI: all passing ✅ (verified locally: tsc --noEmit clean, 430/430 vitest pass incl. 3 new reranking tests, biome clean)

Findings (see inline comments):

  • 🟡 src/reranking/index.ts:90 — provider metadata hardcoded to empty string; sibling embedding model reads it from the response
  • 🟡 .changeset/reranking-runtime-provider.md:2 — patch bump for a new public API; repo precedent (#479 videoModel) used minor

Blast radius: purely additive. New src/reranking/ module + one provider wiring line + one barrel re-export. No existing request path, auth flow, or billing logic touched. Implementation mirrors the embedding/image/video siblings 1:1 (config shape, postJsonToApi call, metadata parse, warnings: []). Verified RerankingModelV3 and ProviderV3.rerankingModel exist in @ai-sdk/provider 3.0.8 and the doRerank return shape matches the interface.

Security: no concerns — no new auth/credential handling; reuses the shared getHeaders + extraBody passthrough.

Test coverage: good — covers provider method exposure, instance creation, and a full doRerank round-trip asserting URL, request body mapping (top_n, documents), ranking transform, and cost metadata.

Unresolved threads: none

Review: tier=medium · model=claude-opus-latest · score=5.6

Comment thread src/reranking/index.ts
})),
providerMetadata: {
openrouter: OpenRouterProviderMetadataSchema.parse({
provider: '',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[suggestion] provider: '' hardcodes the provider name to empty — the sibling embedding model reads it from the response instead.

Suggested change
provider: '',
provider: responseValue.provider ?? '',
Details

Why: Every other model in this provider surfaces the upstream provider slug in providerMetadata.openrouter.provider (e.g. src/embedding/index.ts:95 does provider: responseValue.provider ?? ''). Here it is pinned to '', so callers can never tell which upstream provider (cohere, jina, etc.) actually served the rerank — useful signal that the rest of the SDK exposes consistently.

The rerank response schema (src/reranking/schemas.ts) uses .passthrough(), so a provider field returned by the API is already preserved on responseValue at runtime; the only missing piece is surfacing it. If the API does not return provider on /rerank, the ?? '' fallback keeps current behaviour, so the change is safe either way. Optionally add provider: z.string().optional() to the schema to make the field explicit.

Reviewed at bd948de

@@ -0,0 +1,5 @@
---
"@openrouter/ai-sdk-provider": patch

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[suggestion] Changeset is marked patch — a new public API method is conventionally a minor bump.

Suggested change
"@openrouter/ai-sdk-provider": patch
"@openrouter/ai-sdk-provider": minor
Details

Why: This PR adds a brand-new public surface — provider.rerankingModel() and the exported OpenRouterRerankingModel / OpenRouterRerankingSettings types. Per semver, new backwards-compatible API additions are minor, not patch. The most recent comparable change in this repo — #479, which added provider.videoModel() — shipped as a Minor changeset (see CHANGELOG.md). Marking this patch means the new method lands in a patch release, which under-signals the feature to consumers pinning on ~ ranges.

Non-blocking: if the maintainers intentionally batch feature additions into patch releases for this package, disregard.

Reviewed at bd948de

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.

rerankingModel exists in typings but is undefined at runtime

1 participant