assistants/migration.md +126 −142
26 <tbody>26 <tbody>
27 <tr>27 <tr>
28 <td>`Assistants`</td>28 <td>`Assistants`</td>
2929 <td>`Prompts`</td> <td>Responses request configuration</td>
30 <td>30 <td>
3131 Prompts hold configuration (model, tools, instructions) and are easier Keep the model, instructions, and tools in your application and pass
3232 to version and update them in the request.
33 </td>33 </td>
34 </tr>34 </tr>
35 <tr>35 <tr>
55 </tbody>55 </tbody>
56</table>56</table>
57 57
58## Migrate Ruby applications
59
60The [Ruby SDK migration guide](https://developers.openai.com/api/docs/guides/ruby-sdk-migration) lists
61the retired Assistants and Threads bindings. Keep your model choice,
62instructions, and tool definitions in your application. Pass them as
63`model:`, `instructions:`, and `tools:` when you call
64`client.responses.create`.
65
66Create a Conversation for each thread you migrate. Convert messages and tool
67results from your own stored history to Conversation items and keep them in
68their original order. Store the new Conversation ID with the original thread
69ID.
70
71Create a Conversation with saved messages
72
73```ruby
74require "openai"
75
76client = OpenAI::Client.new
77conversation = client.conversations.create(
78 items: [
79 {
80 role: :user,
81 content: "What are the five Ds of dodgeball?"
82 }
83 ],
84 metadata: { user_id: "peter_le_fleur" }
85)
86puts(conversation.id)
87```
88
89
90Pass the returned ID as `conversation:` on later requests. For
91application-run function tools, send each result as a `function_call_output`
92item with the matching `call_id`.
93
94Continue the Conversation with a Response
95
96```ruby
97require "openai"
98
99client = OpenAI::Client.new
100response = client.responses.create(
101 model: "gpt-6.2-astra",
102 input: [
103 {
104 role: :user,
105 content: "What are the five Ds of dodgeball?"
106 }
107 ],
108 conversation: "conv_abc123"
109)
110puts(response.output_text)
111```
112
113
58## From assistants to prompts114## From assistants to prompts
59 115
60116Assistants were persistent API objects that bundled model choice, instructions, and tool declarations—created and managed entirely through the API. Their replacement, prompts, can only be created in the dashboard, where you can version them as you develop your product.Assistants were persistent API objects that bundled model choice, instructions, and tool declarations. Earlier migrations moved that configuration into reusable prompts created in the dashboard.
61 117
62### Why this is helpful118### Why this is helpful
63 119
482 538
483Follow the migration steps below to move from the Assistants API to the Responses API, without losing any feature support.539Follow the migration steps below to move from the Assistants API to the Responses API, without losing any feature support.
484 540
541For Ruby, follow [Migrate Ruby applications](#migrate-ruby-applications) to
542pass your application configuration directly in the request.
543
485### 1. Create prompts from your assistants544### 1. Create prompts from your assistants
486 545
4871. Identify the most important assistant objects in your application.5461. Identify the most important assistant objects in your application.
542conversation = openai.conversations.create(items=items)601conversation = openai.conversations.create(items=items)
543```602```
544 603
545```ruby
546# Replace the illustrative IDs and URLs below with your own resource values.
547require "openai"
548
549client = OpenAI::Client.new
550thread_id = "thread_123"
551messages = client.beta.threads.messages.list(thread_id, order: :asc)
552items = []
553messages.auto_paging_each do |message|
554 content = message.content.filter_map do |part|
555 case part
556 when OpenAI::Models::Beta::Threads::TextContentBlock
557 type = if message.role == OpenAI::Models::Beta::Threads::Message::Role::USER
558 :input_text
559 else
560 :output_text
561 end
562 {
563 type: type,
564 text: part.text.value
565 }
566 when OpenAI::Models::Beta::Threads::ImageURLContentBlock
567 {
568 type: :input_image,
569 image_url: part.image_url.url,
570 detail: part.image_url.detail
571 }
572 end
573 end
574 items << {
575 role: message.role,
576 content: content
577 }
578end
579conversation = client.conversations.create(
580 items: items
581)
582puts(conversation.id)
583```
584
585 604
586## Comparing full examples605## Comparing full examples
587 606
632 return {"content": messages.data[0].content}651 return {"content": messages.data[0].content}
633```652```
634 653
635```ruby
636# Replace the illustrative IDs and URLs below with your own resource values.
637require "openai"
638
639client = OpenAI::Client.new
640assistant_id = "asst_123"
641threads_by_session = {}
642
643handle_message = lambda do |session_id:, content:|
644 thread_id = threads_by_session[session_id]
645 unless thread_id
646 thread_id = client.beta.threads.create.id
647 threads_by_session[session_id] = thread_id
648 end
649
650 client.beta.threads.messages.create(
651 thread_id,
652 role: :user,
653 content: content
654 )
655 run = client.beta.threads.runs.create(
656 thread_id,
657 assistant_id: assistant_id
658 )
659 while [:queued, :in_progress].include?(run.status)
660 sleep(1)
661 run = client.beta.threads.runs.retrieve(run.id, thread_id: thread_id)
662 end
663
664 messages = client.beta.threads.messages.list(
665 thread_id,
666 order: :desc,
667 limit: 1
668 )
669 { content: messages.data&.first&.content }
670end
671
672puts(
673 handle_message.call(
674 session_id: "example-session",
675 content: "What are the five Ds of dodgeball?"
676 )
677)
678```
679
680 654
681 655
682 656
685 659
686Responses API660Responses API
687 661
688```typescript
689// Replace the illustrative IDs and URLs below with your own resource values.
690import express from "express";
691import OpenAI from "openai";
692
693const app = express();
694const client = new OpenAI();
695const conversationsBySession = new Map<string, Promise<string>>();
696
697app.use(express.json());
698
699app.post("/messages", async (request, response) => {
700 const { content, session_id: sessionId } = request.body ?? {};
701 if (
702 typeof content !== "string" ||
703 !content.trim() ||
704 typeof sessionId !== "string" ||
705 !sessionId.trim()
706 ) {
707 response.status(400).json({
708 error: "content and session_id must be non-empty strings.",
709 });
710 return;
711 }
712
713 let conversationIdPromise = conversationsBySession.get(sessionId);
714
715 if (!conversationIdPromise) {
716 conversationIdPromise = client.conversations
717 .create()
718 .then((conversation) => conversation.id)
719 .catch((error) => {
720 conversationsBySession.delete(sessionId);
721 throw error;
722 });
723 conversationsBySession.set(sessionId, conversationIdPromise);
724 }
725 const conversationId = await conversationIdPromise;
726
727 const promptId = "pmpt_123";
728
729 const result = await client.responses.create({
730 prompt: { id: promptId },
731 input: [{ role: "user", content }],
732 conversation: conversationId,
733 });
734
735 response.json({ content: result.output_text });
736});
737
738app.listen(Number(process.env.OPENAI_EXAMPLE_PORT ?? 8000), "127.0.0.1");
739```
740
741```python662```python
742# Replace the illustrative IDs and URLs below with your own resource values.663# Replace the illustrative IDs and URLs below with your own resource values.
743conversations_by_session: dict[str, str] = {}664conversations_by_session: dict[str, str] = {}
869}790}
870```791```
871 792
793```typescript
794// Replace the illustrative IDs and URLs below with your own resource values.
795import express from "express";
796import OpenAI from "openai";
797
798const app = express();
799const client = new OpenAI();
800const conversationsBySession = new Map<string, Promise<string>>();
801
802app.use(express.json());
803
804app.post("/messages", async (request, response) => {
805 const { content, session_id: sessionId } = request.body ?? {};
806 if (
807 typeof content !== "string" ||
808 !content.trim() ||
809 typeof sessionId !== "string" ||
810 !sessionId.trim()
811 ) {
812 response.status(400).json({
813 error: "content and session_id must be non-empty strings.",
814 });
815 return;
816 }
817
818 let conversationIdPromise = conversationsBySession.get(sessionId);
819
820 if (!conversationIdPromise) {
821 conversationIdPromise = client.conversations
822 .create()
823 .then((conversation) => conversation.id)
824 .catch((error) => {
825 conversationsBySession.delete(sessionId);
826 throw error;
827 });
828 conversationsBySession.set(sessionId, conversationIdPromise);
829 }
830 const conversationId = await conversationIdPromise;
831
832 const promptId = "pmpt_123";
833
834 const result = await client.responses.create({
835 prompt: { id: promptId },
836 input: [{ role: "user", content }],
837 conversation: conversationId,
838 });
839
840 response.json({ content: result.output_text });
841});
842
843app.listen(Number(process.env.OPENAI_EXAMPLE_PORT ?? 8000), "127.0.0.1");
844```
845
846
847
848<details>
849 <summary>Earlier Ruby example using a reusable prompt</summary>
850
851For new Ruby integrations, use the application-owned configuration in
852[Migrate Ruby applications](#migrate-ruby-applications).
853
872```ruby854```ruby
873# Replace the illustrative IDs and URLs below with your own resource values.855# Replace the illustrative IDs and URLs below with your own resource values.
874require "openai"856require "openai"
907 )889 )
908)890)
909```891```
892
893</details>