Skip to content

Commit 7e6e246

Browse files
Use stream helpers for SSE APIs
1 parent 237122e commit 7e6e246

14 files changed

Lines changed: 68 additions & 103 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# openai-cpp
22

3-
`openai-cpp` is a modern, fully native C++17 client for the OpenAI API. It mirrors the ergonomics of the official TypeScript SDK (`client.responses().create()`, `client.chat().completions().create_stream()`, etc.) while taking advantage of strong typing, RAII-friendly resource management, and portable CMake tooling.
3+
`openai-cpp` is a modern, fully native C++17 client for the OpenAI API. It mirrors the ergonomics of the official TypeScript SDK (`client.responses().create()`, `client.chat().completions().stream()`, etc.) while taking advantage of strong typing, RAII-friendly resource management, and portable CMake tooling.
44

55
> ⚠️ This library is still under active development. API coverage is broad, but expect surface area to evolve while the upstream OpenAI platform continues to ship new capabilities.
66
@@ -167,7 +167,7 @@ void stream_story(openai::OpenAIClient& client) {
167167
168168
request.input.push_back(item);
169169
170-
client.responses().create_stream(
170+
client.responses().stream(
171171
request,
172172
[&](const openai::ResponseStreamEvent& event) {
173173
if (event.text_delta && event.text_delta->output_index == 0) {

apps/chat_demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ int main()
9292
bool saw_error = false;
9393
std::optional<openai::Response> completed_response;
9494

95-
client.responses().create_stream(
95+
client.responses().stream(
9696
request,
9797
[&](const openai::ResponseStreamEvent& event)
9898
{

apps/demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int main()
6565
std::size_t chunk_index = 0;
6666
auto start = std::chrono::steady_clock::now();
6767

68-
client.responses().create_stream(
68+
client.responses().stream(
6969
stream_request,
7070
[&](const openai::ResponseStreamEvent &event)
7171
{

docs/PARITY_CHECKLIST.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Tracking progress toward 1:1 feature coverage with the official `openai-node` Ty
5858
- [x] Assistants runs (beta create/retrieve/update/list/cancel/submit outputs)
5959
- [x] Run steps (beta list/retrieve)
6060
- [x] Assistants streaming events (typed parser)
61-
- [x] Run streaming & polling helpers (`create_stream`, `stream`, `submit_tool_outputs_stream`, `poll`, `create_and_run_poll`)
61+
- [x] Run streaming & polling helpers (`stream`, `submit_tool_outputs_stream`, `poll`, `create_and_run_poll`)
6262
- [x] Thread create-and-run helpers (stream/poll)
6363
- [x] Assistants tool runner actions & automation helpers
6464
- [x] Fine-tuning
@@ -74,8 +74,8 @@ Tracking progress toward 1:1 feature coverage with the official `openai-node` Ty
7474
- [x] Azure OpenAI compatibility layer
7575

7676
## Streaming Coverage
77-
- [x] Responses streaming (`create_stream`, `retrieve_stream`)
78-
- [x] Chat completions streaming (`chat.completions.create_stream`)
77+
- [x] Responses streaming (`stream`, `retrieve_stream`)
78+
- [x] Chat completions streaming (`chat.completions.stream`)
7979
- [x] Assistants/Threads streaming helpers (`runs`, `run_steps`, `threads`)
8080
- [x] Images streaming (`images.generate_stream`, `images.edit_stream`)
8181
- [ ] Legacy completions streaming (`completions.create` with `stream=true`)

include/openai/chat.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,14 @@ class ChatCompletionsResource {
501501
ChatCompletion create(const ChatCompletionRequest& request,
502502
const struct RequestOptions& options) const;
503503

504-
std::vector<ServerSentEvent> create_stream(const ChatCompletionRequest& request) const;
505-
std::vector<ServerSentEvent> create_stream(const ChatCompletionRequest& request,
506-
const struct RequestOptions& options) const;
507-
void create_stream(const ChatCompletionRequest& request,
508-
const std::function<bool(const ServerSentEvent&)>& on_event) const;
509-
void create_stream(const ChatCompletionRequest& request,
510-
const std::function<bool(const ServerSentEvent&)>& on_event,
511-
const struct RequestOptions& options) const;
504+
std::vector<ServerSentEvent> stream(const ChatCompletionRequest& request) const;
505+
std::vector<ServerSentEvent> stream(const ChatCompletionRequest& request,
506+
const struct RequestOptions& options) const;
507+
void stream(const ChatCompletionRequest& request,
508+
const std::function<bool(const ServerSentEvent&)>& on_event) const;
509+
void stream(const ChatCompletionRequest& request,
510+
const std::function<bool(const ServerSentEvent&)>& on_event,
511+
const struct RequestOptions& options) const;
512512

513513
ChatCompletion retrieve(const std::string& completion_id) const;
514514
ChatCompletion retrieve(const std::string& completion_id, const struct RequestOptions& options) const;

include/openai/responses.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,14 +1199,13 @@ class ResponsesResource {
11991199
CursorPage<Response> list_page() const;
12001200
CursorPage<Response> list_page(const struct RequestOptions& options) const;
12011201

1202-
std::vector<ServerSentEvent> create_stream(const ResponseRequest& request) const;
1203-
std::vector<ServerSentEvent> create_stream(const ResponseRequest& request,
1204-
const struct RequestOptions& options) const;
1205-
void create_stream(const ResponseRequest& request,
1206-
const std::function<bool(const ResponseStreamEvent&)>& on_event) const;
1207-
void create_stream(const ResponseRequest& request,
1208-
const std::function<bool(const ResponseStreamEvent&)>& on_event,
1209-
const struct RequestOptions& options) const;
1202+
std::vector<ServerSentEvent> stream(const ResponseRequest& request) const;
1203+
std::vector<ServerSentEvent> stream(const ResponseRequest& request, const struct RequestOptions& options) const;
1204+
void stream(const ResponseRequest& request,
1205+
const std::function<bool(const ResponseStreamEvent&)>& on_event) const;
1206+
void stream(const ResponseRequest& request,
1207+
const std::function<bool(const ResponseStreamEvent&)>& on_event,
1208+
const struct RequestOptions& options) const;
12101209

12111210
std::vector<ServerSentEvent> retrieve_stream(const std::string& response_id) const;
12121211
std::vector<ServerSentEvent> retrieve_stream(const std::string& response_id,

include/openai/runs.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,6 @@ class RunsResource {
271271
const RunSubmitToolOutputsRequest& request,
272272
const RequestOptions& options) const;
273273

274-
std::vector<AssistantStreamEvent> create_stream(const std::string& thread_id, const RunCreateRequest& request) const;
275-
std::vector<AssistantStreamEvent> create_stream(const std::string& thread_id,
276-
const RunCreateRequest& request,
277-
const RequestOptions& options) const;
278-
void create_stream(const std::string& thread_id,
279-
const RunCreateRequest& request,
280-
const std::function<bool(const AssistantStreamEvent&)>& on_event) const;
281-
void create_stream(const std::string& thread_id,
282-
const RunCreateRequest& request,
283-
const std::function<bool(const AssistantStreamEvent&)>& on_event,
284-
const RequestOptions& options) const;
285-
286274
AssistantStreamSnapshot create_stream_snapshot(const std::string& thread_id, const RunCreateRequest& request) const;
287275
AssistantStreamSnapshot create_stream_snapshot(const std::string& thread_id,
288276
const RunCreateRequest& request,

src/chat.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -871,9 +871,9 @@ ChatCompletion ChatCompletionsResource::create(const ChatCompletionRequest& requ
871871
}
872872
}
873873

874-
void ChatCompletionsResource::create_stream(const ChatCompletionRequest& request,
875-
const std::function<bool(const ServerSentEvent&)>& on_event,
876-
const RequestOptions& options) const {
874+
void ChatCompletionsResource::stream(const ChatCompletionRequest& request,
875+
const std::function<bool(const ServerSentEvent&)>& on_event,
876+
const RequestOptions& options) const {
877877
json body = build_chat_request_body(request, true);
878878

879879
RequestOptions request_options = options;
@@ -895,19 +895,19 @@ void ChatCompletionsResource::create_stream(const ChatCompletionRequest& request
895895
stream.finalize();
896896
}
897897

898-
std::vector<ServerSentEvent> ChatCompletionsResource::create_stream(const ChatCompletionRequest& request) const {
899-
return create_stream(request, RequestOptions{});
898+
std::vector<ServerSentEvent> ChatCompletionsResource::stream(const ChatCompletionRequest& request) const {
899+
return stream(request, RequestOptions{});
900900
}
901901

902-
void ChatCompletionsResource::create_stream(const ChatCompletionRequest& request,
903-
const std::function<bool(const ServerSentEvent&)>& on_event) const {
904-
create_stream(request, on_event, RequestOptions{});
902+
void ChatCompletionsResource::stream(const ChatCompletionRequest& request,
903+
const std::function<bool(const ServerSentEvent&)>& on_event) const {
904+
stream(request, on_event, RequestOptions{});
905905
}
906906

907-
std::vector<ServerSentEvent> ChatCompletionsResource::create_stream(const ChatCompletionRequest& request,
908-
const RequestOptions& options) const {
907+
std::vector<ServerSentEvent> ChatCompletionsResource::stream(const ChatCompletionRequest& request,
908+
const RequestOptions& options) const {
909909
std::vector<ServerSentEvent> events;
910-
create_stream(
910+
stream(
911911
request,
912912
[&](const ServerSentEvent& event) {
913913
events.push_back(event);

src/chatkit.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ json state_variables_to_json(const std::optional<std::map<std::string, beta::Cha
2525
if (!values || values->empty()) return json::object();
2626

2727
json object = json::object();
28-
for (const auto& [key, value] : *values) {
28+
for (const auto& entry : *values) {
29+
const auto& key = entry.first;
30+
const auto& value = entry.second;
2931
std::visit(
3032
[&](auto&& arg) {
3133
object[key] = arg;

src/responses.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,8 +2430,8 @@ ResponseList ResponsesResource::list() const {
24302430
return list(RequestOptions{});
24312431
}
24322432

2433-
std::vector<ServerSentEvent> ResponsesResource::create_stream(const ResponseRequest& request,
2434-
const RequestOptions& options) const {
2433+
std::vector<ServerSentEvent> ResponsesResource::stream(const ResponseRequest& request,
2434+
const RequestOptions& options) const {
24352435
SSEEventStream stream;
24362436

24372437
auto body = build_request_body(request);
@@ -2448,13 +2448,13 @@ std::vector<ServerSentEvent> ResponsesResource::create_stream(const ResponseRequ
24482448
return stream.events();
24492449
}
24502450

2451-
std::vector<ServerSentEvent> ResponsesResource::create_stream(const ResponseRequest& request) const {
2452-
return create_stream(request, RequestOptions{});
2451+
std::vector<ServerSentEvent> ResponsesResource::stream(const ResponseRequest& request) const {
2452+
return stream(request, RequestOptions{});
24532453
}
24542454

2455-
void ResponsesResource::create_stream(const ResponseRequest& request,
2456-
const std::function<bool(const ResponseStreamEvent&)>& on_event,
2457-
const RequestOptions& options) const {
2455+
void ResponsesResource::stream(const ResponseRequest& request,
2456+
const std::function<bool(const ResponseStreamEvent&)>& on_event,
2457+
const RequestOptions& options) const {
24582458
SSEEventStream stream([&](const ServerSentEvent& sse_event) {
24592459
if (!on_event) {
24602460
return true;
@@ -2478,9 +2478,9 @@ void ResponsesResource::create_stream(const ResponseRequest& request,
24782478
stream.finalize();
24792479
}
24802480

2481-
void ResponsesResource::create_stream(const ResponseRequest& request,
2482-
const std::function<bool(const ResponseStreamEvent&)>& on_event) const {
2483-
create_stream(request, on_event, RequestOptions{});
2481+
void ResponsesResource::stream(const ResponseRequest& request,
2482+
const std::function<bool(const ResponseStreamEvent&)>& on_event) const {
2483+
stream(request, on_event, RequestOptions{});
24842484
}
24852485

24862486
std::vector<ServerSentEvent> ResponsesResource::retrieve_stream(const std::string& response_id,

0 commit comments

Comments
 (0)