Skip to content

Commit a2a1135

Browse files
authored
Fix Prompt deserialization defaults. (#6)
* Fix Prompt deserialization defaults. - Prompt now deserializes from an empty map or with fields missing, * Remove caching from CI - Writing the cache to disk took for CI on a crate this small hurt performance.
1 parent f82988b commit a2a1135

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,10 @@ jobs:
2323
with:
2424
components: llvm-tools-preview
2525

26-
- name: Cache cargo registry
27-
uses: actions/cache@v2
28-
with:
29-
path: ~/.cargo/registry
30-
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
31-
restore-keys: |
32-
${{ runner.os }}-cargo-registry-
33-
34-
- name: Cache cargo index
35-
uses: actions/cache@v2
36-
with:
37-
path: ~/.cargo/git
38-
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
39-
restore-keys: |
40-
${{ runner.os }}-cargo-index-
41-
42-
- name: Cache cargo build
43-
uses: actions/cache@v2
44-
with:
45-
path: target
46-
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
47-
restore-keys: |
48-
${{ runner.os }}-cargo-build-
49-
5026
- name: Build
5127
run: cargo build --all-features --verbose
5228

53-
- name: Run tests
29+
- name: Test
5430
run: cargo test --all-features --verbose
5531

5632
# This should only happen on push to main. PRs should not upload coverage.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "misanthropic"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
edition = "2021"
55
authors = ["Michael de Gans <[email protected]>"]
66
description = "An async, ergonomic, client for Anthropic's Messages API"

src/prompt.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use message::Message;
1717
/// [Anthropic Messages API]: <https://docs.anthropic.com/en/api/messages>
1818
#[derive(Serialize, Deserialize)]
1919
#[cfg_attr(any(feature = "partial_eq", test), derive(PartialEq))]
20+
#[serde(default)]
2021
pub struct Prompt<'a> {
2122
/// [`Model`] to use for inference.
2223
pub model: Model,
@@ -37,7 +38,7 @@ pub struct Prompt<'a> {
3738
pub max_tokens: NonZeroU16,
3839
/// Optional info about the request, for example, `user_id` to help
3940
/// Anthropic detect and prevent abuse. Do not use PII here (email, phone).
40-
#[serde(skip_serializing_if = "serde_json::Map::is_empty")]
41+
#[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
4142
pub metadata: serde_json::Map<String, serde_json::Value>,
4243
/// Optional stop sequences. If the model generates any of these sequences,
4344
/// the completion will stop with [`StopReason::StopSequence`].
@@ -794,6 +795,20 @@ mod tests {
794795
.is_cached());
795796
}
796797

798+
#[test]
799+
fn test_serde() {
800+
// Test default deserialization.
801+
const JSON: &str = r#"{}"#;
802+
803+
let defaults = serde_json::from_str::<Prompt>(JSON).unwrap();
804+
805+
// Another round trip to ensure serialization works.
806+
let json = serde_json::to_string(&defaults).unwrap();
807+
let _ = serde_json::from_str::<Prompt>(&json).unwrap();
808+
809+
// TODO: impl Default and PartialEq when `cfg(test)`
810+
}
811+
797812
#[test]
798813
fn test_tools() {
799814
// A tool can be added from a json object. This is fallible. It must

0 commit comments

Comments
 (0)