ccproxy.llms.streaming¶
ccproxy.llms.streaming
¶
Streaming utilities for LLM response formatting.
This module provides Server-Sent Events (SSE) formatting for various LLM streaming response formats including OpenAI-compatible and Anthropic formats.
ClaudeAccumulator
¶
Bases: StreamAccumulator
Accumulate Anthropic/Claude streaming events.
Source code in ccproxy/llms/streaming/accumulators.py
accumulate
¶
Accumulate Claude streaming events.
Processes Claude-specific event types like: - content_block_start - content_block_delta - content_block_stop
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_name
|
str
|
Name of the event |
required |
event_data
|
dict[str, Any]
|
Data associated with the event |
required |
Source code in ccproxy/llms/streaming/accumulators.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
get_complete_tool_calls
¶
Get complete tool calls accumulated so far.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of complete tool calls |
Source code in ccproxy/llms/streaming/accumulators.py
rebuild_response_object
¶
Rebuild the complete Claude response with all accumulated content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
dict[str, Any]
|
Original Claude response |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Rebuilt response with complete content |
Source code in ccproxy/llms/streaming/accumulators.py
get_block_info
¶
Return (block_id, block_data) for a content block index.
Source code in ccproxy/llms/streaming/accumulators.py
get_tool_entry
¶
Fetch the tool metadata tracked by the accumulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
int | str
|
Either the integer index from the stream event or the underlying block identifier tracked by the accumulator. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The tracked tool entry if present. |
Source code in ccproxy/llms/streaming/accumulators.py
OpenAIAccumulator
¶
Bases: StreamAccumulator
Accumulate tool calls emitted via OpenAI chat/completion deltas.
Source code in ccproxy/llms/streaming/accumulators.py
accumulate
¶
Accumulate OpenAI streaming events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_name
|
str
|
Name of the event |
required |
event_data
|
dict[str, Any]
|
Data associated with the event |
required |
Source code in ccproxy/llms/streaming/accumulators.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
get_complete_tool_calls
¶
Get complete tool calls accumulated so far.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of complete tool calls |
Source code in ccproxy/llms/streaming/accumulators.py
rebuild_response_object
¶
Rebuild the complete OpenAI response with all accumulated content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
dict[str, Any]
|
Original OpenAI response |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Rebuilt response with complete content |
Source code in ccproxy/llms/streaming/accumulators.py
ResponsesAccumulator
¶
Bases: StreamAccumulator
Accumulate events emitted by the OpenAI Responses API using typed models.
Source code in ccproxy/llms/streaming/accumulators.py
accumulate
¶
Accumulate Responses API streaming events.
Source code in ccproxy/llms/streaming/accumulators.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
get_complete_tool_calls
¶
Get complete tool calls accumulated so far.
Source code in ccproxy/llms/streaming/accumulators.py
rebuild_response_object
¶
Rebuild a complete Responses API payload with accumulated data.
Source code in ccproxy/llms/streaming/accumulators.py
get_completed_response
¶
Return the final response payload captured from the stream, if any.
Source code in ccproxy/llms/streaming/accumulators.py
StreamAccumulator
¶
Base class for accumulating streaming response chunks.
Source code in ccproxy/llms/streaming/accumulators.py
accumulate
¶
get_complete_tool_calls
¶
rebuild_response_object
¶
Rebuild the complete response object with accumulated content.
This method takes a response object and rebuilds it to include all accumulated content like tool calls, content blocks, thinking/reasoning, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
dict[str, Any]
|
The original response object |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The updated response with all accumulated content |
Source code in ccproxy/llms/streaming/accumulators.py
AnthropicSSEFormatter
¶
Formats streaming responses to match Anthropic's Messages API SSE format.
format_event
staticmethod
¶
Format an event for Anthropic Messages API Server-Sent Events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
str
|
Event type (e.g., 'message_start', 'content_block_delta') |
required |
data
|
dict[str, Any]
|
Event data dictionary |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted SSE string with event and data lines |
Source code in ccproxy/llms/streaming/formatters.py
format_ping
staticmethod
¶
OpenAISSEFormatter
¶
Formats streaming responses to match OpenAI's SSE format.
format_data_event
staticmethod
¶
format_first_chunk
staticmethod
¶
Format the first chunk with role and basic metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_id
|
str
|
Unique identifier for the completion |
required |
model
|
str
|
Model name being used |
required |
created
|
int
|
Unix timestamp when the completion was created |
required |
role
|
str
|
Role of the assistant |
'assistant'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted SSE string |
Source code in ccproxy/llms/streaming/formatters.py
format_content_chunk
staticmethod
¶
Format a content chunk with text delta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_id
|
str
|
Unique identifier for the completion |
required |
model
|
str
|
Model name being used |
required |
created
|
int
|
Unix timestamp when the completion was created |
required |
content
|
str
|
Text content to include in the delta |
required |
choice_index
|
int
|
Index of the choice (usually 0) |
0
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted SSE string |
Source code in ccproxy/llms/streaming/formatters.py
format_tool_call_chunk
staticmethod
¶
format_tool_call_chunk(
message_id,
model,
created,
tool_call_id,
function_name=None,
function_arguments=None,
tool_call_index=0,
choice_index=0,
)
Format a tool call chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_id
|
str
|
Unique identifier for the completion |
required |
model
|
str
|
Model name being used |
required |
created
|
int
|
Unix timestamp when the completion was created |
required |
tool_call_id
|
str
|
ID of the tool call |
required |
function_name
|
str | None
|
Name of the function being called |
None
|
function_arguments
|
str | None
|
Arguments for the function |
None
|
tool_call_index
|
int
|
Index of the tool call |
0
|
choice_index
|
int
|
Index of the choice (usually 0) |
0
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted SSE string |
Source code in ccproxy/llms/streaming/formatters.py
format_final_chunk
staticmethod
¶
Format the final chunk with finish_reason.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_id
|
str
|
Unique identifier for the completion |
required |
model
|
str
|
Model name being used |
required |
created
|
int
|
Unix timestamp when the completion was created |
required |
finish_reason
|
str
|
Reason for completion (stop, length, tool_calls, etc.) |
'stop'
|
choice_index
|
int
|
Index of the choice (usually 0) |
0
|
usage
|
dict[str, int] | None
|
Optional usage information to include |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted SSE string |
Source code in ccproxy/llms/streaming/formatters.py
format_error_chunk
staticmethod
¶
Format an error chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_id
|
str
|
Unique identifier for the completion |
required |
model
|
str
|
Model name being used |
required |
created
|
int
|
Unix timestamp when the completion was created |
required |
error_type
|
str
|
Type of error |
required |
error_message
|
str
|
Error message |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted SSE string |
Source code in ccproxy/llms/streaming/formatters.py
AnthropicStreamProcessor
¶
Processes OpenAI streaming data into Anthropic SSE format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model name for responses |
'claude-3-5-sonnet-20241022'
|
Source code in ccproxy/llms/streaming/processors.py
process_stream
async
¶
Process OpenAI-format streaming data into Anthropic SSE format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
AsyncIterator[dict[str, Any]]
|
Async iterator of OpenAI-style response chunks |
required |
Yields:
| Type | Description |
|---|---|
AsyncIterator[str]
|
Anthropic-formatted SSE strings with proper event: lines |
Source code in ccproxy/llms/streaming/processors.py
OpenAIStreamProcessor
¶
OpenAIStreamProcessor(
message_id=None,
model="claude-3-5-sonnet-20241022",
created=None,
enable_usage=True,
enable_tool_calls=True,
enable_thinking_serialization=None,
output_format="sse",
)
Processes Anthropic/Claude streaming responses into OpenAI format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_id
|
str | None
|
Response ID, generated if not provided |
None
|
model
|
str
|
Model name for responses |
'claude-3-5-sonnet-20241022'
|
created
|
int | None
|
Creation timestamp, current time if not provided |
None
|
enable_usage
|
bool
|
Whether to include usage information |
True
|
enable_tool_calls
|
bool
|
Whether to process tool calls |
True
|
output_format
|
Literal['sse', 'dict']
|
Output format - "sse" for Server-Sent Events strings, "dict" for dict objects |
'sse'
|
Source code in ccproxy/llms/streaming/processors.py
process_stream
async
¶
Process a Claude/Anthropic stream into OpenAI format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
claude_stream
|
AsyncIterator[dict[str, Any]]
|
Async iterator of Claude response chunks |
required |
Yields:
| Type | Description |
|---|---|
AsyncIterator[str | dict[str, Any]]
|
OpenAI-formatted SSE strings or dict objects based on output_format |
Source code in ccproxy/llms/streaming/processors.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |