|
23 | 23 | ContentBlockDeltaEvent, |
24 | 24 | ContentBlockStartEvent, |
25 | 25 | ContentBlockStopEvent, |
26 | | - MessageDeltaEvent, |
| 26 | + # MessageDeltaEvent, # Broken at the moment |
27 | 27 | MessageStopEvent, |
28 | 28 | ) |
29 | 29 | from anthropic.types import Message as AnthropicMessage |
@@ -137,16 +137,16 @@ def _prepare_request_kwargs( |
137 | 137 | Dict[str, Any]: The request keyword arguments. |
138 | 138 | """ |
139 | 139 | request_kwargs = self.request_kwargs.copy() |
140 | | - |
141 | | - if self.cache_system_prompt: |
142 | | - cache_control = ( |
143 | | - {"type": "ephemeral", "ttl": "1h"} |
144 | | - if self.extended_cache_time is not None and self.extended_cache_time is True |
145 | | - else {"type": "ephemeral"} |
146 | | - ) |
147 | | - request_kwargs["system"] = [{"text": system_message, "type": "text", "cache_control": cache_control}] |
148 | | - else: |
149 | | - request_kwargs["system"] = [{"text": system_message, "type": "text"}] |
| 140 | + if system_message: |
| 141 | + if self.cache_system_prompt: |
| 142 | + cache_control = ( |
| 143 | + {"type": "ephemeral", "ttl": "1h"} |
| 144 | + if self.extended_cache_time is not None and self.extended_cache_time is True |
| 145 | + else {"type": "ephemeral"} |
| 146 | + ) |
| 147 | + request_kwargs["system"] = [{"text": system_message, "type": "text", "cache_control": cache_control}] |
| 148 | + else: |
| 149 | + request_kwargs["system"] = [{"text": system_message, "type": "text"}] |
150 | 150 |
|
151 | 151 | if tools: |
152 | 152 | request_kwargs["tools"] = self._format_tools_for_model(tools) |
@@ -212,7 +212,6 @@ def invoke( |
212 | 212 | try: |
213 | 213 | chat_messages, system_message = format_messages(messages) |
214 | 214 | request_kwargs = self._prepare_request_kwargs(system_message, tools) |
215 | | - |
216 | 215 | return self.get_client().messages.create( |
217 | 216 | model=self.id, |
218 | 217 | messages=chat_messages, # type: ignore |
@@ -473,7 +472,7 @@ def parse_provider_response(self, response: AnthropicMessage, **kwargs) -> Model |
473 | 472 | return model_response |
474 | 473 |
|
475 | 474 | def parse_provider_response_delta( |
476 | | - self, response: Union[ContentBlockDeltaEvent, ContentBlockStopEvent, MessageDeltaEvent] |
| 475 | + self, response: Union[ContentBlockStartEvent, ContentBlockDeltaEvent, ContentBlockStopEvent, MessageStopEvent] |
477 | 476 | ) -> ModelResponse: |
478 | 477 | """ |
479 | 478 | Parse the Claude streaming response into ModelProviderResponse objects. |
@@ -543,12 +542,12 @@ def parse_provider_response_delta( |
543 | 542 | DocumentCitation(document_title=citation.document_title, cited_text=citation.cited_text) |
544 | 543 | ) |
545 | 544 |
|
546 | | - if response.message.usage is not None: |
547 | | - model_response.response_usage = { |
548 | | - "cache_write_tokens": response.usage.cache_creation_input_tokens, |
549 | | - "cached_tokens": response.usage.cache_read_input_tokens, |
550 | | - "input_tokens": response.usage.input_tokens, |
551 | | - "output_tokens": response.usage.output_tokens, |
552 | | - } |
| 545 | + if hasattr(response, "usage") and response.usage is not None: |
| 546 | + model_response.response_usage = { |
| 547 | + "cache_write_tokens": response.usage.cache_creation_input_tokens or 0, |
| 548 | + "cached_tokens": response.usage.cache_read_input_tokens or 0, |
| 549 | + "input_tokens": response.usage.input_tokens or 0, |
| 550 | + "output_tokens": response.usage.output_tokens or 0, |
| 551 | + } |
553 | 552 |
|
554 | 553 | return model_response |
0 commit comments