Skip to content

Commit 9030945

Browse files
authored
Merge pull request #21 from firefliesai/chore-sc-70927-add-gemini-vertex-api-support
feat: add gemini vertex api support
2 parents 26a756c + 882bb56 commit 9030945

File tree

7 files changed

+107
-13
lines changed

7 files changed

+107
-13
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to Schema Forge will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.2] - 2025-03-25
9+
10+
1. Publish `jsonSchemaToGeminiOldTool`, `jsonSchemaToGeminiOldResponseSchema` that whould be published in 1.0.0.
11+
2. Support the tool format and responseSchema format of google vertex API, `@google-cloud/vertexai`. Supply the following functions
12+
1. `jsonSchemaToGeminiVertexTool`
13+
2. `jsonSchemaToGeminiVertexResponseSchema`
14+
3. `classToGeminiVertexResponseSchema`
15+
4. `classToGeminiVertexTool`
16+
817
## [1.0.1] - 2025-03-25
918

1019
1. Improve README

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,33 @@ if (message.content[0].type === 'tool_use') {
287287
#### Google Gemini
288288

289289
```typescript
290-
import { classToGeminiTool, classToGeminiOldTool } from '@firefliesai/schema-forge';
290+
import { classToGeminiTool, classToGeminiOldTool, classToGeminiVertexTool } from '@firefliesai/schema-forge';
291291

292292
/** Use with @google/genai */
293-
const tool = classToGeminiTool(UserInput);
293+
const geminiTool = classToGeminiTool(UserInput);
294294
const response = await geminiClient.models.generateContent({
295295
model: 'gemini-2.0-flash-001',
296296
contents: userMessage,
297297
config: {
298-
tools: [{ functionDeclarations: [tool] }],
298+
tools: [{ functionDeclarations: [geminiTool] }],
299299
},
300300
});
301301

302302
/** Use with Google @google/generative-ai */
303-
const geminiTool = classToGeminiOldTool(UserInput);
303+
const geminiOldTool = classToGeminiOldTool(UserInput);
304304
const model = geminiOldClient.getGenerativeModel({
305305
model: "gemini-2.0-flash-001",
306-
tools: { functionDeclarations: [geminiTool] },
306+
tools: { functionDeclarations: [geminiOldTool] },
307307
});
308308
const result = await model.generateContent([userMessage]);
309+
310+
/** Use with Google @google-cloud/vertexai */
311+
const geminiVertexTool = classToGeminiVertexTool(UserInput);
312+
const vertexModel = geminiOldClient.getGenerativeModel({
313+
model: "gemini-2.0-flash-001",
314+
tools: { functionDeclarations: [geminiVertexTool] },
315+
});
316+
const resp = await generativeModel.generateContent(prompt);
309317
```
310318

311319
## Advanced Usage
@@ -678,6 +686,8 @@ roles: string[];
678686
- `classToGeminiResponseSchema(target, options?)`: Generates Gemini response schema for new `@google/genai` API
679687
- `classToGeminiOldTool(target, options?)`: Generates Google Gemini tool format for legacy `@google/generative-ai` API
680688
- `classToGeminiOldResponseSchema(target, options?)`: Generates Gemini response schema for legacy `@google/generative-ai` API
689+
- `classToGeminiVertexTool(target, options?)`: Generates Google Gemini tool format for `@google-cloud/vertexai` API *(Note: functionality implemented but not verified with real API calls)*
690+
- `classToGeminiVertexResponseSchema(target, options?)`: Generates Gemini response schema for `@google-cloud/vertexai` API *(Note: functionality implemented but not verified with real API calls)*
681691

682692
#### JSON Schema to LLM Format Converters
683693

@@ -686,9 +696,13 @@ roles: string[];
686696
- `jsonSchemaToOpenAIResponseFormat(schema, metadata, options?)`: Converts JSON Schema to OpenAI response format for Chat Completions API
687697
- `jsonSchemaToOpenAIResponseApiTextSchema(schema, metadata, options?)`: Converts JSON Schema to OpenAI text format for Response API
688698
- `jsonSchemaToAnthropicTool(schema, metadata)`: Converts JSON Schema to Anthropic Claude tool format
689-
- `jsonSchemaToGeminiTool(schema, metadata)`: Converts JSON Schema to Google Gemini tool format
690-
- `jsonSchemaToGeminiResponseSchema(schema, metadata)`: Converts JSON Schema to Gemini response schema format
691-
699+
- `jsonSchemaToGeminiTool(schema, metadata)`: Converts JSON Schema to Google Gemini tool format for new `@google/genai` API
700+
- `jsonSchemaToGeminiResponseSchema(schema, metadata)`: Converts JSON Schema to Gemini response schema format for new `@google/genai` API
701+
- `jsonSchemaToGeminiOldTool(schema, metadata)`: Converts JSON Schema to Google Gemini old tool format for legacy `@google/generative-ai` API
702+
- `jsonSchemaToGeminiOldResponseSchema(schema, metadata)`: Converts JSON Schema to Gemini old response schema format for legacy `@google/generative-ai` API
703+
- `jsonSchemaToGeminiVertexTool(schema, metadata)`: Converts JSON Schema to Gemini tool format for `@google-cloud/vertexai` API *(Note: functionality implemented but not verified with real API calls)*
704+
- `jsonSchemaToGeminiVertexResponseSchema(schema, metadata)`: Converts JSON Schema to Gemini response schema format for `@google-cloud/vertexai` API *(Note: functionality implemented but not verified with real API calls)*
705+
692706
#### LLM Format to JSON Schema Converters
693707

694708
- `openAIToolToJsonSchema(openAITool)`: Extracts JSON Schema and metadata from an OpenAI Chat Completions API tool
@@ -821,13 +835,14 @@ Google provides multiple API packages for working with Gemini models. Schema For
821835
3. **Google Vertex AI**: `@google-cloud/vertexai`
822836
- Enterprise API for Google Cloud Platform
823837
- Requires GCP project and location settings
824-
- Uses the same formats as the new `@google/genai` library
838+
- Use `classToGeminiVertexTool` and `classToGeminiVertexResponseSchema`
839+
- **Note**: Vertex AI support is implemented but not verified with actual API calls yet
840+
- **Important**: Google may eventually deprecate this API in favor of `@google/genai`
825841
- Example:
826842
```typescript
827843
import { VertexAI } from '@google-cloud/vertexai';
828844
const vertexAI = new VertexAI({project, location});
829-
// Same format which is supposed to works, but not verified yet.
830-
const tool = classToGeminiTool(MyClass);
845+
const tool = classToGeminiVertexTool(MyClass);
831846
```
832847
833848
#### Anthropic Claude

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@firefliesai/schema-forge",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"main": "dist/lib/index.js",
55
"types": "dist/lib/index.d.ts",
66
"exports": {
@@ -95,4 +95,4 @@
9595
"peerDependencies": {
9696
"reflect-metadata": "^0.2.2"
9797
}
98-
}
98+
}

