@langchain/openrouter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +174 -0
  3. package/dist/api-types.d.cts +368 -0
  4. package/dist/api-types.d.cts.map +1 -0
  5. package/dist/api-types.d.ts +368 -0
  6. package/dist/api-types.d.ts.map +1 -0
  7. package/dist/chat_models/index.cjs +401 -0
  8. package/dist/chat_models/index.cjs.map +1 -0
  9. package/dist/chat_models/index.d.cts +160 -0
  10. package/dist/chat_models/index.d.cts.map +1 -0
  11. package/dist/chat_models/index.d.ts +160 -0
  12. package/dist/chat_models/index.d.ts.map +1 -0
  13. package/dist/chat_models/index.js +401 -0
  14. package/dist/chat_models/index.js.map +1 -0
  15. package/dist/chat_models/types.d.cts +97 -0
  16. package/dist/chat_models/types.d.cts.map +1 -0
  17. package/dist/chat_models/types.d.ts +97 -0
  18. package/dist/chat_models/types.d.ts.map +1 -0
  19. package/dist/converters/messages.cjs +90 -0
  20. package/dist/converters/messages.cjs.map +1 -0
  21. package/dist/converters/messages.js +87 -0
  22. package/dist/converters/messages.js.map +1 -0
  23. package/dist/converters/tools.cjs +30 -0
  24. package/dist/converters/tools.cjs.map +1 -0
  25. package/dist/converters/tools.js +29 -0
  26. package/dist/converters/tools.js.map +1 -0
  27. package/dist/index.cjs +10 -0
  28. package/dist/index.d.cts +6 -0
  29. package/dist/index.d.ts +6 -0
  30. package/dist/index.js +5 -0
  31. package/dist/profiles.cjs +2570 -0
  32. package/dist/profiles.cjs.map +1 -0
  33. package/dist/profiles.d.cts +7 -0
  34. package/dist/profiles.d.cts.map +1 -0
  35. package/dist/profiles.d.ts +7 -0
  36. package/dist/profiles.d.ts.map +1 -0
  37. package/dist/profiles.js +2569 -0
  38. package/dist/profiles.js.map +1 -0
  39. package/dist/utils/errors.cjs +82 -0
  40. package/dist/utils/errors.cjs.map +1 -0
  41. package/dist/utils/errors.d.cts +68 -0
  42. package/dist/utils/errors.d.cts.map +1 -0
  43. package/dist/utils/errors.d.ts +68 -0
  44. package/dist/utils/errors.d.ts.map +1 -0
  45. package/dist/utils/errors.js +80 -0
  46. package/dist/utils/errors.js.map +1 -0
  47. package/dist/utils/stream.cjs +28 -0
  48. package/dist/utils/stream.cjs.map +1 -0
  49. package/dist/utils/stream.js +27 -0
  50. package/dist/utils/stream.js.map +1 -0
  51. package/dist/utils/structured_output.cjs +44 -0
  52. package/dist/utils/structured_output.cjs.map +1 -0
  53. package/dist/utils/structured_output.js +43 -0
  54. package/dist/utils/structured_output.js.map +1 -0
  55. package/package.json +86 -0
