purra-openai 0.5.0 → 1.0.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.
package/dist/chat.d.ts CHANGED
@@ -14,6 +14,7 @@ export declare class OpenAIChatCompletionsGateway implements ModelGateway {
14
14
  #private;
15
15
  readonly capabilities: ModelCapabilitySnapshot;
16
16
  constructor(options: OpenAIChatCompletionsOptions);
17
+ validateOutputContract(request: ModelRequest): string | undefined;
17
18
  invoke(request: ModelRequest, signal?: AbortSignal): Promise<ModelTurn>;
18
19
  stream(request: ModelRequest, signal?: AbortSignal): Promise<ModelStream>;
19
20
  }
package/dist/chat.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { validateOutput } from "./structured.js";
1
2
  import OpenAI from "openai";
2
3
  import { AgentCanceledError, AgentError } from "purra";
3
4
  function text(value) {
@@ -64,11 +65,18 @@ export class OpenAIChatCompletionsGateway {
64
65
  this.#options = { ...options };
65
66
  this.#client = (options.client ?? new OpenAI()).withOptions({ maxRetries: 0, timeout: options.timeoutMs ?? 60000 });
66
67
  }
68
+ validateOutputContract(request) {
69
+ return validateOutput(request, this.capabilities);
70
+ }
67
71
  #request(request) {
72
+ this.validateOutputContract(request);
68
73
  if (!request.outputBudget)
69
74
  throw new TypeError("OpenAI gateway requires a resolved generation budget");
70
75
  const o = this.#options;
71
76
  return { model: o.model, messages: messages(request.messages), store: false, max_completion_tokens: request.outputBudget.maxGenerationTokens,
77
+ ...(request.outputContract?.mode === "native_required" ? { response_format: { type: "json_schema", json_schema: {
78
+ name: "purra_output", strict: true, schema: JSON.parse(JSON.stringify(request.outputContract.schema)),
79
+ } } } : {}),
72
80
  ...(o.reasoningEffort === undefined ? {} : { reasoning_effort: o.reasoningEffort }),
73
81
  ...(o.temperature === undefined ? {} : { temperature: o.temperature }),
74
82
  ...(o.topP === undefined ? {} : { top_p: o.topP }),
package/dist/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export declare class OpenAIResponsesGateway implements ModelGateway {
16
16
  #private;
17
17
  readonly capabilities: ModelCapabilitySnapshot;
18
18
  constructor(options: OpenAIResponsesOptions);
19
+ validateOutputContract(request: ModelRequest): string | undefined;
19
20
  invoke(request: ModelRequest, signal?: AbortSignal): Promise<ModelTurn>;
20
21
  stream(request: ModelRequest, signal?: AbortSignal): Promise<ModelStream>;
21
22
  }
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { validateOutput } from "./structured.js";
1
2
  import OpenAI from "openai";
2
3
  export { OpenAIChatCompletionsGateway } from "./chat.js";
3
4
  import { AgentCanceledError, AgentError } from "purra";
@@ -77,7 +78,11 @@ export class OpenAIResponsesGateway {
77
78
  this.#reasoning = options.reasoning;
78
79
  this.#client = (options.client ?? new OpenAI()).withOptions({ maxRetries: 0, timeout: options.timeoutMs ?? 60000 });
79
80
  }
81
+ validateOutputContract(request) {
82
+ return validateOutput(request, this.capabilities);
83
+ }
80
84
  #request(request) {
85
+ this.validateOutputContract(request);
81
86
  if (!request.outputBudget)
82
87
  throw new TypeError("OpenAI gateway requires a resolved generation budget");
83
88
  return { model: this.#model, input: input(request.messages), store: false, include: ["reasoning.encrypted_content"],
@@ -85,6 +90,9 @@ export class OpenAIResponsesGateway {
85
90
  tools: request.tools.map(t => ({ type: "function", name: t.name, description: t.description,
86
91
  parameters: JSON.parse(JSON.stringify(t.inputSchema)), strict: false })),
87
92
  tool_choice: request.tools.length ? "auto" : "none",
93
+ ...(request.outputContract?.mode === "native_required" ? { text: { format: {
94
+ type: "json_schema", name: "purra_output", strict: true, schema: JSON.parse(JSON.stringify(request.outputContract.schema)),
95
+ } } } : {}),
88
96
  ...(this.#reasoning === undefined ? {} : { reasoning: this.#reasoning }), };
89
97
  }
90
98
  async invoke(request, signal) {
@@ -0,0 +1,2 @@
1
+ import type { ModelCapabilitySnapshot, ModelRequest } from "purra";
2
+ export declare function validateOutput(request: ModelRequest, capabilities: ModelCapabilitySnapshot): string | undefined;
@@ -0,0 +1,49 @@
1
+ import { StructuredOutputError } from "purra";
2
+ const allowed = new Set(["type", "properties", "required", "additionalProperties", "items", "enum", "description", "title"]);
3
+ export function validateOutput(request, capabilities) {
4
+ const output = request.outputContract;
5
+ if (output === undefined || output.mode === "local")
6
+ return undefined;
7
+ function reject() { throw new StructuredOutputError("structured_output_mode_unsupported", "nativeDialect"); }
8
+ if (request.tools.length || capabilities.protocol.jsonSchemaLevel !== "json_schema"
9
+ || request.capabilitySnapshot?.protocol.jsonSchemaLevel !== "json_schema")
10
+ reject();
11
+ let enumCount = 0, propertyCount = 0;
12
+ function visit(node, depth) {
13
+ if (depth > 8 || Object.keys(node).some(key => !allowed.has(key)))
14
+ reject();
15
+ let kind = node.type;
16
+ if (typeof kind !== "string") {
17
+ if (!Array.isArray(kind) || kind.length !== 2 || !kind.includes("null"))
18
+ reject();
19
+ kind = kind.find(t => t !== "null");
20
+ }
21
+ if (kind === "object") {
22
+ const props = (node.properties ?? {});
23
+ propertyCount += Object.keys(props).length;
24
+ const required = (node.required ?? []);
25
+ if (propertyCount > 100 || node.additionalProperties !== false || !Array.isArray(required)
26
+ || required.length !== Object.keys(props).length
27
+ || Object.keys(props).some(key => !required.includes(key)))
28
+ reject();
29
+ for (const child of Object.values(props))
30
+ visit(child, depth + 1);
31
+ }
32
+ else if (["properties", "required", "additionalProperties"].some(key => key in node))
33
+ reject();
34
+ if (kind === "array") {
35
+ if (!node.items || typeof node.items !== "object" || Array.isArray(node.items))
36
+ reject();
37
+ visit(node.items, depth + 1);
38
+ }
39
+ else if ("items" in node)
40
+ reject();
41
+ if (Array.isArray(node.enum)) {
42
+ enumCount += node.enum.length;
43
+ if (enumCount > 100 || node.enum.some(v => v !== null && typeof v === "object"))
44
+ reject();
45
+ }
46
+ }
47
+ visit(output.schema, 0);
48
+ return "purra.openai-json-schema/v1";
49
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "purra-openai",
3
- "version": "0.5.0",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Official OpenAI Responses and Chat Completions SDK adapter for PurrA",
@@ -20,7 +20,7 @@
20
20
  "openai": "7.9.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "purra": "0.5.0"
23
+ "purra": "1.0.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "purra": "file:../../../typescript",