src/lib/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ export {
2020
classToGeminiOldTool,
2121
classToGeminiResponseSchema,
2222
classToGeminiTool,
23+
classToGeminiVertexResponseSchema,
24+
classToGeminiVertexTool,
2325
classToOpenAIResponseApiTextSchema,
2426
classToOpenAIResponseApiTool,
2527
classToOpenAIResponseFormatJsonSchema,
2628
classToOpenAITool,
2729
// Direct JSON Schema converters
2830
jsonSchemaToAnthropicTool,
31+
jsonSchemaToGeminiOldResponseSchema,
32+
jsonSchemaToGeminiOldTool,
2933
jsonSchemaToGeminiResponseSchema,
3034
jsonSchemaToGeminiTool,
35+
jsonSchemaToGeminiVertexResponseSchema,
36+
jsonSchemaToGeminiVertexTool,
3137
jsonSchemaToOpenAIResponseApiTextSchema,
3238
jsonSchemaToOpenAIResponseApiTool,
3339
jsonSchemaToOpenAIResponseFormat,

src/lib/llm-formats.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
GeminiResponseSchemaOptions,
1212
GeminiToolFunction,
1313
GeminiToolOptions,
14+
GeminiVertexResponseSchema,
15+
GeminiVertexToolFunction,
1416
JSONSchemaDefinition,
1517
OpenAIResponseApiTextSchema,
1618
OpenAIResponseApiToolFunction,
@@ -231,6 +233,14 @@ export function jsonSchemaToGeminiOldTool(
231233
};
232234
}
233235

