Skip to content

Commit

Permalink
[realtime] Address serialization issue with ConversationToolChoice (#282
Browse files Browse the repository at this point in the history
)
  • Loading branch information
trrwilson authored Nov 4, 2024
1 parent fa2c26f commit 9de3709
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Made improvements to the experimental Realtime API. Please note this features area is currently under rapid development and not all changes may be reflected here. (commit_hash)
- Several types have been renamed for consistency and clarity.
- `ConversationRateLimitsUpdate` (previously `ConversationRateLimitsUpdatedUpdate`) now includes named `RequestDetails` and `TokenDetails` properties, mapping to the corresponding named items in the underlying `rate_limits` command payload.
- Serialization and deserialization of `ConversationToolChoice` literal values (such as `"required"`) is fixed

### Other Changes

Expand Down
9 changes: 5 additions & 4 deletions api/OpenAI.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2755,10 +2755,11 @@ public class ConversationToolChoice : IJsonModel<ConversationToolChoice>, IPersi
BinaryData IPersistableModel<ConversationToolChoice>.Write(ModelReaderWriterOptions options);
}
public enum ConversationToolChoiceKind {
Auto = 0,
None = 1,
Required = 2,
Function = 3
Unknown = 0,
Auto = 1,
None = 2,
Required = 3,
Function = 4
}
public readonly partial struct ConversationToolKind : IEquatable<ConversationToolKind> {
private readonly object _dummy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public ConversationToolChoice ToolChoice
get => ConversationToolChoice.FromBinaryData(_internalToolChoice);
set
{
_internalToolChoice = ModelReaderWriter.Write(value);
_internalToolChoice = value is not null
? ModelReaderWriter.Write(value)
: null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static void SerializeConversationToolChoice(ConversationToolChoice inst
}
else
{
writer.WriteStringValue(instance.Kind.ToString());
writer.WriteStringValue(instance.Kind.ToSerialString());
}
}

Expand All @@ -50,7 +50,7 @@ internal static ConversationToolChoice DeserializeConversationToolChoice(JsonEle
return choiceObject switch
{
InternalRealtimeToolChoiceFunctionObject => new(ConversationToolChoiceKind.Function, choiceObject),
_ => null,
_ => new(ConversationToolChoiceKind.Unknown, choiceObject),
};
}
return null;
Expand Down
4 changes: 4 additions & 0 deletions src/Custom/RealtimeConversation/ConversationToolChoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ internal ConversationToolChoice(ConversationToolChoiceKind choiceKind, InternalR
Kind = choiceKind;
_objectToolChoice = objectToolChoice;
}

internal ConversationToolChoice() : this(ConversationToolChoiceKind.Unknown, null)
{
}
}
2 changes: 2 additions & 0 deletions src/Custom/RealtimeConversation/ConversationToolChoiceKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ namespace OpenAI.RealtimeConversation;
[CodeGenModel("RealtimeToolChoiceLiteral")]
public enum ConversationToolChoiceKind
{
Unknown,
[CodeGenMember("Auto")]
Auto,
[CodeGenMember("None")]
None,
[CodeGenMember("Required")]
Required,
[CodeGenMember("Function")]
Function,
}
29 changes: 29 additions & 0 deletions tests/RealtimeConversation/ConversationSmokeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,35 @@ public class ConversationSmokeTests : ConversationTestFixtureBase
public ConversationSmokeTests(bool isAsync) : base(isAsync)
{ }

[Test]
public void ToolChoiceSerializationInSessionOptionsWorks()
{
foreach((ConversationToolChoice toolChoice, string expected) in new (ConversationToolChoice, string)[]
{
(null, "{}"),
(ConversationToolChoice.CreateNoneToolChoice(), @"{""tool_choice"":""none""}"),
(ConversationToolChoice.CreateAutoToolChoice(), @"{""tool_choice"":""auto""}"),
(ConversationToolChoice.CreateRequiredToolChoice(), @"{""tool_choice"":""required""}"),
(ConversationToolChoice.CreateFunctionToolChoice("foo"), @"{""function"":{""name"":""foo""}")
})
{
ConversationSessionOptions options = new()
{
ToolChoice = toolChoice,
};
Assert.That(ModelReaderWriter.Write(options).ToString(), Does.Contain(expected));
}

ConversationToolChoice mrwToolChoice = ModelReaderWriter.Read<ConversationToolChoice>(
BinaryData.FromString("""
{
"type":"some_manual_type"
}
"""));
Assert.That(mrwToolChoice.Kind, Is.EqualTo(ConversationToolChoiceKind.Unknown));
Assert.That(ModelReaderWriter.Write(mrwToolChoice).ToString(), Does.Contain(@"""type"":""some_manual_type"""));
}

[Test]
public void ItemCreation()
{
Expand Down

0 comments on commit 9de3709

Please sign in to comment.