Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Treat regional OpenAI API hosts as hosted OpenAI (#4836)

Krish Wanchoob0e6e4efdc01
diff --git a/app/models/ai_health.rb b/app/models/ai_health.rb
index ac7217c2f6..1d440b809a 100644
--- a/app/models/ai_health.rb
+++ b/app/models/ai_health.rb
@@ -387,12 +387,7 @@ class AiHealth
end
def hosted_openai_endpoint?(value)
- uri = URI.parse(value.to_s)
- normalized_path = uri.path.to_s.sub(%r{/+\z}, "")
-
- uri.scheme == "https" && uri.host == "api.openai.com" && uri.port == 443 && normalized_path.in?([ "", "/v1" ])
- rescue URI::InvalidURIError
- false
+ Provider::Openai.hosted_endpoint?(value)
end
def redact_endpoint(value)
diff --git a/app/models/family/ai_promptable.rb b/app/models/family/ai_promptable.rb
index 49b3079c04..31df9f0cda 100644
--- a/app/models/family/ai_promptable.rb
+++ b/app/models/family/ai_promptable.rb
@@ -18,7 +18,7 @@ module Family::AiPromptable
# it swaps a small local model's terse rules for a prompt written for GPT-4.
# Mirrors how Provider::Registry picks the endpoint.
def self.custom_openai_provider?
- (ENV["OPENAI_URI_BASE"].presence || Setting.openai_uri_base).present?
+ Provider::Openai.custom_endpoint?(ENV["OPENAI_URI_BASE"].presence || Setting.openai_uri_base)
end
# Each key's stock text, lazily. Constructing a provider class with
diff --git a/app/models/provider/openai.rb b/app/models/provider/openai.rb
index f11e902596..9f79d7c2f0 100644
--- a/app/models/provider/openai.rb
+++ b/app/models/provider/openai.rb
@@ -16,6 +16,30 @@ class Provider::Openai < Provider
SUPPORTED_MODELS = %w[gpt-4 gpt-5 gpt-6 o1 o3].freeze
VISION_CAPABLE_MODEL_PREFIXES = %w[gpt-4o gpt-4-turbo gpt-4.1 gpt-5 gpt-6 o1 o3].freeze
PDF_COMPLETION_LIMIT_MODEL_PREFIXES = %w[gpt-6 o1 o3].freeze
+ # api.openai.com and its regional hosts (us.api.openai.com), which some keys must use
+ HOSTED_API_HOST = /\A(?:[a-z0-9-]+\.)?api\.openai\.com\z/
+
+ # Whether a base URL points at OpenAI's own API rather than an
+ # OpenAI-compatible provider, so OpenAI-only features stay enabled.
+ # @param uri_base [String, nil] the configured base URL
+ # @return [Boolean]
+ def self.hosted_endpoint?(uri_base)
+ uri = URI.parse(uri_base.to_s)
+ normalized_path = uri.path.to_s.sub(%r{/+\z}, "")
+
+ uri.scheme == "https" && uri.host.to_s.downcase.match?(HOSTED_API_HOST) &&
+ uri.port == 443 && normalized_path.in?([ "", "/v1" ])
+ rescue URI::InvalidURIError
+ false
+ end
+
+ # Whether a base URL points at an OpenAI-compatible provider other than
+ # OpenAI. A blank base URL means OpenAI's default endpoint.
+ # @param uri_base [String, nil] the configured base URL
+ # @return [Boolean]
+ def self.custom_endpoint?(uri_base)
+ uri_base.present? && !hosted_endpoint?(uri_base)
+ end
# Keep the provider's response budget and the PDF request parameter in sync.
# @return [Boolean] whether native PDF vision uses max_completion_tokens
@@ -129,7 +153,7 @@ class Provider::Openai < Provider
end
def custom_provider?
- @uri_base.present?
+ self.class.custom_endpoint?(@uri_base)
end
# Token-budget knobs. Precedence: ENV > Setting > default. Defaults match
diff --git a/app/models/provider/registry.rb b/app/models/provider/registry.rb
index ec1a198bcf..48926becc8 100644
--- a/app/models/provider/registry.rb
+++ b/app/models/provider/registry.rb
@@ -87,7 +87,7 @@ class Provider::Registry
uri_base = ENV["OPENAI_URI_BASE"].presence || Setting.openai_uri_base
model = ENV["OPENAI_MODEL"].presence || Setting.openai_model
- if uri_base.present? && model.blank?
+ if Provider::Openai.custom_endpoint?(uri_base) && model.blank?
Rails.logger.error("Custom OpenAI provider configured without a model; please set OPENAI_MODEL or Setting.openai_model")
return nil
end
diff --git a/app/models/setting.rb b/app/models/setting.rb
index 7357b54af1..dbe567d7e1 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -343,8 +343,9 @@ class Setting < RailsSettings::Base
uri_base_value = uri_base.nil? ? openai_uri_base : uri_base
model_value = model.nil? ? openai_model : model
- # If custom URI base is set, model must also be set
- if uri_base_value.present? && model_value.blank?
+ # If a custom URI base is set, model must also be set. OpenAI's own
+ # hosts (including regional ones) fall back to the default model.
+ if Provider::Openai.custom_endpoint?(uri_base_value) && model_value.blank?
raise ValidationError, "OpenAI model is required when custom URI base is configured"
end
end
diff --git a/test/models/family/ai_promptable_test.rb b/test/models/family/ai_promptable_test.rb
index 4ba767e913..a4f3c3470f 100644
--- a/test/models/family/ai_promptable_test.rb
+++ b/test/models/family/ai_promptable_test.rb
@@ -55,6 +55,16 @@ class Family::AiPromptableTest < ActiveSupport::TestCase
assert_not_includes @family.ai_prompt_default(:merchant_openai), '{"merchants":'
end
+ test "a regional OpenAI host counts as native OpenAI for prompt defaults" do
+ ClimateControl.modify("OPENAI_URI_BASE" => "https://us.api.openai.com") do
+ assert_not Family::AiPromptable.custom_openai_provider?
+ end
+
+ ClimateControl.modify("OPENAI_URI_BASE" => "https://llm.example.com/v1") do
+ assert Family::AiPromptable.custom_openai_provider?
+ end
+ end
+
# Advisory only. It has to stay quiet for the pre-filled default (which now
# carries the key on custom providers), or admins learn to ignore it.
test "flags only an openai override that dropped the wrapper key" do
diff --git a/test/models/provider/openai_test.rb b/test/models/provider/openai_test.rb
index 14c232768c..0c46b173e7 100644
--- a/test/models/provider/openai_test.rb
+++ b/test/models/provider/openai_test.rb
@@ -107,6 +107,30 @@ class Provider::OpenaiTest < ActiveSupport::TestCase
assert local.supports_model?("qwen2.5:32b")
end
+ # Some keys only work on a regional host, so setting one must not turn off
+ # the OpenAI-only paths like the Responses API and max_completion_tokens.
+ test "an OpenAI regional host is hosted OpenAI, not a custom provider" do
+ with_env_overrides("OPENAI_SUPPORTS_RESPONSES_ENDPOINT" => nil) do
+ regional = Provider::Openai.new("test-token", uri_base: "https://us.api.openai.com")
+
+ assert_not regional.custom_provider?
+ assert regional.supports_responses_endpoint?
+ assert_equal "OpenAI", regional.provider_name
+ assert Provider::Openai.native_pdf_completion_limit?(model: "gpt-6-luna", custom_provider: regional.custom_provider?)
+ end
+ end
+
+ test "only OpenAI's own hosts count as a hosted OpenAI endpoint" do
+ [ "https://api.openai.com", "https://api.openai.com/v1/", "https://us.api.openai.com", "https://EU.api.openai.com/v1" ].each do |url|
+ assert Provider::Openai.hosted_endpoint?(url), url
+ end
+
+ [ "http://us.api.openai.com", "https://evil-api.openai.com", "https://api.openai.com.example.com",
+ "https://us.api.openai.com/proxy", "https://us.api.openai.com:8443", "https://custom.example/v1", "not a url" ].each do |url|
+ assert_not Provider::Openai.hosted_endpoint?(url), url
+ end
+ end
+
test "legacy and custom PDF processing preserve the fallback output limit" do
with_env_overrides("LLM_MAX_RESPONSE_TOKENS" => nil) do
Setting.stubs(:llm_max_response_tokens).returns(nil)
diff --git a/test/models/provider/registry_test.rb b/test/models/provider/registry_test.rb
index 80b1d0a0fb..e1858ba4d3 100644
--- a/test/models/provider/registry_test.rb
+++ b/test/models/provider/registry_test.rb
@@ -108,6 +108,33 @@ class Provider::RegistryTest < ActiveSupport::TestCase
end
end
+ test "openai provider accepts a regional OpenAI host without a model" do
+ ClimateControl.modify(
+ "OPENAI_ACCESS_TOKEN" => "test-token",
+ "OPENAI_URI_BASE" => "https://us.api.openai.com/v1",
+ "OPENAI_MODEL" => ""
+ ) do
+ Setting.stubs(:openai_model).returns(nil)
+
+ provider = Provider::Registry.get_provider(:openai)
+
+ assert_instance_of Provider::Openai, provider
+ assert_not provider.custom_provider?
+ end
+ end
+
+ test "openai provider still needs a model for a custom endpoint" do
+ ClimateControl.modify(
+ "OPENAI_ACCESS_TOKEN" => "test-token",
+ "OPENAI_URI_BASE" => "https://llm.example.com/v1",
+ "OPENAI_MODEL" => ""
+ ) do
+ Setting.stubs(:openai_model).returns(nil)
+
+ assert_nil Provider::Registry.get_provider(:openai)
+ end
+ end
+
test "preferred_llm_provider returns openai by default" do
openai = mock("openai")
Provider::Registry.stubs(:openai).returns(openai)
diff --git a/test/models/setting_test.rb b/test/models/setting_test.rb
index 824e48108a..aa29496a02 100644
--- a/test/models/setting_test.rb
+++ b/test/models/setting_test.rb
@@ -38,6 +38,12 @@ class SettingTest < ActiveSupport::TestCase
assert_match(/OpenAI model is required/, error.message)
end
+ test "validate_openai_config! passes for a regional OpenAI host without a model" do
+ assert_nothing_raised do
+ Setting.validate_openai_config!(uri_base: "https://us.api.openai.com", model: "")
+ end
+ end
+
test "validate_openai_config! uses current settings when parameters are nil" do
Setting.openai_uri_base = "https://api.example.com"
Setting.openai_model = "gpt-4"