236+
/** @google-cloud/vertexai */
237+
export function jsonSchemaToGeminiVertexTool(
238+
schema: JSONSchemaDefinition,
239+
metadata: { name: string; description?: string },
240+
): GeminiVertexToolFunction {
241+
return jsonSchemaToGeminiTool(schema, metadata) as unknown as GeminiVertexToolFunction;
242+
}
243+
234244
/**
235245
* Converts a JSON Schema to Gemini response schema format (@google/genai)
236246
*
@@ -272,6 +282,17 @@ export function jsonSchemaToGeminiOldResponseSchema(
272282
};
273283
}
274284

285+
// @google-cloud/vertexai
286+
export function jsonSchemaToGeminiVertexResponseSchema(
287+
schema: JSONSchemaDefinition,
288+
metadata: { description?: string },
289+
): GeminiVertexResponseSchema {
290+
return jsonSchemaToGeminiResponseSchema(
291+
schema,
292+
metadata,
293+
) as unknown as GeminiVertexResponseSchema;
294+
}
295+
275296
/**
276297
* Creates an OpenAI-compatible tool function from a class
277298
*
@@ -535,6 +556,14 @@ export function classToGeminiOldTool<T extends object>(
535556
});
536557
}
537558

559+
// @google-cloud/vertexai
560+
export function classToGeminiVertexTool<T extends object>(
561+
target: new (...args: any[]) => T,
562+
options?: GeminiToolOptions<T>,
563+
): GeminiVertexToolFunction {
564+
return classToGeminiTool(target, options) as unknown as GeminiVertexToolFunction;
565+
}
566+
538567
/**
539568
* Creates an Anthropic-compatible tool function from a class
540569
*
@@ -643,6 +672,14 @@ export function classToGeminiOldResponseSchema<T extends object>(
643672
});
644673
}
645674

675+
/** @google-cloud/vertexai */
676+
export function classToGeminiVertexResponseSchema<T extends object>(
677+
target: new (...args: any[]) => T,
678+
options?: GeminiResponseSchemaOptions<T>,
679+
): GeminiVertexResponseSchema {
680+
return classToGeminiResponseSchema(target, options) as unknown as GeminiVertexResponseSchema;
681+
}
682+
646683
/**
647684
* Extracts JSON Schema and metadata from an OpenAI Chat Completions API tool format
648685
*

src/lib/types.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Types for schema-forge
33
*/
4+
import { SchemaType as SchemaVertexType } from './vertex-api.type';
45

56
/** @google/generative-ai */
67
export enum SchemaType {
@@ -128,6 +129,20 @@ export interface GeminiOldToolFunction {
128129
};
129130
}
130131

132+
// @google-cloud/vertexai
133+
export interface GeminiVertexToolFunction {
134+
name: string;
135+
description?: string;
136+
parameters: {
137+
// json schema like structure,
138+
// Google Gemini (@google-cloud/vertexai) prefers capital 'OBJECT' instead of 'object'
139+
type: SchemaVertexType.OBJECT;
140+
description?: string;
141+
properties: Record<string, any>;
142+
required?: string[];
143+
};
144+
}
145+
131146
/** Google Gemini (@google/genai):
132147
* json schema like, except object becoming capital 'OBJECT */
133148
export interface GeminiResponseSchema {
@@ -145,6 +160,14 @@ export interface GeminiOldResponseSchema {
145160
required?: string[];
146161
}
147162

163+
// @google-cloud/vertexai
164+
export interface GeminiVertexResponseSchema {
165+
description?: string;
166+
type: SchemaVertexType.OBJECT;
167+
properties: Record<string, any>;
168+
required?: string[];
169+
}
170+
148171
// Anthropic Tool Function format
149172
export interface AnthropicToolFunction {
150173
name: string;

src/lib/vertex-api.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @google-cloud/vertexai
2+
export enum SchemaType {
3+
OBJECT = 'OBJECT',
4+
}

0 commit comments

Comments
 (0)