@@ -0,0 +1,44 @@
1
+
2
+ //#region src/utils/structured_output.ts
3
+ /**
4
+ * The three strategies OpenRouter can use to extract structured output:
5
+ *
6
+ * - `"jsonSchema"` — native JSON Schema response format (only models that
7
+ * advertise `structuredOutput` in their profile support this).
8
+ * - `"functionCalling"` — wraps the schema as a tool/function call and
9
+ * parses the tool output. Works on any model that supports tools.
10
+ * - `"jsonMode"` — asks the model to respond in JSON without a strict
11
+ * schema constraint (`response_format: { type: "json_object" }`).
12
+ */
13
+ const SUPPORTED_STRUCTURED_OUTPUT_METHODS = [
14
+ "jsonSchema",
15
+ "functionCalling",
16
+ "jsonMode"
17
+ ];
18
+ /**
19
+ * Determines which structured-output strategy to use for a given model
20
+ * and caller configuration.
21
+ *
22
+ * Resolution order:
23
+ * 1. If the caller explicitly requested a method, validate and return it
24
+ * (throws if the method is unsupported or incompatible with the model).
25
+ * 2. If OpenRouter routing is active (multi-model `models` list or
26
+ * `route: "fallback"`), fall back to `"functionCalling"` because the
27
+ * actual backend model — and its capabilities — are unknown at request
28
+ * time.
29
+ * 3. Otherwise, pick the best method the model supports: `"jsonSchema"`
30
+ * when the profile advertises native structured output, else
31
+ * `"functionCalling"`.
32
+ */
33
+ function resolveOpenRouterStructuredOutputMethod({ model, method, profile, models, route }) {
34
+ if (method !== void 0 && !SUPPORTED_STRUCTURED_OUTPUT_METHODS.includes(method)) throw new Error(`Invalid structured output method: ${String(method)}. Supported methods are: ${SUPPORTED_STRUCTURED_OUTPUT_METHODS.join(", ")}`);
35
+ const supportsStructuredOutput = profile.structuredOutput === true;
36
+ if (method === "jsonSchema" && !supportsStructuredOutput) throw new Error(`Structured output method "jsonSchema" is not supported for model "${model}". Use "functionCalling" or "jsonMode" instead.`);
37
+ if (method !== void 0) return method;
38
+ if (route === "fallback" || (models?.length ?? 0) > 0) return "functionCalling";
39
+ return supportsStructuredOutput ? "jsonSchema" : "functionCalling";
40
+ }
41
+
42
+ //#endregion
43
+ exports.resolveOpenRouterStructuredOutputMethod = resolveOpenRouterStructuredOutputMethod;
44
+ //# sourceMappingURL=structured_output.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structured_output.cjs","names":[],"sources":["../../src/utils/structured_output.ts"],"sourcesContent":["import type { ModelProfile } from \"@langchain/core/language_models/profile\";\n\n/**\n * The three strategies OpenRouter can use to extract structured output:\n *\n * - `\"jsonSchema\"` — native JSON Schema response format (only models that\n * advertise `structuredOutput` in their profile support this).\n * - `\"functionCalling\"` — wraps the schema as a tool/function call and\n * parses the tool output. Works on any model that supports tools.\n * - `\"jsonMode\"` — asks the model to respond in JSON without a strict\n * schema constraint (`response_format: { type: \"json_object\" }`).\n */\nconst SUPPORTED_STRUCTURED_OUTPUT_METHODS = [\n \"jsonSchema\",\n \"functionCalling\",\n \"jsonMode\",\n] as const;\n\nexport type OpenRouterStructuredOutputMethod =\n (typeof SUPPORTED_STRUCTURED_OUTPUT_METHODS)[number];\n\ninterface ResolveStructuredOutputMethodParams {\n /** The model identifier, e.g. `\"anthropic/claude-4-sonnet\"`. */\n model: string;\n /** Caller-requested method, or `undefined` to auto-detect. */\n method: unknown;\n /** Static capability profile for the model. */\n profile: ModelProfile;\n /** Optional list of candidate models used with OpenRouter routing. */\n models?: string[];\n /** Optional routing strategy (currently only `\"fallback\"`). */\n route?: \"fallback\";\n}\n\n/**\n * Determines which structured-output strategy to use for a given model\n * and caller configuration.\n *\n * Resolution order:\n * 1. If the caller explicitly requested a method, validate and return it\n * (throws if the method is unsupported or incompatible with the model).\n * 2. If OpenRouter routing is active (multi-model `models` list or\n * `route: \"fallback\"`), fall back to `\"functionCalling\"` because the\n * actual backend model — and its capabilities — are unknown at request\n * time.\n * 3. Otherwise, pick the best method the model supports: `\"jsonSchema\"`\n * when the profile advertises native structured output, else\n * `\"functionCalling\"`.\n */\nexport function resolveOpenRouterStructuredOutputMethod({\n model,\n method,\n profile,\n models,\n route,\n}: ResolveStructuredOutputMethodParams): OpenRouterStructuredOutputMethod {\n if (\n method !== undefined &&\n !SUPPORTED_STRUCTURED_OUTPUT_METHODS.includes(\n method as OpenRouterStructuredOutputMethod\n )\n ) {\n throw new Error(\n `Invalid structured output method: ${String(\n method\n )}. Supported methods are: ${SUPPORTED_STRUCTURED_OUTPUT_METHODS.join(\n \", \"\n )}`\n );\n }\n\n const supportsStructuredOutput = profile.structuredOutput === true;\n\n if (method === \"jsonSchema\" && !supportsStructuredOutput) {\n throw new Error(\n `Structured output method \"jsonSchema\" is not supported for model \"${model}\". Use \"functionCalling\" or \"jsonMode\" instead.`\n );\n }\n\n if (method !== undefined) {\n return method as OpenRouterStructuredOutputMethod;\n }\n\n const hasRoutedModelSelection =\n route === \"fallback\" || (models?.length ?? 0) > 0;\n\n if (hasRoutedModelSelection) {\n return \"functionCalling\";\n }\n\n return supportsStructuredOutput ? \"jsonSchema\" : \"functionCalling\";\n}\n"],"mappings":";;;;;;;;;;;;AAYA,MAAM,sCAAsC;CAC1C;CACA;CACA;CACD;;;;;;;;;;;;;;;;AAiCD,SAAgB,wCAAwC,EACtD,OACA,QACA,SACA,QACA,SACwE;AACxE,KACE,WAAW,UACX,CAAC,oCAAoC,SACnC,OACD,CAED,OAAM,IAAI,MACR,qCAAqC,OACnC,OACD,CAAC,2BAA2B,oCAAoC,KAC/D,KACD,GACF;CAGH,MAAM,2BAA2B,QAAQ,qBAAqB;AAE9D,KAAI,WAAW,gBAAgB,CAAC,yBAC9B,OAAM,IAAI,MACR,qEAAqE,MAAM,iDAC5E;AAGH,KAAI,WAAW,OACb,QAAO;AAMT,KAFE,UAAU,eAAe,QAAQ,UAAU,KAAK,EAGhD,QAAO;AAGT,QAAO,2BAA2B,eAAe"}
@@ -0,0 +1,43 @@
1
+ //#region src/utils/structured_output.ts
2
+ /**
3
+ * The three strategies OpenRouter can use to extract structured output:
4
+ *
5
+ * - `"jsonSchema"` — native JSON Schema response format (only models that
6
+ * advertise `structuredOutput` in their profile support this).
7
+ * - `"functionCalling"` — wraps the schema as a tool/function call and
8
+ * parses the tool output. Works on any model that supports tools.
9
+ * - `"jsonMode"` — asks the model to respond in JSON without a strict
10
+ * schema constraint (`response_format: { type: "json_object" }`).
11
+ */
12
+ const SUPPORTED_STRUCTURED_OUTPUT_METHODS = [
13
+ "jsonSchema",
14
+ "functionCalling",
15
+ "jsonMode"
16
+ ];
17
+ /**
18
+ * Determines which structured-output strategy to use for a given model
19
+ * and caller configuration.
20
+ *
21
+ * Resolution order:
22
+ * 1. If the caller explicitly requested a method, validate and return it
23
+ * (throws if the method is unsupported or incompatible with the model).
24
+ * 2. If OpenRouter routing is active (multi-model `models` list or
25
+ * `route: "fallback"`), fall back to `"functionCalling"` because the
26
+ * actual backend model — and its capabilities — are unknown at request
27
+ * time.
28
+ * 3. Otherwise, pick the best method the model supports: `"jsonSchema"`
29
+ * when the profile advertises native structured output, else
30
+ * `"functionCalling"`.
31
+ */
32
+ function resolveOpenRouterStructuredOutputMethod({ model, method, profile, models, route }) {
33
+ if (method !== void 0 && !SUPPORTED_STRUCTURED_OUTPUT_METHODS.includes(method)) throw new Error(`Invalid structured output method: ${String(method)}. Supported methods are: ${SUPPORTED_STRUCTURED_OUTPUT_METHODS.join(", ")}`);
34
+ const supportsStructuredOutput = profile.structuredOutput === true;
35
+ if (method === "jsonSchema" && !supportsStructuredOutput) throw new Error(`Structured output method "jsonSchema" is not supported for model "${model}". Use "functionCalling" or "jsonMode" instead.`);
36
+ if (method !== void 0) return method;
37
+ if (route === "fallback" || (models?.length ?? 0) > 0) return "functionCalling";
38
+ return supportsStructuredOutput ? "jsonSchema" : "functionCalling";
39
+ }
40
+
41
+ //#endregion
42
+ export { resolveOpenRouterStructuredOutputMethod };
43
+ //# sourceMappingURL=structured_output.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structured_output.js","names":[],"sources":["../../src/utils/structured_output.ts"],"sourcesContent":["import type { ModelProfile } from \"@langchain/core/language_models/profile\";\n\n/**\n * The three strategies OpenRouter can use to extract structured output:\n *\n * - `\"jsonSchema\"` — native JSON Schema response format (only models that\n * advertise `structuredOutput` in their profile support this).\n * - `\"functionCalling\"` — wraps the schema as a tool/function call and\n * parses the tool output. Works on any model that supports tools.\n * - `\"jsonMode\"` — asks the model to respond in JSON without a strict\n * schema constraint (`response_format: { type: \"json_object\" }`).\n */\nconst SUPPORTED_STRUCTURED_OUTPUT_METHODS = [\n \"jsonSchema\",\n \"functionCalling\",\n \"jsonMode\",\n] as const;\n\nexport type OpenRouterStructuredOutputMethod =\n (typeof SUPPORTED_STRUCTURED_OUTPUT_METHODS)[number];\n\ninterface ResolveStructuredOutputMethodParams {\n /** The model identifier, e.g. `\"anthropic/claude-4-sonnet\"`. */\n model: string;\n /** Caller-requested method, or `undefined` to auto-detect. */\n method: unknown;\n /** Static capability profile for the model. */\n profile: ModelProfile;\n /** Optional list of candidate models used with OpenRouter routing. */\n models?: string[];\n /** Optional routing strategy (currently only `\"fallback\"`). */\n route?: \"fallback\";\n}\n\n/**\n * Determines which structured-output strategy to use for a given model\n * and caller configuration.\n *\n * Resolution order:\n * 1. If the caller explicitly requested a method, validate and return it\n * (throws if the method is unsupported or incompatible with the model).\n * 2. If OpenRouter routing is active (multi-model `models` list or\n * `route: \"fallback\"`), fall back to `\"functionCalling\"` because the\n * actual backend model — and its capabilities — are unknown at request\n * time.\n * 3. Otherwise, pick the best method the model supports: `\"jsonSchema\"`\n * when the profile advertises native structured output, else\n * `\"functionCalling\"`.\n */\nexport function resolveOpenRouterStructuredOutputMethod({\n model,\n method,\n profile,\n models,\n route,\n}: ResolveStructuredOutputMethodParams): OpenRouterStructuredOutputMethod {\n if (\n method !== undefined &&\n !SUPPORTED_STRUCTURED_OUTPUT_METHODS.includes(\n method as OpenRouterStructuredOutputMethod\n )\n ) {\n throw new Error(\n `Invalid structured output method: ${String(\n method\n )}. Supported methods are: ${SUPPORTED_STRUCTURED_OUTPUT_METHODS.join(\n \", \"\n )}`\n );\n }\n\n const supportsStructuredOutput = profile.structuredOutput === true;\n\n if (method === \"jsonSchema\" && !supportsStructuredOutput) {\n throw new Error(\n `Structured output method \"jsonSchema\" is not supported for model \"${model}\". Use \"functionCalling\" or \"jsonMode\" instead.`\n );\n }\n\n if (method !== undefined) {\n return method as OpenRouterStructuredOutputMethod;\n }\n\n const hasRoutedModelSelection =\n route === \"fallback\" || (models?.length ?? 0) > 0;\n\n if (hasRoutedModelSelection) {\n return \"functionCalling\";\n }\n\n return supportsStructuredOutput ? \"jsonSchema\" : \"functionCalling\";\n}\n"],"mappings":";;;;;;;;;;;AAYA,MAAM,sCAAsC;CAC1C;CACA;CACA;CACD;;;;;;;;;;;;;;;;AAiCD,SAAgB,wCAAwC,EACtD,OACA,QACA,SACA,QACA,SACwE;AACxE,KACE,WAAW,UACX,CAAC,oCAAoC,SACnC,OACD,CAED,OAAM,IAAI,MACR,qCAAqC,OACnC,OACD,CAAC,2BAA2B,oCAAoC,KAC/D,KACD,GACF;CAGH,MAAM,2BAA2B,QAAQ,qBAAqB;AAE9D,KAAI,WAAW,gBAAgB,CAAC,yBAC9B,OAAM,IAAI,MACR,qEAAqE,MAAM,iDAC5E;AAGH,KAAI,WAAW,OACb,QAAO;AAMT,KAFE,UAAU,eAAe,QAAQ,UAAU,KAAK,EAGhD,QAAO;AAGT,QAAO,2BAA2B,eAAe"}
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@langchain/openrouter",
3
+ "version": "0.1.0",
4
+ "description": "OpenRouter integration for LangChain.js",
5
+ "author": "LangChain",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "engines": {
9
+ "node": ">=20"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git@github.com:langchain-ai/langchainjs.git",
14
+ "directory": "libs/providers/langchain-openrouter"
15
+ },
16
+ "dependencies": {
17
+ "eventsource-parser": "^3.0.6",
18
+ "openai": "^6.22.0",
19
+ "@langchain/openai": "1.2.8"
20
+ },
21
+ "peerDependencies": {
22
+ "@langchain/core": "^1.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@tsconfig/recommended": "^1.0.10",
26
+ "dpdm": "^3.14.0",
27
+ "eslint": "^9.34.0",
28
+ "prettier": "^2.8.3",
29
+ "sparktype": "^0.1.3",
30
+ "typescript": "~5.8.3",
31
+ "vitest": "^3.2.4",
32
+ "zod": "^3.25.76 || ^4",
33
+ "zod-to-json-schema": "^3.24.6",
34
+ "@langchain/openai": "^1.2.8",
35
+ "@langchain/eslint": "0.1.1",
36
+ "@langchain/standard-tests": "0.0.23",
37
+ "@langchain/core": "^1.1.26"
38
+ },
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "exports": {
43
+ ".": {
44
+ "input": "./src/index.ts",
45
+ "require": {
46
+ "types": "./dist/index.d.cts",
47
+ "default": "./dist/index.cjs"
48
+ },
49
+ "import": {
50
+ "types": "./dist/index.d.ts",
51
+ "default": "./dist/index.js"
52
+ }
53
+ },
54
+ "./package.json": "./package.json"
55
+ },
56
+ "files": [
57
+ "dist/",
58
+ "CHANGELOG.md",
59
+ "README.md",
60
+ "LICENSE"
61
+ ],
62
+ "main": "./dist/index.cjs",
63
+ "module": "./dist/index.js",
64
+ "types": "./dist/index.d.cts",
65
+ "scripts": {
66
+ "build": "turbo build:compile --filter @langchain/openrouter --output-logs new-only",
67
+ "build:compile": "tsdown",
68
+ "lint:eslint": "eslint --cache src/",
69
+ "lint:dpdm": "dpdm --skip-dynamic-imports circular --exit-code circular:1 --no-warning --no-tree src/**/*.ts",
70
+ "lint": "pnpm lint:eslint && pnpm lint:dpdm",
71
+ "lint:fix": "pnpm lint:eslint --fix && pnpm lint:dpdm",
72
+ "clean": "rm -rf .turbo dist/",
73
+ "test": "vitest run",
74
+ "test:watch": "vitest",
75
+ "test:int": "vitest run --mode int",
76
+ "test:standard:unit": "vitest run --mode standard-unit",
77
+ "test:standard:int": "vitest run --mode standard-int",
78
+ "test:standard": "pnpm test:standard:unit && pnpm test:standard:int",
79
+ "format": "prettier --write \"src\"",
80
+ "format:check": "prettier --check \"src\"",
81
+ "typegen": "pnpm run typegen:sparktype && pnpm run typegen:profiles",
82
+ "typegen:sparktype": "sparktype generate",
83
+ "typegen:profiles": "pnpm --filter @langchain/model-profiles make --config profiles.toml",
84
+ "typegen:check": "sparktype check"
85
+ }
86
+ }