@@ -154,16 +154,23 @@ create or replace package body uc_ai_structured_output as
154154 */
155155 function to_openai_format(
156156 p_schema in json_object_t,
157- p_name in varchar2 default 'structured_output',
158157 p_strict in boolean default true
159158 ) return json_object_t
160159 as
161160 l_scope uc_ai_logger.scope := c_scope_prefix || 'to_openai_format';
162161 l_response_format json_object_t := json_object_t();
163162 l_schema_copy json_object_t;
164163 l_json_schema json_object_t;
164+ l_name varchar2(4000 char) := 'structured_output';
165165 begin
166- uc_ai_logger.log('Converting schema to OpenAI format', l_scope, 'Name: ' || p_name || ', Strict: ' || case when p_strict then 'true' else 'false' end);
166+ uc_ai_logger.log('Converting schema to OpenAI format', l_scope, ' Strict: ' || case when p_strict then 'true' else 'false' end);
167+
168+ if p_schema.has('title') then
169+ l_name := replace(p_schema.get_string('title'), ' ', '_');
170+ -- make it match [a-zA-Z0-9_-]+
171+ l_name := regexp_replace(l_name, '[^a-zA-Z0-9_-]', null);
172+ l_name := coalesce(l_name, 'structured_output');
173+ end if;
167174
168175 -- Create a copy of the input schema and process for strict mode
169176 if p_strict then
@@ -175,7 +182,7 @@ create or replace package body uc_ai_structured_output as
175182 l_response_format.put('type', 'json_schema');
176183
177184 l_json_schema := json_object_t();
178- l_json_schema.put('name', p_name );
185+ l_json_schema.put('name', l_name );
179186 l_json_schema.put('schema', l_schema_copy);
180187 l_json_schema.put('strict', p_strict);
181188
@@ -221,13 +228,51 @@ create or replace package body uc_ai_structured_output as
221228 return l_schema_copy;
222229 end to_ollama_format;
223230
231+ /*
232+ * Convert a standard JSON schema to Responses API format for structured output
233+ */
234+ function to_responses_api_format(
235+ p_schema in json_object_t,
236+ p_strict in boolean default true
237+ ) return json_object_t
238+ as
239+ l_scope uc_ai_logger.scope := c_scope_prefix || 'to_responses_api_format';
240+ l_response_format json_object_t := json_object_t();
241+ l_schema_copy json_object_t;
242+ l_name varchar2(4000 char) := 'structured_output';
243+ begin
244+ uc_ai_logger.log('Converting schema to Responses API format', l_scope, ' Strict: ' || case when p_strict then 'true' else 'false' end);
245+
246+ if p_schema.has('title') then
247+ l_name := replace(p_schema.get_string('title'), ' ', '_');
248+ -- make it match [a-zA-Z0-9_-]+
249+ l_name := regexp_replace(l_name, '[^a-zA-Z0-9_-]', null);
250+ l_name := coalesce(l_name, 'structured_output');
251+ end if;
252+
253+ -- Create a copy of the input schema and process for strict mode
254+ if p_strict then
255+ l_schema_copy := process_openai_strict_schema(p_schema);
256+ else
257+ l_schema_copy := json_object_t(p_schema.to_clob);
258+ end if;
259+
260+ l_response_format.put('type', 'json_schema');
261+ l_response_format.put('name', l_name);
262+ l_response_format.put('strict', p_strict);
263+ l_response_format.put('schema', l_schema_copy);
264+
265+ uc_ai_logger.log('Responses API format conversion complete', l_scope, l_response_format.to_clob);
266+ return l_response_format;
267+ end to_responses_api_format;
268+
269+
224270 /*
225271 * Generic function to convert schema based on provider
226272 */
227273 function format_schema(
228274 p_schema in json_object_t,
229275 p_provider in uc_ai.provider_type,
230- p_name in varchar2 default 'structured_output',
231276 p_strict in boolean default true
232277 ) return json_object_t
233278 as
@@ -238,7 +283,7 @@ create or replace package body uc_ai_structured_output as
238283
239284 case p_provider
240285 when uc_ai.c_provider_openai then
241- l_result := to_openai_format(p_schema, p_name, p_strict);
286+ l_result := to_openai_format(p_schema, p_strict);
242287 when uc_ai.c_provider_google then
243288 l_result := to_google_format(p_schema);
244289 when uc_ai.c_provider_ollama then
0 commit comments