@@ -645,3 +645,108 @@ func TestPromptSerialization(t *testing.T) {
645645 }
646646 })
647647}
648+
649+ func TestStreamPartErrorSerialization (t * testing.T ) {
650+ t .Run ("stream part with ProviderError containing OpenAI API error" , func (t * testing.T ) {
651+ // Create a mock OpenAI API error
652+ openaiErr := errors .New ("invalid_api_key: Incorrect API key provided" )
653+
654+ // Wrap in ProviderError
655+ providerErr := & ProviderError {
656+ Title : "unauthorized" ,
657+ Message : "Incorrect API key provided" ,
658+ Cause : openaiErr ,
659+ URL : "https://api.openai.com/v1/chat/completions" ,
660+ StatusCode : 401 ,
661+ RequestBody : []byte (`{"model":"gpt-4","messages":[]}` ),
662+ ResponseHeaders : map [string ]string {
663+ "content-type" : "application/json" ,
664+ },
665+ ResponseBody : []byte (`{"error":{"message":"Incorrect API key provided","type":"invalid_request_error"}}` ),
666+ }
667+
668+ // Create StreamPart with error
669+ streamPart := StreamPart {
670+ Type : StreamPartTypeError ,
671+ Error : providerErr ,
672+ }
673+
674+ // Marshal the stream part
675+ data , err := json .Marshal (streamPart )
676+ if err != nil {
677+ t .Fatalf ("failed to marshal stream part: %v" , err )
678+ }
679+
680+ // Unmarshal back
681+ var decoded StreamPart
682+ err = json .Unmarshal (data , & decoded )
683+ if err != nil {
684+ t .Fatalf ("failed to unmarshal stream part: %v" , err )
685+ }
686+
687+ // Verify the stream part type
688+ if decoded .Type != StreamPartTypeError {
689+ t .Errorf ("type mismatch: got %v, want %v" , decoded .Type , StreamPartTypeError )
690+ }
691+
692+ // Verify error exists
693+ if decoded .Error == nil {
694+ t .Fatal ("expected error to be present, got nil" )
695+ }
696+
697+ // Verify error message
698+ expectedMsg := "unauthorized: Incorrect API key provided"
699+ if decoded .Error .Error () != expectedMsg {
700+ t .Errorf ("error message mismatch: got %q, want %q" , decoded .Error .Error (), expectedMsg )
701+ }
702+ })
703+
704+ t .Run ("unmarshal stream part with error from JSON" , func (t * testing.T ) {
705+ // JSON representing a StreamPart with an error
706+ jsonData := `{
707+ "type": "error",
708+ "error": "unauthorized: Incorrect API key provided",
709+ "id": "",
710+ "tool_call_name": "",
711+ "tool_call_input": "",
712+ "delta": "",
713+ "provider_executed": false,
714+ "usage": {
715+ "input_tokens": 0,
716+ "output_tokens": 0,
717+ "total_tokens": 0,
718+ "reasoning_tokens": 0,
719+ "cache_creation_tokens": 0,
720+ "cache_read_tokens": 0
721+ },
722+ "finish_reason": "",
723+ "warnings": null,
724+ "source_type": "",
725+ "url": "",
726+ "title": "",
727+ "provider_metadata": null
728+ }`
729+
730+ var streamPart StreamPart
731+ err := json .Unmarshal ([]byte (jsonData ), & streamPart )
732+ if err != nil {
733+ t .Fatalf ("failed to unmarshal stream part: %v" , err )
734+ }
735+
736+ // Verify the stream part type
737+ if streamPart .Type != StreamPartTypeError {
738+ t .Errorf ("type mismatch: got %v, want %v" , streamPart .Type , StreamPartTypeError )
739+ }
740+
741+ // Verify error exists
742+ if streamPart .Error == nil {
743+ t .Fatal ("expected error to be present, got nil" )
744+ }
745+
746+ // Verify error message
747+ expectedMsg := "unauthorized: Incorrect API key provided"
748+ if streamPart .Error .Error () != expectedMsg {
749+ t .Errorf ("error message mismatch: got %q, want %q" , streamPart .Error .Error (), expectedMsg )
750+ }
751+ })
752+ }
0 commit comments