mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-12 23:01:41 +00:00
Optimize response string construction
Use list accumulation and join in get_response_string to avoid O(n^2) string concatenation and add a brief comment explaining the performance rationale.
This commit is contained in:
parent
8ea0e31678
commit
98e2b64040
1 changed files with 12 additions and 8 deletions
|
|
@ -4437,17 +4437,20 @@ def get_response_string(response_obj: Union[ModelResponse, ModelResponseStream])
|
|||
responses_api_response = getattr(response_obj, "response", None)
|
||||
if responses_api_response and hasattr(responses_api_response, "output"):
|
||||
output_list = responses_api_response.output
|
||||
response_str = ""
|
||||
# Use list accumulation to avoid O(n^2) string concatenation:
|
||||
# repeatedly doing `response_str += part` copies the full string each time
|
||||
# because Python strings are immutable, so total work grows with n^2.
|
||||
response_output_parts: List[str] = []
|
||||
for output_item in output_list:
|
||||
# Handle output items with content array
|
||||
if hasattr(output_item, "content"):
|
||||
for content_part in output_item.content:
|
||||
if hasattr(content_part, "text"):
|
||||
response_str += content_part.text
|
||||
response_output_parts.append(content_part.text)
|
||||
# Handle output items with direct text field
|
||||
elif hasattr(output_item, "text"):
|
||||
response_str += output_item.text
|
||||
return response_str
|
||||
response_output_parts.append(output_item.text)
|
||||
return "".join(response_output_parts)
|
||||
|
||||
# Handle Responses API text delta events
|
||||
if hasattr(response_obj, "type") and hasattr(response_obj, "delta"):
|
||||
|
|
@ -4461,16 +4464,17 @@ def get_response_string(response_obj: Union[ModelResponse, ModelResponseStream])
|
|||
response_obj.choices
|
||||
)
|
||||
|
||||
response_str = ""
|
||||
# Use list accumulation to avoid O(n^2) string concatenation across choices
|
||||
response_parts: List[str] = []
|
||||
for choice in _choices:
|
||||
if isinstance(choice, Choices):
|
||||
if choice.message.content is not None:
|
||||
response_str += choice.message.content
|
||||
response_parts.append(str(choice.message.content))
|
||||
elif isinstance(choice, StreamingChoices):
|
||||
if choice.delta.content is not None:
|
||||
response_str += choice.delta.content
|
||||
response_parts.append(str(choice.delta.content))
|
||||
|
||||
return response_str
|
||||
return "".join(response_parts)
|
||||
|
||||
|
||||
def get_api_key(llm_provider: str, dynamic_api_key: Optional[str]):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue