Skip to content

Add backward-compatible OpenAI Responses API support with URL auto-detection#305

Merged
robgruen merged 8 commits into
mainfrom
copilot/test-backward-compatibility
May 4, 2026
Merged

Add backward-compatible OpenAI Responses API support with URL auto-detection#305
robgruen merged 8 commits into
mainfrom
copilot/test-backward-compatibility

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 25, 2026

PR #277 proposed switching the default OpenAI endpoint from /chat/completions to /responses, but the change was incomplete: the Responses API has a different request format (input vs messages) and response shape (output[0].content[0].text vs choices[0].message.content), making the PR as-is a silent breaking change.

Changes

  • createOpenAILanguageModel extended — accepts an optional fifth parameter useResponsesApi?: boolean and auto-detects the API variant from the endpoint URL (path ending with /responses):

    • useResponsesApi: true — forces Responses API regardless of URL
    • useResponsesApi: false — forces Chat Completions regardless of URL
    • useResponsesApi omitted (default) — inferred from the endpoint URL
  • URL auto-detection — any endpoint whose path ends with /responses (e.g. https://api.openai.com/v1/responses or an Azure deployment URL like https://{resource}.openai.azure.com/.../responses?api-version=...) automatically uses the Responses API request/response format; all other URLs use Chat Completions

  • createOpenAIResponsesLanguageModel removed — the separate function was never published and has been deleted; all Responses API usage goes through createOpenAILanguageModel

  • createLanguageModel simplified — the OPENAI_USE_RESPONSES_API env var is removed; to opt into the Responses API set OPENAI_ENDPOINT to a /responses URL and URL auto-detection handles the rest

  • retry-after header support — on transient error responses (429, 503, etc.), the Retry-After header value (seconds) is used as the back-off delay, capped at retryPauseMs × retryMaxAttempts to stay within the configured total retry budget

  • Improved documentationcreateResponsesFetchLanguageModel now includes a sample response JSON, links to OpenAI and Azure OpenAI Responses API docs, and full @param documentation

// Existing code — unchanged, backward compatible
const model = createOpenAILanguageModel(apiKey, "gpt-4");

// New: explicit Responses API via URL (auto-detected)
const model = createOpenAILanguageModel(apiKey, "gpt-4", "https://api.openai.com/v1/responses");

// New: explicit Responses API via flag
const model = createOpenAILanguageModel(apiKey, "gpt-4", undefined, undefined, true);

// Via createLanguageModel — set OPENAI_ENDPOINT to a /responses URL
// OPENAI_ENDPOINT=https://api.openai.com/v1/responses  →  uses /v1/responses
// (default)                                             →  uses /v1/chat/completions
const model = createLanguageModel(process.env);
  • Tests — 19 unit tests (typescript/tests/model.test.mjs) using Node's built-in node:test with mocked fetch, covering both API paths, URL auto-detection, the useResponsesApi flag, retry-after handling (429 and 503) for both paths, and createLanguageModel routing; "test" script added to package.json

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Verify backward compatibility for PR#277 changes Add backward-compatible OpenAI Responses API support with opt-in env var Feb 25, 2026
Comment thread typescript/src/model.ts Outdated
Comment thread typescript/src/model.ts Outdated
Comment thread typescript/src/model.ts Outdated
Comment thread typescript/src/model.ts
Comment thread typescript/src/model.ts Outdated
Copilot AI changed the title Add backward-compatible OpenAI Responses API support with opt-in env var Add backward-compatible OpenAI Responses API support with URL auto-detection May 4, 2026
Copilot AI requested a review from robgruen May 4, 2026 17:50
Comment thread typescript/src/model.ts Outdated
Comment thread typescript/src/model.ts Outdated
Copilot AI requested a review from robgruen May 4, 2026 18:23
@robgruen robgruen marked this pull request as ready for review May 4, 2026 18:56
@robgruen robgruen requested a review from TalZaccai May 4, 2026 19:08
@robgruen robgruen merged commit c90f683 into main May 4, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants