Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

feat: add Google Vertex AI instrumentation in TS SDK (#1280)

Linda Oraegbunam1f7150cf1293
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index d7d8b746..6dce368f 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -40,6 +40,7 @@ This project proudly follows and maintains the [Semantic Conventions](https://gi
| [✅ Groq](https://docs.openlit.io/latest/integrations/groq) |
| [✅ Mistral](https://docs.openlit.io/latest/integrations/mistral) |
| [✅ Google AI Studio](https://docs.openlit.io/latest/integrations/google-ai-studio) |
+| [✅ Google Vertex AI](https://docs.openlit.io/latest/integrations/vertex-ai) *(via `@google-cloud/vertexai`)* |
| [✅ Together AI](https://docs.openlit.io/latest/integrations/together) |
| [✅ Ollama](https://docs.openlit.io/latest/integrations/ollama) |
| [✅ AWS Bedrock](https://docs.openlit.io/latest/integrations/bedrock) |
diff --git a/sdk/typescript/package-lock.json b/sdk/typescript/package-lock.json
index 522130e5..100e3206 100644
--- a/sdk/typescript/package-lock.json
+++ b/sdk/typescript/package-lock.json
@@ -26,6 +26,7 @@
"devDependencies": {
"@digitalocean/gradient": "^0.1.0-alpha.2",
"@eslint/js": "^9.2.0",
+ "@google-cloud/vertexai": "^1.12.0",
"@types/jest": "^29.0.0",
"ai21": "^1.3.0",
"eslint": "^8.57.0",
@@ -694,6 +695,122 @@
"url": "https://eslint.org/donate"
}
},
+ "node_modules/@google-cloud/vertexai": {
+ "version": "1.12.0",
+ "resolved": "https://registry.npmjs.org/@google-cloud/vertexai/-/vertexai-1.12.0.tgz",
+ "integrity": "sha512-XMJIk7GIeavFLP5A3YEUlowKa5Y5PZRrnnuTJcqR0k+lFKkv7+IWpdRp+Xbqb8xNDrvQaE2hP2RYPUylyD5EdA==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "@google/genai": "^1.45.0",
+ "google-auth-library": "^9.1.0"
+ },
+ "engines": {
+ "node": ">=18.0.0"
+ }
+ },
+ "node_modules/@google/genai": {
+ "version": "1.52.0",
+ "resolved": "https://registry.npmjs.org/@google/genai/-/genai-1.52.0.tgz",
+ "integrity": "sha512-gwSvbpiN/17O9TbsqSsE/OzZcpv5Fo4RQjdngGgogtuB9RsyJ8ZHhX5KjHj1bp5N9snN2eK8LDGXSaWW2hof8Q==",
+ "dev": true,
+ "hasInstallScript": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "google-auth-library": "^10.3.0",
+ "p-retry": "^4.6.2",
+ "protobufjs": "^7.5.4",
+ "ws": "^8.18.0"
+ },
+ "engines": {
+ "node": ">=20.0.0"
+ },
+ "peerDependencies": {
+ "@modelcontextprotocol/sdk": "^1.25.2"
+ },
+ "peerDependenciesMeta": {
+ "@modelcontextprotocol/sdk": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@google/genai/node_modules/gaxios": {
+ "version": "7.1.5",
+ "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-7.1.5.tgz",
+ "integrity": "sha512-5FZy72Rh8LhtjmvDrKkI+lVhrsQrVKVsItxMoDm5mNQE+xR0WVIIs+jzPSJgBvKVsLi24fZhXJIsNI0bihDzFg==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "extend": "^3.0.2",
+ "https-proxy-agent": "^7.0.1",
+ "node-fetch": "^3.3.2"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@google/genai/node_modules/gcp-metadata": {
+ "version": "8.1.2",
+ "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-8.1.2.tgz",
+ "integrity": "sha512-zV/5HKTfCeKWnxG0Dmrw51hEWFGfcF2xiXqcA3+J90WDuP0SvoiSO5ORvcBsifmx/FoIjgQN3oNOGaQ5PhLFkg==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "gaxios": "^7.0.0",
+ "google-logging-utils": "^1.0.0",
+ "json-bigint": "^1.0.0"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@google/genai/node_modules/google-auth-library": {
+ "version": "10.7.0",
+ "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-10.7.0.tgz",
+ "integrity": "sha512-QpTAbNJ36TliZLx3TTtahR8HG0hN9RllL1e3FymOvQSIKK8JmgV58H924ub2wa2DsS3ANjjP1Aw1N+Ramc8hqQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "base64-js": "^1.3.0",
+ "ecdsa-sig-formatter": "^1.0.11",
+ "gaxios": "^7.1.4",
+ "gcp-metadata": "8.1.2",
+ "google-logging-utils": "1.1.3",
+ "jws": "^4.0.0"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@google/genai/node_modules/google-logging-utils": {
+ "version": "1.1.3",
+ "resolved": "https://registry.npmjs.org/google-logging-utils/-/google-logging-utils-1.1.3.tgz",
+ "integrity": "sha512-eAmLkjDjAFCVXg7A1unxHsLf961m6y17QFqXqAXGj/gVkKFrEICfStRfwUlGNfeCEjNRa32JEWOUTlYXPyyKvA==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "engines": {
+ "node": ">=14"
+ }
+ },
+ "node_modules/@google/genai/node_modules/node-fetch": {
+ "version": "3.3.2",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
+ "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "data-uri-to-buffer": "^4.0.0",
+ "fetch-blob": "^3.1.4",
+ "formdata-polyfill": "^4.0.10"
+ },
+ "engines": {
+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/node-fetch"
+ }
+ },
"node_modules/@humanwhocodes/config-array": {
"version": "0.13.0",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.13.0.tgz",
@@ -1857,6 +1974,13 @@
"undici-types": "~7.18.0"
}
},
+ "node_modules/@types/retry": {
+ "version": "0.12.0",
+ "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz",
+ "integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/@types/stack-utils": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz",
@@ -2164,6 +2288,16 @@
"node": ">=0.4.0"
}
},
+ "node_modules/agent-base": {
+ "version": "7.1.4",
+ "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz",
+ "integrity": "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">= 14"
+ }
+ },
"node_modules/ai21": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/ai21/-/ai21-1.3.0.tgz",
@@ -2449,6 +2583,16 @@
"node": ">=6.0.0"
}
},
+ "node_modules/bignumber.js": {
+ "version": "9.3.1",
+ "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.3.1.tgz",
+ "integrity": "sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": "*"
+ }
+ },
"node_modules/brace-expansion": {
"version": "1.1.13",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.13.tgz",
@@ -2530,6 +2674,13 @@
"node-int64": "^0.4.0"
}
},
+ "node_modules/buffer-equal-constant-time": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
+ "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==",
+ "dev": true,
+ "license": "BSD-3-Clause"
+ },
"node_modules/buffer-from": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
@@ -2738,6 +2889,16 @@
"node": ">= 8"
}
},
+ "node_modules/data-uri-to-buffer": {
+ "version": "4.0.1",
+ "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
+ "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">= 12"
+ }
+ },
"node_modules/debug": {
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
@@ -2843,6 +3004,16 @@
"node": ">=6.0.0"
}
},
+ "node_modules/ecdsa-sig-formatter": {
+ "version": "1.0.11",
+ "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
+ "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "safe-buffer": "^5.0.1"
+ }
+ },
"node_modules/electron-to-chromium": {
"version": "1.5.330",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.330.tgz",
@@ -3201,6 +3372,13 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
+ "node_modules/extend": {
+ "version": "3.0.2",
+ "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
+ "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
@@ -3279,6 +3457,30 @@
"bser": "2.1.1"
}
},
+ "node_modules/fetch-blob": {
+ "version": "3.2.0",
+ "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
+ "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
+ "dev": true,
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/jimmywarting"
+ },
+ {
+ "type": "paypal",
+ "url": "https://paypal.me/jimmywarting"
+ }
+ ],
+ "license": "MIT",
+ "dependencies": {
+ "node-domexception": "^1.0.0",
+ "web-streams-polyfill": "^3.0.3"
+ },
+ "engines": {
+ "node": "^12.20 || >= 14.13"
+ }
+ },
"node_modules/file-entry-cache": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
@@ -3354,6 +3556,19 @@
"node": ">= 18"
}
},
+ "node_modules/formdata-polyfill": {
+ "version": "4.0.10",
+ "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
+ "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "fetch-blob": "^3.1.2"
+ },
+ "engines": {
+ "node": ">=12.20.0"
+ }
+ },
"node_modules/fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@@ -3385,6 +3600,38 @@
"url": "https://github.com/sponsors/ljharb"
}
},
+ "node_modules/gaxios": {
+ "version": "6.7.1",
+ "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-6.7.1.tgz",
+ "integrity": "sha512-LDODD4TMYx7XXdpwxAVRAIAuB0bzv0s+ywFonY46k126qzQHT9ygyoa9tncmOiQmmDrik65UYsEkv3lbfqQ3yQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "extend": "^3.0.2",
+ "https-proxy-agent": "^7.0.1",
+ "is-stream": "^2.0.0",
+ "node-fetch": "^2.6.9",
+ "uuid": "^9.0.1"
+ },
+ "engines": {
+ "node": ">=14"
+ }
+ },
+ "node_modules/gcp-metadata": {
+ "version": "6.1.1",
+ "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-6.1.1.tgz",
+ "integrity": "sha512-a4tiq7E0/5fTjxPAaH4jpjkSv/uCaU2p5KC6HVGrvl0cDjA8iBZv4vv1gyzlmK0ZUKqwpOyQMKzZQe3lTit77A==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "gaxios": "^6.1.1",
+ "google-logging-utils": "^0.0.2",
+ "json-bigint": "^1.0.0"
+ },
+ "engines": {
+ "node": ">=14"
+ }
+ },
"node_modules/gensync": {
"version": "1.0.0-beta.2",
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
@@ -3497,6 +3744,34 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/google-auth-library": {
+ "version": "9.15.1",
+ "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-9.15.1.tgz",
+ "integrity": "sha512-Jb6Z0+nvECVz+2lzSMt9u98UsoakXxA2HGHMCxh+so3n90XgYWkq5dur19JAJV7ONiJY22yBTyJB1TSkvPq9Ng==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "base64-js": "^1.3.0",
+ "ecdsa-sig-formatter": "^1.0.11",
+ "gaxios": "^6.1.1",
+ "gcp-metadata": "^6.1.0",
+ "gtoken": "^7.0.0",
+ "jws": "^4.0.0"
+ },
+ "engines": {
+ "node": ">=14"
+ }
+ },
+ "node_modules/google-logging-utils": {
+ "version": "0.0.2",
+ "resolved": "https://registry.npmjs.org/google-logging-utils/-/google-logging-utils-0.0.2.tgz",
+ "integrity": "sha512-NEgUnEcBiP5HrPzufUkBzJOD/Sxsco3rLNo1F1TNf7ieU8ryUzBhqba8r756CjLX7rn3fHl6iLEwPYuqpoKgQQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "engines": {
+ "node": ">=14"
+ }
+ },
"node_modules/graceful-fs": {
"version": "4.2.11",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
@@ -3511,6 +3786,20 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/gtoken": {
+ "version": "7.1.0",
+ "resolved": "https://registry.npmjs.org/gtoken/-/gtoken-7.1.0.tgz",
+ "integrity": "sha512-pCcEwRi+TKpMlxAQObHDQ56KawURgyAf6jtIY046fJ5tIv3zDe/LEIubckAO8fj6JnAxLdmWkUfNyulQ2iKdEw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "gaxios": "^6.0.0",
+ "jws": "^4.0.0"
+ },
+ "engines": {
+ "node": ">=14.0.0"
+ }
+ },
"node_modules/handlebars": {
"version": "4.7.9",
"resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.9.tgz",
@@ -3562,6 +3851,20 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/https-proxy-agent": {
+ "version": "7.0.6",
+ "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz",
+ "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "agent-base": "^7.1.2",
+ "debug": "4"
+ },
+ "engines": {
+ "node": ">= 14"
+ }
+ },
"node_modules/human-signals": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz",
@@ -4487,6 +4790,16 @@
"node": ">=6"
}
},
+ "node_modules/json-bigint": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz",
+ "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "bignumber.js": "^9.0.0"
+ }
+ },
"node_modules/json-buffer": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz",
@@ -4528,6 +4841,29 @@
"node": ">=6"
}
},
+ "node_modules/jwa": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.1.tgz",
+ "integrity": "sha512-hRF04fqJIP8Abbkq5NKGN0Bbr3JxlQ+qhZufXVr0DvujKy93ZCbXZMHDL4EOtodSbCWxOqR8MS1tXA5hwqCXDg==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "buffer-equal-constant-time": "^1.0.1",
+ "ecdsa-sig-formatter": "1.0.11",
+ "safe-buffer": "^5.0.1"
+ }
+ },
+ "node_modules/jws": {
+ "version": "4.0.1",
+ "resolved": "https://registry.npmjs.org/jws/-/jws-4.0.1.tgz",
+ "integrity": "sha512-EKI/M/yqPncGUUh44xz0PxSidXFr/+r0pA70+gIYhjv+et7yxM+s29Y+VGDkovRofQem0fs7Uvf4+YmAdyRduA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "jwa": "^2.0.1",
+ "safe-buffer": "^5.0.1"
+ }
+ },
"node_modules/keyv": {
"version": "4.5.4",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",
@@ -4761,6 +5097,27 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/node-domexception": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
+ "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
+ "deprecated": "Use your platform's native DOMException instead",
+ "dev": true,
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/jimmywarting"
+ },
+ {
+ "type": "github",
+ "url": "https://paypal.me/jimmywarting"
+ }
+ ],
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.5.0"
+ }
+ },
"node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
@@ -4895,6 +5252,20 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/p-retry": {
+ "version": "4.6.2",
+ "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-4.6.2.tgz",
+ "integrity": "sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@types/retry": "0.12.0",
+ "retry": "^0.13.1"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/p-try": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
@@ -5329,6 +5700,16 @@
"node": ">=10"
}
},
+ "node_modules/retry": {
+ "version": "0.13.1",
+ "resolved": "https://registry.npmjs.org/retry/-/retry-0.13.1.tgz",
+ "integrity": "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">= 4"
+ }
+ },
"node_modules/reusify": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/reusify/-/reusify-1.1.0.tgz",
@@ -5381,6 +5762,27 @@
"queue-microtask": "^1.2.2"
}
},
+ "node_modules/safe-buffer": {
+ "version": "5.2.1",
+ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
+ "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
+ "dev": true,
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/feross"
+ },
+ {
+ "type": "patreon",
+ "url": "https://www.patreon.com/feross"
+ },
+ {
+ "type": "consulting",
+ "url": "https://feross.org/support"
+ }
+ ],
+ "license": "MIT"
+ },
"node_modules/semver": {
"version": "6.3.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz",
@@ -5928,6 +6330,21 @@
"punycode": "^2.1.0"
}
},
+ "node_modules/uuid": {
+ "version": "9.0.1",
+ "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz",
+ "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==",
+ "deprecated": "uuid@10 and below is no longer supported. For ESM codebases, update to uuid@latest. For CommonJS codebases, use uuid@11 (but be aware this version will likely be deprecated in 2028).",
+ "dev": true,
+ "funding": [
+ "https://github.com/sponsors/broofa",
+ "https://github.com/sponsors/ctavan"
+ ],
+ "license": "MIT",
+ "bin": {
+ "uuid": "dist/bin/uuid"
+ }
+ },
"node_modules/v8-compile-cache-lib": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
@@ -5960,6 +6377,16 @@
"makeerror": "1.0.12"
}
},
+ "node_modules/web-streams-polyfill": {
+ "version": "3.3.3",
+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
+ "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">= 8"
+ }
+ },
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
@@ -6050,6 +6477,28 @@
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
}
},
+ "node_modules/ws": {
+ "version": "8.21.0",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.21.0.tgz",
+ "integrity": "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.0.0"
+ },
+ "peerDependencies": {
+ "bufferutil": "^4.0.1",
+ "utf-8-validate": ">=5.0.2"
+ },
+ "peerDependenciesMeta": {
+ "bufferutil": {
+ "optional": true
+ },
+ "utf-8-validate": {
+ "optional": true
+ }
+ }
+ },
"node_modules/y18n": {
"version": "5.0.8",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index d595d317..9fc4d802 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -46,6 +46,7 @@
"devDependencies": {
"@digitalocean/gradient": "^0.1.0-alpha.2",
"@eslint/js": "^9.2.0",
+ "@google-cloud/vertexai": "^1.12.0",
"@types/jest": "^29.0.0",
"ai21": "^1.3.0",
"eslint": "^8.57.0",
diff --git a/sdk/typescript/src/instrumentation/__tests__/vertexai-trace-comparison.test.ts b/sdk/typescript/src/instrumentation/__tests__/vertexai-trace-comparison.test.ts
new file mode 100644
index 00000000..02d2f59c
--- /dev/null
+++ b/sdk/typescript/src/instrumentation/__tests__/vertexai-trace-comparison.test.ts
@@ -0,0 +1,486 @@
+/**
+ * Cross-Language Trace Comparison Tests for Vertex AI Integration.
+ * Verifies the TS SDK emits the same telemetry as the Python SDK's
+ * openlit/instrumentation/vertexai/ reference implementation.
+ */
+
+import { trace } from '@opentelemetry/api';
+import VertexAIWrapper from '../vertexai/wrapper';
+import OpenlitConfig from '../../config';
+import OpenLitHelper from '../../helpers';
+import BaseWrapper from '../base-wrapper';
+import SemanticConvention from '../../semantic-convention';
+
+jest.mock('../../config');
+jest.mock('../../helpers', () => {
+ const actual = jest.requireActual('../../helpers');
+ return {
+ ...actual,
+ getChatModelCost: jest.fn(),
+ handleException: jest.fn(),
+ buildInputMessages: jest.fn(),
+ buildOutputMessages: jest.fn(),
+ emitInferenceEvent: jest.fn(),
+ buildToolDefinitions: jest.fn(),
+ computeAgentVersionHash: jest.fn(),
+ isFrameworkLlmActive: jest.fn().mockReturnValue(false),
+ getFrameworkParentContext: jest.fn().mockReturnValue(undefined),
+ getCurrentAgentVersion: jest.fn().mockReturnValue(null),
+ };
+});
+jest.mock('../base-wrapper');
+
+const mockTracer = trace.getTracer('test-tracer');
+
+describe('Vertex AI Cross-Language Trace Comparison', () => {
+ let mockSpan: any;
+
+ beforeEach(() => {
+ mockSpan = mockTracer.startSpan('test-span');
+ mockSpan.setAttribute = jest.fn();
+ mockSpan.addEvent = jest.fn();
+ mockSpan.end = jest.fn();
+ mockSpan.setStatus = jest.fn();
+
+ (OpenlitConfig as any).environment = 'openlit-testing';
+ (OpenlitConfig as any).applicationName = 'openlit-test';
+ (OpenlitConfig as any).captureMessageContent = true;
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = false;
+
+ (OpenLitHelper as any).getChatModelCost = jest.fn().mockReturnValue(0.001);
+ (OpenLitHelper as any).handleException = jest.fn();
+ (OpenLitHelper as any).buildInputMessages = jest.fn().mockReturnValue('[{"role":"user","parts":[{"type":"text","content":"Hello"}]}]');
+ (OpenLitHelper as any).buildOutputMessages = jest.fn().mockReturnValue('[{"role":"assistant","parts":[{"type":"text","content":"Hi"}],"finish_reason":"STOP"}]');
+ (OpenLitHelper as any).emitInferenceEvent = jest.fn();
+ (OpenLitHelper as any).buildToolDefinitions = jest.fn().mockReturnValue(undefined);
+ (OpenLitHelper as any).computeAgentVersionHash = jest.fn().mockReturnValue(null);
+
+ (BaseWrapper as any).recordMetrics = jest.fn();
+ (BaseWrapper as any).setBaseSpanAttributes = jest.fn().mockImplementation((span: any, attrs: any) => {
+ span.setAttribute(SemanticConvention.GEN_AI_PROVIDER_NAME_OTEL, attrs.aiSystem);
+ span.setAttribute(SemanticConvention.GEN_AI_ENDPOINT, attrs.genAIEndpoint);
+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MODEL, attrs.model);
+ if (attrs.cost !== undefined) {
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_COST, attrs.cost);
+ }
+ if (attrs.serverAddress) {
+ span.setAttribute(SemanticConvention.SERVER_ADDRESS, attrs.serverAddress);
+ }
+ if (attrs.serverPort !== undefined) {
+ span.setAttribute(SemanticConvention.SERVER_PORT, attrs.serverPort);
+ }
+ });
+ });
+
+ afterEach(() => {
+ jest.clearAllMocks();
+ });
+
+ describe('generateContent — non-streaming', () => {
+ it('sets provider name to vertex_ai (matches Python GEN_AI_SYSTEM_VERTEXAI)', async () => {
+ const mockArgs = [{
+ contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
+ generationConfig: { temperature: 0.7, maxOutputTokens: 256, topP: 0.9 },
+ }];
+ const mockResponse = {
+ response: {
+ candidates: [{
+ content: { parts: [{ text: 'Hi there!' }], role: 'model' },
+ finishReason: 'STOP',
+ }],
+ usageMetadata: { promptTokenCount: 5, candidatesTokenCount: 3, totalTokenCount: 8 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_PROVIDER_NAME_OTEL,
+ 'vertex_ai'
+ );
+ });
+
+ it('sets request params only when explicitly provided', async () => {
+ const mockArgs = [{
+ contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
+ generationConfig: { temperature: 0.5, maxOutputTokens: 100, topP: 0.8, topK: 40 },
+ }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 2, candidatesTokenCount: 1, totalTokenCount: 3 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, 0.5);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, 100);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_TOP_P, 0.8);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_TOP_K, 40);
+ });
+
+ it('omits request params when not set (no sentinel values)', async () => {
+ const mockArgs = [{ contents: 'Hello', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1, totalTokenCount: 2 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ const attrKeys = mockSpan.setAttribute.mock.calls.map((c: any[]) => c[0]);
+ expect(attrKeys).not.toContain(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE);
+ expect(attrKeys).not.toContain(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS);
+ expect(attrKeys).not.toContain(SemanticConvention.GEN_AI_REQUEST_TOP_P);
+ expect(attrKeys).not.toContain(SemanticConvention.GEN_AI_REQUEST_TOP_K);
+ expect(attrKeys).not.toContain(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES);
+ });
+
+ it('sets token usage and server address from location', async () => {
+ const mockArgs = [{ contents: 'Ping', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Pong' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'europe-west1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, 10);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, 5);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.SERVER_ADDRESS,
+ 'europe-west1-aiplatform.googleapis.com'
+ );
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_PORT, 443);
+ });
+
+ it('sets finish reason as array (matches Python list)', async () => {
+ const mockArgs = [{ contents: 'Hello', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1, totalTokenCount: 2 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
+ ['STOP']
+ );
+ });
+
+ it('sets is_stream=false for non-streaming', async () => {
+ const mockArgs = [{ contents: 'Hello', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1, totalTokenCount: 2 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);
+ });
+
+ it('captures function call attributes', async () => {
+ const mockArgs = [{ contents: 'What is the weather?', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{
+ content: {
+ parts: [{ functionCall: { name: 'get_weather', args: { city: 'Berlin' } } }],
+ role: 'model',
+ },
+ finishReason: 'STOP',
+ }],
+ usageMetadata: { promptTokenCount: 6, candidatesTokenCount: 4, totalTokenCount: 10 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_TOOL_NAME, 'get_weather');
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_TOOL_ARGS,
+ '{"city":"Berlin"}'
+ );
+ });
+
+ it('emits inference event independently of captureMessageContent', async () => {
+ (OpenlitConfig as any).captureMessageContent = false;
+
+ const mockArgs = [{ contents: 'Hello', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1, totalTokenCount: 2 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(OpenLitHelper.emitInferenceEvent).toHaveBeenCalledTimes(1);
+ const eventAttrs = (OpenLitHelper.emitInferenceEvent as jest.Mock).mock.calls[0][1];
+ expect(eventAttrs[SemanticConvention.GEN_AI_INPUT_MESSAGES]).toBeUndefined();
+ expect(eventAttrs[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]).toBeUndefined();
+ });
+
+ it('records metrics with cost from pricingInfo', async () => {
+ (OpenlitConfig as any).pricingInfo = { chat: { 'gemini-2.0-flash': { promptPrice: 0.1, completionPrice: 0.2 } } };
+ (OpenLitHelper as any).getChatModelCost = jest.fn().mockReturnValue(0.003);
+
+ const mockArgs = [{ contents: 'Hello', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(BaseWrapper.recordMetrics).toHaveBeenCalledWith(
+ mockSpan,
+ expect.objectContaining({
+ model: 'gemini-2.0-flash',
+ cost: 0.003,
+ aiSystem: 'vertex_ai',
+ })
+ );
+ });
+ });
+
+ describe('model name extraction', () => {
+ it('strips full projects/locations/publishers/models/ resource path', () => {
+ const instance = {
+ model: 'projects/my-project/locations/us-central1/publishers/google/models/gemini-2.0-flash',
+ };
+ expect(VertexAIWrapper._extractModelName(instance)).toBe('gemini-2.0-flash');
+ });
+
+ it('strips publishers/google/models/ prefix', () => {
+ const instance = { model: 'publishers/google/models/gemini-pro' };
+ expect(VertexAIWrapper._extractModelName(instance)).toBe('gemini-pro');
+ });
+
+ it('leaves a plain model name unchanged', () => {
+ const instance = { model: 'gemini-2.0-flash' };
+ expect(VertexAIWrapper._extractModelName(instance)).toBe('gemini-2.0-flash');
+ });
+
+ it('falls back to gemini-2.0-flash when instance has no model field', () => {
+ expect(VertexAIWrapper._extractModelName({})).toBe('gemini-2.0-flash');
+ });
+
+ it('reads model from resourcePath (ChatSession shape)', () => {
+ const instance = {
+ resourcePath: 'projects/my-project/locations/us-central1/publishers/google/models/gemini-1.5-pro',
+ };
+ expect(VertexAIWrapper._extractModelName(instance)).toBe('gemini-1.5-pro');
+ });
+
+ it('reads model from generativeModel property', () => {
+ const instance = { generativeModel: { model: 'gemini-1.5-pro' } };
+ expect(VertexAIWrapper._extractModelName(instance)).toBe('gemini-1.5-pro');
+ });
+ });
+
+ describe('ChatSession.sendMessage', () => {
+ it('captures the turn message as input and reads session config from instance', async () => {
+ const mockArgs = ['What is the capital of France?'];
+ const mockInstance = {
+ generationConfig: { temperature: 0.4, maxOutputTokens: 128 },
+ systemInstruction: { parts: [{ text: 'You are a geography tutor.' }] },
+ };
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Paris' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 8, candidatesTokenCount: 3, totalTokenCount: 11 },
+ },
+ };
+
+ (OpenLitHelper as any).buildInputMessages = jest
+ .fn()
+ .mockReturnValue('[{"role":"user","parts":[{"type":"text","content":"What is the capital of France?"}]}]');
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ instance: mockInstance,
+ genAIEndpoint: 'vertexai.generative_models.chat_session.send_message',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ isChatSession: true,
+ });
+
+ expect(OpenLitHelper.buildInputMessages).toHaveBeenCalledWith([
+ { role: 'user', content: 'What is the capital of France?' },
+ ]);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, 0.4);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, 128);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
+ 8
+ );
+ });
+ });
+
+ describe('custom span attributes', () => {
+ it('applies global custom attributes via setBaseSpanAttributes', async () => {
+ const actualBaseWrapper = jest.requireActual('../base-wrapper').default;
+ (BaseWrapper as any).setBaseSpanAttributes = actualBaseWrapper.setBaseSpanAttributes;
+ (OpenlitConfig as any).customSpanAttributes = { 'team.id': 'eng-ml' };
+
+ const mockArgs = [{ contents: 'Hello', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1, totalTokenCount: 2 },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith('team.id', 'eng-ml');
+ });
+
+ it('applies custom attributes on failed requests before span end', async () => {
+ (OpenlitConfig as any).customSpanAttributes = { 'request.source': 'batch' };
+
+ const errorSpan = {
+ setAttribute: jest.fn(),
+ end: jest.fn(),
+ setStatus: jest.fn(),
+ recordException: jest.fn(),
+ };
+ const tracer = {
+ startSpan: jest.fn().mockReturnValue(errorSpan),
+ };
+
+ const patcher = VertexAIWrapper._patchGenerateContent(tracer as any);
+ const failingMethod = patcher(async () => {
+ throw new Error('vertex unavailable');
+ });
+
+ await expect(
+ failingMethod.call({ model: 'gemini-2.0-flash', location: 'us-central1' }, {
+ contents: 'Hello',
+ generationConfig: {},
+ })
+ ).rejects.toThrow('vertex unavailable');
+
+ expect(errorSpan.setAttribute).toHaveBeenCalledWith('request.source', 'batch');
+ });
+ });
+
+ describe('cache token attributes', () => {
+ it('sets cache_read_input_tokens when non-zero', async () => {
+ const mockArgs = [{ contents: 'Hello', generationConfig: {} }];
+ const mockResponse = {
+ response: {
+ candidates: [{ content: { parts: [{ text: 'Hi' }] }, finishReason: 'STOP' }],
+ usageMetadata: {
+ promptTokenCount: 10,
+ candidatesTokenCount: 5,
+ totalTokenCount: 15,
+ cachedContentTokenCount: 3,
+ },
+ },
+ };
+
+ await VertexAIWrapper._processResponse({
+ args: mockArgs,
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ response: mockResponse,
+ span: mockSpan,
+ requestModel: 'gemini-2.0-flash',
+ serverAddress: 'us-central1-aiplatform.googleapis.com',
+ });
+
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
+ 3
+ );
+ });
+ });
+});
diff --git a/sdk/typescript/src/instrumentation/index.ts b/sdk/typescript/src/instrumentation/index.ts
index 45d500fd..0cc91d28 100644
--- a/sdk/typescript/src/instrumentation/index.ts
+++ b/sdk/typescript/src/instrumentation/index.ts
@@ -10,6 +10,7 @@ import AI21Instrumentation from './ai21';
import GradientInstrumentation from './gradient';
import MistralInstrumentation from './mistral';
import GoogleAIInstrumentation from './google-ai';
+import VertexAIInstrumentation from './vertexai';
import TogetherInstrumentation from './together';
import OllamaInstrumentation from './ollama';
import VercelAIInstrumentation from './vercel-ai';
@@ -83,6 +84,7 @@ export default class Instrumentations {
gradient: new GradientInstrumentation(),
mistral: new MistralInstrumentation(),
'google-ai': new GoogleAIInstrumentation(),
+ vertexai: new VertexAIInstrumentation(),
together: new TogetherInstrumentation(),
ollama: new OllamaInstrumentation(),
'vercel-ai': new VercelAIInstrumentation(),
diff --git a/sdk/typescript/src/instrumentation/vertexai/index.ts b/sdk/typescript/src/instrumentation/vertexai/index.ts
new file mode 100644
index 00000000..25330fdb
--- /dev/null
+++ b/sdk/typescript/src/instrumentation/vertexai/index.ts
@@ -0,0 +1,102 @@
+import {
+ InstrumentationBase,
+ InstrumentationModuleDefinition,
+ InstrumentationNodeModuleDefinition,
+ isWrapped,
+} from '@opentelemetry/instrumentation';
+import { InstrumentationConfig } from '@opentelemetry/instrumentation';
+import { INSTRUMENTATION_PREFIX } from '../../constant';
+import VertexAIWrapper from './wrapper';
+
+export interface VertexAIInstrumentationConfig extends InstrumentationConfig {}
+
+export default class OpenlitVertexAIInstrumentation extends InstrumentationBase {
+ constructor(config: VertexAIInstrumentationConfig = {}) {
+ super(`${INSTRUMENTATION_PREFIX}/instrumentation-vertexai`, '1.0.0', config);
+ }
+
+ protected init(): void | InstrumentationModuleDefinition | InstrumentationModuleDefinition[] {
+ const module = new InstrumentationNodeModuleDefinition(
+ '@google-cloud/vertexai',
+ ['>=0.1.0'],
+ (moduleExports) => {
+ this._patch(moduleExports);
+ return moduleExports;
+ },
+ (moduleExports) => {
+ if (moduleExports !== undefined) {
+ this._unpatch(moduleExports);
+ }
+ }
+ );
+ return [module];
+ }
+
+ public manualPatch(vertexAI: any): void {
+ this._patch(vertexAI);
+ }
+
+ protected _patch(moduleExports: any) {
+ try {
+ const GenerativeModel =
+ moduleExports.GenerativeModel || moduleExports.GenerativeModelPreview;
+ const ChatSession =
+ moduleExports.ChatSession || moduleExports.ChatSessionPreview;
+
+ if (GenerativeModel) {
+ if (isWrapped(GenerativeModel.prototype.generateContent)) {
+ this._unwrap(GenerativeModel.prototype, 'generateContent');
+ }
+ if (isWrapped(GenerativeModel.prototype.generateContentStream)) {
+ this._unwrap(GenerativeModel.prototype, 'generateContentStream');
+ }
+ this._wrap(
+ GenerativeModel.prototype,
+ 'generateContent',
+ VertexAIWrapper._patchGenerateContent(this.tracer)
+ );
+ this._wrap(
+ GenerativeModel.prototype,
+ 'generateContentStream',
+ VertexAIWrapper._patchGenerateContentStream(this.tracer)
+ );
+ }
+
+ if (ChatSession) {
+ if (isWrapped(ChatSession.prototype.sendMessage)) {
+ this._unwrap(ChatSession.prototype, 'sendMessage');
+ }
+ if (isWrapped(ChatSession.prototype.sendMessageStream)) {
+ this._unwrap(ChatSession.prototype, 'sendMessageStream');
+ }
+ this._wrap(
+ ChatSession.prototype,
+ 'sendMessage',
+ VertexAIWrapper._patchSendMessage(this.tracer)
+ );
+ this._wrap(
+ ChatSession.prototype,
+ 'sendMessageStream',
+ VertexAIWrapper._patchSendMessageStream(this.tracer)
+ );
+ }
+ } catch (e) {
+ console.error('Error in _patch method:', e);
+ }
+ }
+
+ protected _unpatch(moduleExports: any) {
+ const GenerativeModel =
+ moduleExports.GenerativeModel || moduleExports.GenerativeModelPreview;
+ const ChatSession =
+ moduleExports.ChatSession || moduleExports.ChatSessionPreview;
+ if (GenerativeModel) {
+ this._unwrap(GenerativeModel.prototype, 'generateContent');
+ this._unwrap(GenerativeModel.prototype, 'generateContentStream');
+ }
+ if (ChatSession) {
+ this._unwrap(ChatSession.prototype, 'sendMessage');
+ this._unwrap(ChatSession.prototype, 'sendMessageStream');
+ }
+ }
+}
diff --git a/sdk/typescript/src/instrumentation/vertexai/wrapper.ts b/sdk/typescript/src/instrumentation/vertexai/wrapper.ts
new file mode 100644
index 00000000..97ef51a6
--- /dev/null
+++ b/sdk/typescript/src/instrumentation/vertexai/wrapper.ts
@@ -0,0 +1,631 @@
+import { Span, SpanKind, Tracer, context, trace, Attributes } from '@opentelemetry/api';
+import OpenlitConfig from '../../config';
+import OpenLitHelper, {
+ applyCustomSpanAttributes,
+ isFrameworkLlmActive,
+ getFrameworkParentContext,
+ getCurrentAgentVersion,
+} from '../../helpers';
+import SemanticConvention from '../../semantic-convention';
+import BaseWrapper from '../base-wrapper';
+
+// Derive the regional API endpoint from the model/session instance.
+// @google-cloud/vertexai exposes `location` on GenerativeModel and ChatSession.
+
+function extractServerAddress(instance: any): string {
+ const location =
+ instance?.location ||
+ instance?._location ||
+ instance?.generativeModel?.location ||
+ instance?.generativeModel?._location ||
+ 'us-central1';
+ return `${location}-aiplatform.googleapis.com`;
+}
+
+// Non-chat: args[0] is the GenerateContentRequest.
+// ChatSession: args[0] is the turn message; generationConfig/systemInstruction/tools
+// are stored on the session instance from startChat().
+function resolveRequestContext(
+ instance: any,
+ args: any[],
+ isChatSession: boolean
+): Record<string, any> {
+ if (!isChatSession) {
+ return args[0] || {};
+ }
+ return {
+ generationConfig: instance?.generationConfig,
+ systemInstruction: instance?.systemInstruction,
+ tools: instance?.tools,
+ };
+}
+
+function spanCreationAttrs(
+ operationName: string,
+ requestModel: string,
+ serverAddress: string
+): Attributes {
+ return {
+ [SemanticConvention.GEN_AI_OPERATION]: operationName,
+ [SemanticConvention.GEN_AI_PROVIDER_NAME_OTEL]: VertexAIWrapper.aiSystem,
+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: requestModel,
+ [SemanticConvention.SERVER_ADDRESS]: serverAddress,
+ [SemanticConvention.SERVER_PORT]: VertexAIWrapper.serverPort,
+ };
+}
+
+class VertexAIWrapper extends BaseWrapper {
+ static aiSystem = SemanticConvention.GEN_AI_SYSTEM_VERTEXAI;
+ static serverPort = 443;
+
+ // Exposed as a static method so it can be unit-tested directly.
+ // Strips full Vertex AI resource paths to the short model name.
+ // e.g. projects/p/locations/l/publishers/google/models/gemini-2.0-flash → gemini-2.0-flash
+ static _extractModelName(instance: any): string {
+ const raw =
+ instance?.model ||
+ instance?._modelId ||
+ instance?.resourcePath ||
+ instance?.generativeModel?.model ||
+ instance?.generativeModel?._modelId ||
+ instance?.generativeModel?.resourcePath ||
+ 'gemini-2.0-flash';
+ return String(raw)
+ .replace(/^projects\/[^/]+\/locations\/[^/]+\/publishers\/[^/]+\/models\//, '')
+ .replace(/^publishers\/[^/]+\/models\//, '');
+ }
+
+ static _stampAgentVersion(
+ span: Span,
+ args: {
+ systemInstructionsJson?: string;
+ toolDefinitionsJson?: string;
+ primaryModel?: string;
+ temperature?: number | null;
+ top_p?: number | null;
+ max_tokens?: number | null;
+ }
+ ): Record<string, string> {
+ const out: Record<string, string> = {};
+ try {
+ const versionHash = OpenLitHelper.computeAgentVersionHash({
+ systemInstructions: args.systemInstructionsJson ?? null,
+ toolDefinitions: args.toolDefinitionsJson ?? null,
+ primaryModel: args.primaryModel ?? null,
+ runtimeConfig: {
+ temperature: args.temperature ?? null,
+ top_p: args.top_p ?? null,
+ max_tokens: args.max_tokens ?? null,
+ provider: SemanticConvention.GEN_AI_SYSTEM_VERTEXAI,
+ },
+ providers: [SemanticConvention.GEN_AI_SYSTEM_VERTEXAI],
+ });
+ if (versionHash) {
+ out[SemanticConvention.OPENLIT_AGENT_VERSION_HASH] = versionHash;
+ span.setAttribute(SemanticConvention.OPENLIT_AGENT_VERSION_HASH, versionHash);
+ }
+ } catch {
+ // Hash computation must never fail the wrapped call.
+ }
+ const versionLabel = getCurrentAgentVersion();
+ if (versionLabel) {
+ out[SemanticConvention.GEN_AI_AGENT_VERSION] = versionLabel;
+ span.setAttribute(SemanticConvention.GEN_AI_AGENT_VERSION, versionLabel);
+ }
+ return out;
+ }
+
+ // Shared span/context/error boilerplate for all four patch methods.
+ // Only genAIEndpoint, isStream, and isChatSession vary between them.
+ static _buildPatcher({
+ genAIEndpoint,
+ isStream,
+ isChatSession,
+ tracer,
+ }: {
+ genAIEndpoint: string;
+ isStream: boolean;
+ isChatSession: boolean;
+ tracer: Tracer;
+ }): (originalMethod: (...args: any[]) => any) => (...args: any[]) => Promise<any> {
+ return (originalMethod: (...args: any[]) => any) => {
+ return async function (this: any, ...args: any[]) {
+ if (isFrameworkLlmActive()) return originalMethod.apply(this, args);
+ const requestModel = VertexAIWrapper._extractModelName(this);
+ const serverAddress = extractServerAddress(this);
+ const spanName = `${SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} ${requestModel}`;
+ const effectiveCtx = getFrameworkParentContext() ?? context.active();
+ const span = tracer.startSpan(
+ spanName,
+ {
+ kind: SpanKind.CLIENT,
+ attributes: spanCreationAttrs(
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ requestModel,
+ serverAddress
+ ),
+ },
+ effectiveCtx
+ );
+ return context
+ .with(trace.setSpan(effectiveCtx, span), async () => {
+ return originalMethod.apply(this, args);
+ })
+ .then((result: any) => {
+ if (isStream) {
+ const wrappedStream = VertexAIWrapper._streamGenerator({
+ args,
+ genAIEndpoint,
+ instance: this,
+ stream: result.stream,
+ span,
+ requestModel,
+ serverAddress,
+ isChatSession,
+ });
+ return { ...result, stream: wrappedStream };
+ }
+ return VertexAIWrapper._processResponse({
+ args,
+ genAIEndpoint,
+ instance: this,
+ response: result,
+ span,
+ requestModel,
+ serverAddress,
+ isChatSession,
+ });
+ })
+ .catch((e: any) => {
+ OpenLitHelper.handleException(span, e);
+ applyCustomSpanAttributes(span);
+ BaseWrapper.recordMetrics(span, {
+ genAIEndpoint,
+ model: requestModel,
+ aiSystem: VertexAIWrapper.aiSystem,
+ serverAddress,
+ serverPort: VertexAIWrapper.serverPort,
+ errorType: e?.constructor?.name || '_OTHER',
+ });
+ span.end();
+ throw e;
+ });
+ };
+ };
+ }
+
+ static _patchGenerateContent(tracer: Tracer): any {
+ return VertexAIWrapper._buildPatcher({
+ genAIEndpoint: 'vertexai.generative_models.generate_content',
+ isStream: false,
+ isChatSession: false,
+ tracer,
+ });
+ }
+
+ static _patchGenerateContentStream(tracer: Tracer): any {
+ return VertexAIWrapper._buildPatcher({
+ genAIEndpoint: 'vertexai.generative_models.generate_content_stream',
+ isStream: true,
+ isChatSession: false,
+ tracer,
+ });
+ }
+
+ static _patchSendMessage(tracer: Tracer): any {
+ return VertexAIWrapper._buildPatcher({
+ genAIEndpoint: 'vertexai.generative_models.chat_session.send_message',
+ isStream: false,
+ isChatSession: true,
+ tracer,
+ });
+ }
+
+ static _patchSendMessageStream(tracer: Tracer): any {
+ return VertexAIWrapper._buildPatcher({
+ genAIEndpoint: 'vertexai.generative_models.chat_session.send_message_stream',
+ isStream: true,
+ isChatSession: true,
+ tracer,
+ });
+ }
+
+ static async _processResponse({
+ args,
+ genAIEndpoint,
+ instance,
+ response,
+ span,
+ requestModel,
+ serverAddress,
+ isChatSession = false,
+ }: {
+ args: any[];
+ genAIEndpoint: string;
+ instance?: any;
+ response: any;
+ span: Span;
+ requestModel: string;
+ serverAddress: string;
+ isChatSession?: boolean;
+ }): Promise<any> {
+ let metricParams;
+ try {
+ metricParams = await VertexAIWrapper._commonSetter({
+ args,
+ genAIEndpoint,
+ instance,
+ result: response,
+ span,
+ requestModel,
+ serverAddress,
+ isChatSession,
+ });
+ return response;
+ } catch (e: any) {
+ OpenLitHelper.handleException(span, e);
+ throw e;
+ } finally {
+ span.end();
+ if (metricParams) {
+ BaseWrapper.recordMetrics(span, metricParams);
+ }
+ }
+ }
+
+ static async *_streamGenerator({
+ args,
+ genAIEndpoint,
+ instance,
+ stream,
+ span,
+ requestModel,
+ serverAddress,
+ isChatSession = false,
+ }: {
+ args: any[];
+ genAIEndpoint: string;
+ instance?: any;
+ stream: any;
+ span: Span;
+ requestModel: string;
+ serverAddress: string;
+ isChatSession?: boolean;
+ }): AsyncGenerator<unknown, any, unknown> {
+ let metricParams;
+ const timestamps: number[] = [];
+ const startTime = Date.now();
+
+ try {
+ const accumulated = {
+ text: '',
+ candidates: [] as any[],
+ usageMetadata: {
+ promptTokenCount: 0,
+ candidatesTokenCount: 0,
+ totalTokenCount: 0,
+ cachedContentTokenCount: 0,
+ cacheCreationInputTokens: 0,
+ },
+ functionCall: null as any,
+ };
+
+ for await (const chunk of stream) {
+ timestamps.push(Date.now());
+
+ if (chunk.candidates && chunk.candidates.length > 0) {
+ if (accumulated.candidates.length === 0) {
+ accumulated.candidates = chunk.candidates.map((c: any) => ({
+ content: { parts: [{ text: '' }], role: 'model' },
+ finishReason: c.finishReason || '',
+ }));
+ }
+ chunk.candidates.forEach((c: any, idx: number) => {
+ if (c.content?.parts) {
+ c.content.parts.forEach((part: any) => {
+ if (part.text && accumulated.candidates[idx]) {
+ accumulated.candidates[idx].content.parts[0].text += part.text;
+ accumulated.text += part.text;
+ }
+ if (part.functionCall) {
+ accumulated.functionCall = part.functionCall;
+ }
+ });
+ }
+ if (c.finishReason && accumulated.candidates[idx]) {
+ accumulated.candidates[idx].finishReason = c.finishReason;
+ }
+ });
+ }
+
+ if (chunk.usageMetadata) {
+ const u = chunk.usageMetadata;
+ accumulated.usageMetadata = {
+ promptTokenCount: u.promptTokenCount || accumulated.usageMetadata.promptTokenCount,
+ candidatesTokenCount: u.candidatesTokenCount || accumulated.usageMetadata.candidatesTokenCount,
+ totalTokenCount: u.totalTokenCount || accumulated.usageMetadata.totalTokenCount,
+ cachedContentTokenCount: u.cachedContentTokenCount || accumulated.usageMetadata.cachedContentTokenCount,
+ cacheCreationInputTokens: u.cacheCreationInputTokens || accumulated.usageMetadata.cacheCreationInputTokens,
+ };
+ }
+
+ yield chunk;
+ }
+
+ const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;
+ let tbt = 0;
+ if (timestamps.length > 1) {
+ const diffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);
+ tbt = diffs.reduce((a, b) => a + b, 0) / diffs.length / 1000;
+ }
+
+ metricParams = await VertexAIWrapper._commonSetter({
+ args,
+ genAIEndpoint,
+ instance,
+ result: accumulated,
+ span,
+ requestModel,
+ serverAddress,
+ ttft,
+ tbt,
+ isStream: true,
+ isChatSession,
+ });
+
+ return accumulated;
+ } catch (e: any) {
+ OpenLitHelper.handleException(span, e);
+ throw e;
+ } finally {
+ span.end();
+ if (metricParams) {
+ BaseWrapper.recordMetrics(span, metricParams);
+ }
+ }
+ }
+
+ static async _commonSetter({
+ args,
+ genAIEndpoint,
+ instance,
+ result,
+ span,
+ requestModel,
+ serverAddress,
+ ttft = 0,
+ tbt = 0,
+ isStream = false,
+ isChatSession = false,
+ }: {
+ args: any[];
+ genAIEndpoint: string;
+ instance?: any;
+ result: any;
+ span: Span;
+ requestModel: string;
+ serverAddress: string;
+ ttft?: number;
+ tbt?: number;
+ isStream?: boolean;
+ isChatSession?: boolean;
+ }) {
+ const captureContent = OpenlitConfig.captureMessageContent;
+ // Non-streaming: result = {response: GenerateContentResponse}
+ // Streaming: result = accumulated plain object
+ const responseData = result.response || result;
+
+ // @google-cloud/vertexai uses `generationConfig` (camelCase).
+ const requestArg = resolveRequestContext(instance, args, isChatSession);
+ const generationConfig = requestArg.generationConfig || {};
+ const {
+ temperature,
+ maxOutputTokens,
+ topP,
+ topK,
+ stopSequences,
+ frequencyPenalty,
+ presencePenalty,
+ candidateCount,
+ } = generationConfig;
+ const systemInstruction = requestArg.systemInstruction;
+ const _tools = requestArg.tools;
+
+ // Request param attributes — only set when explicitly provided
+ if (temperature != null) span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, temperature);
+ if (maxOutputTokens != null) span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, maxOutputTokens);
+ if (topP != null) span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, topP);
+ if (topK != null) span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, topK);
+ if (stopSequences) span.setAttribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, stopSequences);
+ if (frequencyPenalty) span.setAttribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequencyPenalty);
+ if (presencePenalty) span.setAttribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, presencePenalty);
+ if (candidateCount != null && candidateCount !== 1) {
+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_CHOICE_COUNT, candidateCount);
+ }
+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, isStream);
+
+ const usageMetadata = responseData.usageMetadata;
+ const inputTokens = usageMetadata?.promptTokenCount || 0;
+ const outputTokens = usageMetadata?.candidatesTokenCount || 0;
+ const cacheReadTokens = usageMetadata?.cachedContentTokenCount || 0;
+ const cacheCreationTokens = usageMetadata?.cacheCreationInputTokens || 0;
+
+ const pricingInfo = OpenlitConfig.pricingInfo || {};
+ const cost = OpenLitHelper.getChatModelCost(requestModel, pricingInfo, inputTokens, outputTokens);
+
+ VertexAIWrapper.setBaseSpanAttributes(span, {
+ genAIEndpoint,
+ model: requestModel,
+ user: undefined,
+ cost,
+ aiSystem: VertexAIWrapper.aiSystem,
+ serverAddress,
+ serverPort: VertexAIWrapper.serverPort,
+ });
+
+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, requestModel);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);
+
+ if (cacheReadTokens) span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cacheReadTokens);
+ if (cacheCreationTokens) span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cacheCreationTokens);
+
+ if (ttft > 0) span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ if (tbt > 0) span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);
+
+ const responseId = responseData.id || responseData.name || '';
+ if (responseId) {
+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_ID, responseId);
+ }
+
+ const finishReason = responseData.candidates?.[0]?.finishReason || '';
+ if (finishReason) {
+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [finishReason]);
+ }
+
+ // Resolve completion text
+ const completionText = isStream
+ ? (responseData.text || '')
+ : (responseData.candidates?.[0]?.content?.parts?.find((p: any) => p.text)?.text || '');
+ span.setAttribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ typeof completionText === 'string'
+ ? SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT
+ : SemanticConvention.GEN_AI_OUTPUT_TYPE_JSON
+ );
+ const outputType = typeof completionText === 'string'
+ ? SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT
+ : SemanticConvention.GEN_AI_OUTPUT_TYPE_JSON;
+
+ const functionCall = isStream
+ ? responseData.functionCall
+ : responseData.candidates?.[0]?.content?.parts?.find((p: any) => p.functionCall)?.functionCall;
+ if (functionCall) {
+ if (functionCall.name) span.setAttribute(SemanticConvention.GEN_AI_TOOL_NAME, functionCall.name);
+ if (functionCall.args) span.setAttribute(SemanticConvention.GEN_AI_TOOL_ARGS, JSON.stringify(functionCall.args));
+ }
+
+ const toolDefinitionsJson = OpenLitHelper.buildToolDefinitions(
+ Array.isArray(_tools)
+ ? _tools.flatMap((tool: any) =>
+ Array.isArray(tool?.functionDeclarations) ? tool.functionDeclarations : [tool]
+ )
+ : _tools
+ );
+
+ // System instructions — computed regardless of captureContent for agent version hash
+ let systemInstructionsJson: string | undefined;
+ if (systemInstruction) {
+ let systemText = '';
+ if (typeof systemInstruction === 'string') {
+ systemText = systemInstruction;
+ } else if (Array.isArray(systemInstruction.parts)) {
+ systemText = systemInstruction.parts
+ .map((p: any) => p?.text || '')
+ .filter(Boolean)
+ .join('\n');
+ } else if (typeof systemInstruction.text === 'string') {
+ systemText = systemInstruction.text;
+ }
+ if (systemText) {
+ systemInstructionsJson = JSON.stringify([{ type: 'text', content: systemText }]);
+ }
+ }
+
+ const versionExtras = VertexAIWrapper._stampAgentVersion(span, {
+ systemInstructionsJson,
+ toolDefinitionsJson,
+ primaryModel: requestModel,
+ temperature: temperature ?? null,
+ top_p: topP ?? null,
+ max_tokens: maxOutputTokens ?? null,
+ });
+
+ let inputMessagesJson: string | undefined;
+ let outputMessagesJson: string | undefined;
+
+ if (captureContent) {
+ if (!isChatSession) {
+ const contents = requestArg.contents;
+ let messages: any[] = [];
+ if (typeof contents === 'string') {
+ messages = [{ role: 'user', content: contents }];
+ } else if (Array.isArray(contents)) {
+ messages = contents.map((item: any) => ({
+ role: item.role === 'model' ? 'assistant' : (item.role || 'user'),
+ content: Array.isArray(item.parts)
+ ? item.parts.map((p: any) => p.text || '').join(' ')
+ : String(item.parts || ''),
+ }));
+ }
+ inputMessagesJson = OpenLitHelper.buildInputMessages(messages);
+ span.setAttribute(SemanticConvention.GEN_AI_INPUT_MESSAGES, inputMessagesJson);
+ } else {
+ // ChatSession: capture the current turn message (args[0] can be string or Part[])
+ const turnMessage = args[0];
+ const turnText =
+ typeof turnMessage === 'string'
+ ? turnMessage
+ : Array.isArray(turnMessage)
+ ? turnMessage.map((p: any) => (typeof p === 'string' ? p : p?.text || '')).join(' ')
+ : '';
+ inputMessagesJson = OpenLitHelper.buildInputMessages([{ role: 'user', content: turnText }]);
+ span.setAttribute(SemanticConvention.GEN_AI_INPUT_MESSAGES, inputMessagesJson);
+ }
+
+ const outputContent = completionText || responseData.candidates?.[0]?.content?.parts?.[0]?.text || '';
+ const toolCallsForOutput = functionCall
+ ? [{ name: functionCall.name || '', arguments: functionCall.args || {} }]
+ : undefined;
+ outputMessagesJson = OpenLitHelper.buildOutputMessages(
+ outputContent,
+ finishReason || 'stop',
+ toolCallsForOutput
+ );
+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_MESSAGES, outputMessagesJson);
+
+ if (systemInstructionsJson) {
+ span.setAttribute(SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS, systemInstructionsJson);
+ }
+ }
+
+ if (toolDefinitionsJson) {
+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_DEFINITIONS, toolDefinitionsJson);
+ }
+
+ if (!OpenlitConfig.disableEvents) {
+ const eventAttrs: Attributes = {
+ [SemanticConvention.GEN_AI_OPERATION]: SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: requestModel,
+ [SemanticConvention.GEN_AI_RESPONSE_MODEL]: requestModel,
+ [SemanticConvention.SERVER_ADDRESS]: serverAddress,
+ [SemanticConvention.SERVER_PORT]: VertexAIWrapper.serverPort,
+ [SemanticConvention.GEN_AI_OUTPUT_TYPE]: outputType,
+ [SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS]: inputTokens,
+ [SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS]: outputTokens,
+ ...versionExtras,
+ };
+ if (responseId) eventAttrs[SemanticConvention.GEN_AI_RESPONSE_ID] = responseId;
+ if (finishReason) eventAttrs[SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON] = [finishReason];
+ if (captureContent) {
+ if (inputMessagesJson) eventAttrs[SemanticConvention.GEN_AI_INPUT_MESSAGES] = inputMessagesJson;
+ if (outputMessagesJson) eventAttrs[SemanticConvention.GEN_AI_OUTPUT_MESSAGES] = outputMessagesJson;
+ if (systemInstructionsJson) eventAttrs[SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS] = systemInstructionsJson;
+ }
+ if (toolDefinitionsJson) eventAttrs[SemanticConvention.GEN_AI_TOOL_DEFINITIONS] = toolDefinitionsJson;
+ OpenLitHelper.emitInferenceEvent(span, eventAttrs);
+ }
+
+ return {
+ genAIEndpoint,
+ model: requestModel,
+ user: undefined,
+ cost,
+ aiSystem: VertexAIWrapper.aiSystem,
+ serverAddress,
+ serverPort: VertexAIWrapper.serverPort,
+ };
+ }
+}
+
+export default VertexAIWrapper;
diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.ts
index 20e97f14..be41c711 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -3,7 +3,7 @@ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { metrics } from '@opentelemetry/api';
import type { Guard } from './guard/base';
-export type InstrumentationType = 'openai' | 'anthropic' | 'cohere' | 'groq' | 'mistral' | 'google-ai' | 'together' | 'ollama' | 'vercel-ai' | 'langchain' | 'langgraph' | 'pinecone' | 'bedrock' | 'llamaindex' | 'huggingface' | 'replicate' | 'chroma' | 'qdrant' | 'milvus' | 'astra' | 'azure-ai-inference' | 'openai-agents' | 'strands' | 'google-adk' | 'claude-agent-sdk' | 'cursor-sdk' | 'ai21' | 'gradient' | 'mcp';
+export type InstrumentationType = 'openai' | 'anthropic' | 'cohere' | 'groq' | 'mistral' | 'google-ai' | 'vertexai' | 'together' | 'ollama' | 'vercel-ai' | 'langchain' | 'langgraph' | 'pinecone' | 'bedrock' | 'llamaindex' | 'huggingface' | 'replicate' | 'chroma' | 'qdrant' | 'milvus' | 'astra' | 'azure-ai-inference' | 'openai-agents' | 'strands' | 'google-adk' | 'claude-agent-sdk' | 'cursor-sdk' | 'ai21' | 'gradient' | 'mcp';
export type OpenlitInstrumentations = Partial<Record<InstrumentationType, any>>;